Deploy the WordPress application on Kubernetes and AWS using Terraform

AWS-RDS | Minikube | Terraform | WordPress

Let’s start by knowing what will we be doing?

Deploying a WordPress website on local Kubernetes using Minikube and storing the data of WordPress inside Amazon RDS (Amazon Relational Database Service) by using Terraform as Infrastructure-as-a-Code.

Steps to perform the above task:

  • Create an AWS RDS Instance and all required dependencies.
  • Configure WordPress on local Kubernetes via Minikube.
  • Put the data of WordPress into AWS RDS (MySQL).
  • Then finally expose the WordPress pod/deployment to be accessible to the outside public.
  • All these steps should be done using Terraform as IaaC.

Let us start by setting up our environment:

Assuming that AWS CLI is installed and configured:

aws configure --profile  myprofile

The profile is set so that the Terraform code automatically picks up credentials from the local system without passing it through code.

aws configure — profile myprofile

Then we also have to specify the Availability Zones.

provider.tf

Our next step will be to create the RDS:

To create an RDS we need to keep these in mind:

  • Create a VPC
  • Add Subnets to the VPC
  • Create a VPC Security Group
  • Create a DB Subnet Group
  • Create a DB Instance in the VPC
rds.tf

Now we will create WordPress pod on local Kubernetes using Minikube:

The image used to create a pod is “wordpress” so that it automatically downloads the latest image and uses it to create the pod.

Using Environment variables I have provided values to the WordPress configuration:

  • WORDPRESS_DB_HOST — URL of the DB Instance.
  • WORDPRESS_DB_DATABASE — Name of the DB.
  • WORDPRESS_DB_USER — Username to connect to.
  • WORDPRESS_DB_PASSWORD — Password of the DB.

Then finally specified the port of the WordPress pod — 80.

wordpress.tf
wordpress.tf

Finally created the Service for the WordPress pod. Where we have specified the node_port = 31002. This will make the page accessible to the outside world.

Working -

Step 1 :

Start Minikube in the local system.

minikube start

To check whether any pods are running:

kubectl get all
Checking if any pods are running initially

Step 2:

Run terraform init to initialize the modules.

terraform init
terraform init

Run terraform apply. This will create the RDS Instances and launch the WordPress pod in Minikube.

terraform apply --auto-approve
terraform apply — auto-approve

Output:

We can check our AWS-RDS wordpress-db is launched.

wordpress-db

Minikube output after launch:

The Wordpress pods are launched along with the service, thus exposing the pod. To check use the below command.

kubectl get all
kubectl get all

The Wordpress setup:

The Wordpress dashboard is ready for us:

WordPress Dashboard

The proof that WordPress is connected to the RDS:

We can see below that the endpoint is same in both cases (highlighted part).

The proof that WordPress is connected to the RDS

After we are done we can destroy the entire setup in one-click using the below given command:

terraform destroy --auto-approve
terraform destroy — auto-approve

All the codes are available in GitHub.

If you have any queries/suggestions feel free to connect with me on LinkedIn.

If you liked the article , do 👏👏.

Thank you.