Deploying WordPress on Kubernetes and AWS RDS using Terraform

Gaurav Gupta
4 min readSep 8, 2020

Task Description:

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 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.

Prerequisites:

  • Need one AWS account.
  • For launching Kubernetes locally, I’m using Minikube here.

I already wrote one article for the installation of Minikube in windows, check the below link…

So let’s start now:

1. Launch the WordPress App:

We are going to write the Terraform code for launching the WordPress site on the top of Kubernetes.

For connect the Terraform to Minikube, we need to write this code at first in our file…

provider “kubernetes” {
config_context_cluster = “minikube”
}

Now for launching WordPress, I’m using kubernetes deployment…

resource “kubernetes_deployment” “wordpress” {
metadata {
name = “wordpress”
labels = {
myapp = “MyWordpressSite”
}
}

spec {
replicas = 1
selector {
match_labels = {
myapp = “MyWordpressSite”
}
}

template {
metadata {
labels = {
myapp = “MyWordpressSite”
}
}

spec {
container {
image = “wordpress”
name = “wordpress-app”
}
}
}
}
}

Now, to connect this to the outside world, we need to create one service too…

resource “kubernetes_service” “wp_service”{
metadata {
name = “wp-service”
}
spec {
selector = {
myapp = “${kubernetes_deployment.wordpress.metadata.0.labels.myapp}”
}
session_affinity = “ClientIP”
port {
port = 80
target_port = 80
}
type = “NodePort”
}
}

Save this code in a file having .tf extension…

First launch minikube…

Now run these two commands…

  • terraform init
  • terraform apply

Now Let’s check that it worked or not…

This worked fine…✌

2. Launch AWS RDS service:

For connect AWS with terraform code, we need to install the AWS CLI tool first, install this and Run aws configure in CMD…

Give AWS credential there…

After that, now write your terraform code…

provider “aws” {
region = “ap-south-1”
profile = “terrauser”
}

We need to create one security group too, which allow RDS service to make connectivity… Code for this…

resource “aws_security_group” “allow_mysql” {
name = “allow_mysql”
description = “Allow MySQL Port”
ingress {
description = “allow_sql_port”
from_port = 3306
to_port = 3306
protocol = “tcp”
cidr_blocks = [ “0.0.0.0/0” ]
}
egress {
from_port = 0
to_port = 0
protocol = “-1”
cidr_blocks = [“0.0.0.0/0”]
}
tags = {
Name = “task-sgroup”
}
}

After that, we write the code for launch one RDS Database…

resource “aws_db_instance” “wp_db” {
allocated_storage = 20
storage_type = “gp2”
engine = “mysql”
engine_version = “5.7”
instance_class = “db.t2.micro”
name = “mydb”
username = “admin”
password = “gaurav123”
vpc_security_group_ids = [ aws_security_group.allow_mysql.id ]
publicly_accessible = true
iam_database_authentication_enabled = true
skip_final_snapshot = true
}

Save this code in a file having .tf extension.

Now run these two commands…

  • terraform init
  • terraform apply

Let’s see that it worked on not…

Now let’s try to connect our WordPress site with RDS…

Run minikube_ip:port in browser…

You can get IP by running this command…

This is one of the examples of Hybrid Cloud where we launch WordPress in our own premises and Database in the Public Cloud.

GitHub Link: https://github.com/gaurav-gupta-gtm/terraform-wordpress-RDS

Please clap, if you found it useful…❣

Connect me on LinkedIn for any suggestion or query…You can find some interesting articles there too…👍

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Gaurav Gupta
Gaurav Gupta

Written by Gaurav Gupta

Tech Enthusiasts, Passion to learn and share

No responses yet

Write a response