Deploy the WordPress application on Kubernetes and AWS using Terraform

Image for post
Image for post
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:

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.

Image for post
Image for post
aws configure — profile myprofile

Then we also have to specify the Availability Zones.

Image for post
Image for post
provider.tf

Our next step will be to create the RDS:

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

Image for post
Image for post
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:

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

Image for post
Image for post
wordpress.tf
Image for post
Image for post
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
Image for post
Image for post

To check whether any pods are running:

kubectl get all
Image for post
Image for post
Checking if any pods are running initially

Step 2:

Run terraform init to initialize the modules.

terraform init
Image for post
terraform init

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

terraform apply --auto-approve
Image for post
Image for post
terraform apply — auto-approve

Output:

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

Image for post
Image for post
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
Image for post
Image for post
kubectl get all

The Wordpress setup:

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post

The Wordpress dashboard is ready for us:

Image for post
Image for post
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).

Image for post
Image for post
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
Image for post
Image for post
Image for post
Image for post
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.

Written by

I am a DevOps Enthusiast and recently taken to Cloud Computing. Learning Flutter App Development currently. In my free time I engage in competitive coding.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store