Deploy the WordPress application on Kubernetes and AWS using Terraform
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.
Then we also have to specify the Availability Zones.
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
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.
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.
Step 1 :
Start Minikube in the local system.
To check whether any pods are running:
kubectl get all
Run terraform init to initialize the modules.
Run terraform apply. This will create the RDS Instances and launch the WordPress pod in Minikube.
terraform apply --auto-approve
We can check our AWS-RDS wordpress-db is launched.
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
The Wordpress setup:
The Wordpress dashboard is ready for us:
The proof that WordPress is connected to the RDS:
We can see below that the endpoint is same in both cases (highlighted part).
After we are done we can destroy the entire setup in one-click using the below given command:
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 👏👏.