Deploy the WordPress Application on Kubernetes and RDS services of AWS using Terraform
Amazon Relational Database Service (RDS):
Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups. It frees you to focus on your applications so you can give them the fast performance, high availability, security and compatibility they need.
Description of the task:
Deploy the Wordpress application on Kubernetes and AWS using terraform including the following steps;
1. Write an Infrastructure as code using terraform, which automatically deploy the Wordpress application
2. On AWS, use RDS service for the relational database for Wordpress application.
3. Deploy the Wordpress as a container either on top of Minikube or EKS or Fargate service on AWS.
4. The Wordpress application should be accessible from the public world if deployed on AWS or through workstation if deployed on Minikube.
Solution:
1. Create a Provider.
provider "aws"{ region = "ap-south-1" profile= "myawsprofile" }
2. Create VPC and Subnet.
data "aws_vpc" "default" { default= true } data "aws_subnet_ids" "all" { vpc_id= data.aws_vpc.default.id }
3.Create RDS service on AWS.
module "db" { source = "terraform-aws-modules/rds/aws" version = "~> 2.0" identifier = "db1" engine = "mysql" engine_version = "5.7.19" instance_class = "db.t2.micro" allocated_storage = 10 storage_type ="gp2" storage_encrypted = false username = "Demon" password = "ritikgupta" port = "3306" iam_database_authentication_enabled = false subnet_ids = data.aws_subnet_ids.all.ids publicly_accessible= true availability_zone ="ap-south-1a" maintenance_window = "Mon:00:00-Mon:03:00" backup_window = "03:00-06:00" tags = { Owner = "user" Environment = "dev" } # DB parameter group family = "mysql5.7" # DB option group major_engine_version = "5.7" # Snapshot name upon DB deletion final_snapshot_identifier = "demodb" # Database Deletion Protection deletion_protection = false multi_az = false backup_retention_period=0 enabled_cloudwatch_logs_exports=["audit","general"] parameters = [ { name = "character_set_client" value = "utf8" }, { name = "character_set_server" value = "utf8" } ] options = [ { option_name = "MARIADB_AUDIT_PLUGIN" option_settings = [ { name = "SERVER_AUDIT_EVENTS" value = "CONNECT" }, { name = "SERVER_AUDIT_FILE_ROTATIONS" value = "37" }, ] }, ] }
4.Create kubernetes deployment which deploys WordPress over minkube.
provider "kubernetes" { config_context_cluster = "minikube" } resource "kubernetes_deployment""wordpress" { metadata { name = "mywp" } spec { replicas = 2 selector { match_labels = { env="production" region="IN" app="wordpress" } match_expressions{ key="env" operator="In" values= ["production"] } } template { metadata { labels = { env="production" region="IN" app="wordpress" } } spec { container { image = "wordpress" name = "mywp" } } } } } resource "kubernetes_service" "wordpress" { metadata { name = "mywp" } spec { selector = { app = kubernetes_deployment.wordpress.spec.0.template.0.metadata[0].labels.app } port { node_port = 30321 port = 80 target_port = 80 } type = "NodePort" } }
5. Now, we use the following command to start the minikube.
minkube start
6.Now, our terraform code is complete. So, we have to initialize and launch the infrastructure by using following commands:
terraform init terraform validate terraform apply
7. Now we go to the AWS console and check the database launched in RDS service.
8. we can check the pods launched over kubernetes by using following command :
kubectl get all
9.By using the provided URL, we can launch the WordPress Server.
10.At the end, to delete the whole infrastructure we use the following command:
terraform destroy