Backend as Amazon RDS

Aman Jhagrolia
4 min readSep 2, 2020
Terraform + Amazon RDS + WordPress

Amazon Relational Database Service is a distributed relational database service by Amazon Web Services. It is a web service running “in the cloud” designed to simplify the setup, operation, and scaling of a relational database for use in applications.

Here we will deploy WordPress on our local Kubernetes cluster but the database backend of our WordPress will be AWS Database Instance provided by Amazon RDS Service of AWS Cloud. To create this complete infrastructure we will use Terraform as an automation tool.

GitHub link for Terraform code :- https://github.com/jhagdu/terraform-aws-rds-wp.git

Here Terraform code is divided into two modules. One module named as aws is created to manage Amazon RDS and another module named as Kubernetes is to manage the Local Kubernetes cluster. So first let's take an overview of Terraform code

variables.tf - This is the terraform file that contains variables.

Variables File

main.tf - This is our Main terraform code file. It Contains all the providers and all other modules are imported. Finally, a “local-exec” provisioner is used to open the WordPress site automatically as soon as the complete infrastructure is deployed.

Main Terraform File

AWS Module - This module contains one terraform file named rds.tf which will launch Amazon Database Instance for us. Here we are launching Instance with MySQL database Engine and after launching a database with name “wpdb” is also created. It will also create a Security Group for our database instance which allows TCP Port 3306 so that our WordPress can connect to MySQL Instance to store data.

rds.tf From aws module

Kubernetes Module - It contains one terraform file named deploy_wp.tf which create a deployment for WordPress pod on our local Kubernetes cluster. Along with it will create a service to expose the WordPress deployment to the public world and also create PVC for WordPress.

deploy_wp.tf form Kubernetes module

So we are ready with our terraform code and now on running the code complete infrastructure is created from WordPress on K8s Cluster to Amazon RDS. Before running the code, here is the status of the K8s cluster and Amazon RDS. We can see that in the k8s cluster no pod, deployment or services is running and also there is no database instance in Amazon RDS.

Status before running terraform code

Now we will run our terraform code and for this, we just need to run the “terraform apply” command and our infrastructure start building and after completion, we can see that 7 resources are added.

Running Terraform Code

Now in Amazon RDS, we can see that our MySQL database instance is running and have one connection also i.e. our WordPress. In the configuration, we can see a database with name “wpdb” is also created.

MySQL Database Instance

Here is our security group which is created for Amazon RDS and have one inbound rule which allows us to connect through TCP Port 3306

Security Group

Now if we run “kubectl get all” command we found that one deployment for WordPress pod is running and also one service is created. To store some WordPress configuration data permanently a PVC is created.

Kubernetes Resources

Finally, we are successfully connected to our WordPress

At last, I'm concluding my article. Hope this is helpful to you. Thanks🙂

--

--