Deploy the WordPress application on Kubernetes and AWS using terraform

Gyanesh Sharma
3 min readDec 20, 2020

Statement of the task: Deploy the WordPress application on Kubernetes and AWS using terraform

Steps to perform task:

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.

Tools and modules required to perform this task:

1.AWS CLI

2.Terraform

3.Kubectl

4. Minikube

Steps to perform:

Step 1: Create New Folder for this Task.( In cli.)

cd desktop
mkdir <name_of_folder_you_want>
cd <name_of_folder_you_want>

Step 2: Creating you First terraform <File_name>.tf

notepad main.tf

Step 3: Inside main.tf Create your code.

  1. Create a provider.
provider "kubernetes" {
config_context_cluster = "minikube"
}

2. Create resource.

resource "kubernetes_deployment" "wordpresspod" {
metadata {
name = "wordpress"
}

3. Create specification and templates.

spec {
replicas = 2

selector {
match_labels = {
env = "dev"
region = "IN"
App = "wordpress"
}

}

template {
metadata {
labels = {
env = "dev"
region = "IN"
App = "wordpress"

}
}

4. Create WordPress with port 80.

spec {
container {
image = "wordpress:4.8-apache"
name = "wordpressdb"
}
}
}
}
}

port {
node_port = 32123
port = 80
target_port = 80
}

type = "NodePort"
}
}

This is the last snippet here creation of terraform file is complete. Combine all these snippets to form main.tf .

Step 4: Start Kubernetes services by starting minikibe:

minikube start

Step 5: Run following command to run terraform file. (This is for initializing terraform).

terraform init

Output of the above command is:

Step 6: Command to run and apply terraform code.

terraform apply

Command to check about the creation of PODs

kubectl get pods

Step 7: Create Another Terraform file for Database.

  1. Command to create terraform file.
terraform task.tf

2. Terraform code for database.

provider "aws" {
region = "ap-south-1"
}
resource "aws_db_instance" "db" {

engine = "mysql"
engine_version = "5.7.30"
instance_class = "db.t2.micro"
allocated_storage = 10
name = "mydb"
username = "wordpress_user"
password = "rootroot"
port = "3306"
publicly_accessible = true
iam_database_authentication_enabled = true
vpc_security_group_ids = ["sg-067bf7843a162e7d9"]
tags = {
Name = "mysql"
}
}

Step 8: Command to get IP for WordPress.

minikube ip

Output of these command give you an IP, enter that IP to web browser and it will open WordPress.

Welcome page of WordPress will be open like this.

thank you

--

--