Deploying WordPress on Kubernetes and AWS using Terraform

Arifiya Khan
6 min readSep 15, 2020

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.

What is RDS?

Amazon Relational Database Service (or Amazon RDS) is a distributed relational database service by Amazon Web services(AWS).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.

What is minikube?

Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day.

What is kubernetes?

Kubernetes is an open-source container-orchestration system for automating computer application deployment, scaling, and management.

Pre-requisites:-

  1. AWS
  2. Terraform
  3. Kubernetes
  4. installation of minikube

STEP-BY-STEP IMPLEMENTATION:-

STEP 1:-

start the minikube from the windows CLI ,using a command,

minikube start --driver=virtualbox

Check if kubernetes master is running or not,

Also,you might see the minikube running in your VM,

Minikube in VM

STEP 2:-

For using RDS service ,we need VPC and two subnets running in different AZ,only if you are not using the default VPC.

As I am not using here default VPC,so I will create VPC ,two subnets in different AZ and an internet gateway.

So to start doing changes in AWS,I need to give a provider

 provider "kubernetes" {} provider "aws" {
profile = "arifiya"
region = "ap-south-1"
}

Now,creating VPC and two subnets

After this I will create an internet gateway

#Internet gateway resource "aws_internet_gateway" "Natgw"{
vpc_id = "${aws_vpc.vpc.id}"

tags = {
Name = "Internet_Gateway"
}
}

To run the code,we need to initialize it.For which I am using

terrform init

terrfaorm init

And then,to run the code

terraform apply

So now my vpc,subnets and internet gateway has been created.I am checking it through GUI.

Next Step I will do is giving, AWS and subnet data source

#vpc data sourcedata "aws_vpc" "vpc" {}# Subnet data sourcedata "aws_subnet_ids" "vpc_sub" {vpc_id = data.aws_vpc.vpc.id}

STEP 3:-

In this step I will provide Security Groups that allow data in database.

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic.

resource “aws_security_group” “Data_in_db” {name = “allow_db”description = “Allow Wordpress to put data in DB”vpc_id = data.aws_vpc.vpc.idingress {description = “MySQL”from_port = 3306to_port = 3306protocol = “tcp”cidr_blocks = [“0.0.0.0/0”]}egress {from_port = 0to_port = 0protocol = “-1”cidr_blocks = [“0.0.0.0/0”]}tags = {Name = “wp_data_in_db”}
}

A DB subnet group is a collection of subnets (typically private) that you create for a VPC and that you then designate for your DB instances. A DB subnet group allows you to specify a particular VPC when you create DB instances using the CLI or API.

# subnet group for DBresource “aws_db_subnet_group” “sub_ids” {name = “main”subnet_ids = data.aws_subnet_ids.vpc_sub.idstags = {Name = “Db_subnet_group”}

DB instance:-

A DB instance is an isolated database environment running in the cloud. It is the basic building block of Amazon RDS.

Now I will make a DB instance ,For this I will use the below code.

Checking DB instance through GUI,

terraform apply

Kubernetes Deployment:-

Deployments represent a set of multiple, identical Pods with no unique identities. A Deployment runs multiple replicas of your application and automatically replaces any instances that fail or become unresponsive. Deployments are managed by the Kubernetes Deployment controller.

So Now for kubernetes deployment

#deploymentresource “kubernetes_deployment” “wp_deploy” depends_on = [aws_db_instance.rds_wp]metadata {name = “wordpress”labels = {app = “wordpress”}}spec {selector {match_labels = {app = “wordpress”}}template {metadata {labels = {app = “wordpress”}}spec {container {image = “wordpress”name = “wordpress-pod”env {name = “WORDPRESS_DB_HOST”value = aws_db_instance.rds_wp.endpoint}env {name = “WORDPRESS_DB_DATABASE”value = aws_db_instance.rds_wp.name}env {name = “WORDPRESS_DB_USER”value = aws_db_instance.rds_wp.username}env {name = “WORDPRESS_DB_PASSWORD”value = aws_db_instance.rds_wp.password}port {
container_port = 80
}
} }
}
}
}

After running the code

STEP 4:-

creating service:-

Here I have created service for Wordpress so that we can access Wordpress publically

Now I am creating a service

#creating serviceresource “kubernetes_service” “wp_service” {depends_on = [
kubernetes_deployment.wp_deploy,
]
metadata {
name = “wp-service”
}
spec {
selector = {
app = “wordpress”
}
port {
port = 80
target_port = 80
node_port = 31002
}type = “NodePort”
}
}

Checking through some of the kubectl commands

So now my service has been created.

STEP 5:-

Starting the service and opening wordpress on browser

# open on chromeresource “null_resource” “openwebsite” {depends_on = [kubernetes_service.wp_service]provisioner “local-exec” {command = “minikube service ${kubernetes_service.wp_service.metadata[0].name}”}}

After running this code ,I will get the URL

On running that url in the browser,I can open wordpress

This url will open the wordpress on my chrome browser

Wordpress

Finally,The wordpress is launched.And with this the task is completed.

Thank you,for reading my blog!!

Hope,This would have helped you.

For any query,Feel free to reach out to me😊

--

--