Deployment of WordPress application on Kubernetes and AWS RDS using terraform

Lakshyasinghvi
5 min readJun 14, 2021

What is Cloud Automation?

Cloud automation is a broad term that refers to the processes and tools an organization uses to reduce the manual efforts associated with provisioning and managing cloud computing workloads. IT teams can apply cloud automation to private, public and hybrid cloud environments. Cloud automation enables IT teams and developers to create, modify, and tear down resources on the cloud automatically.

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. The infrastructure Terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

Agenda

To built a highly managed, autoscaled, and autodeployed web app that Fargate pods should run in a private subnet, communicating with the outside world via a load balancer placed in the public subnet. Behind the scenes, all of the data is stored in a database inside a private lab accessible only by admin.

What is Amazon RDS ?

Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud. It provides cost-efficient and resizable capacity while automating time-consuming administration tasks such as hardware provisioning, database setup, patching and backups. It frees you to focus on your applications so you can give them the fast performance, high availability, security and compatibility they need.

Amazon RDS is available on several database instance types — optimized for memory, performance or I/O — and provides you with six familiar database engines to choose from, including Amazon Aurora, PostgreSQL, MySQL, MariaDB, Oracle Database, and SQL Server. You can use the AWS Database Migration Service to easily migrate or replicate your existing databases to Amazon RDS.

What is Kubernetes ?

Kubernetes is a portable, extensible, open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available.

The name Kubernetes originates from Greek, meaning helmsman or pilot. Google open-sourced the Kubernetes project in 2014. Kubernetes combines over 15 years of Google’s experience running production workloads at scale with best-of-breed ideas and practices from the community.

What is WordPress ?

WordPress allows to build a website that meets your unique needs. Start a blog, business site, portfolio, online store, or anything else you can imagine. With built-in optimization and responsive, mobile-ready themes, there’s no limit to who you can reach with your new website.

Task Description (Task-6) :

Deploy the Wordpress application on Kubernetes and AWS using terraform.

Steps to Follow :

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.

Software Requirements :

  1. AWS CLIv2 installed and configured with IAM user.
  2. Minikube and Terraform installed.
  3. Kubectl would be configured.

Proceed to Code :

  1. Declaring our cloud provider and giving our account details so that Terraform can access our AWS account. We will also provide the region where we want to work and the version of AWS. We also declare other providers that will be useful in the code.
provider "aws" {
region = "ap-south-1"
profile = "lakshya"
version = "~> 2.70"
}
provider "local" {
version = "~> 1.4"
}
provider "tls" {
version = "~> 2.1"
}
provider "null" {
version = "~> 2.1"
}
provider "kubernetes" {
config_context_cluster = "minikube"
}

2. Create a Security group for RDS.

resource "aws_security_group" "mysql-rds" {
name = "RDS-Security-Group"
description = "MySQL Ports"

ingress {
description = "Mysql RDS"
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 = "Mysql-RDS"
}
}

3. Create a RDS.

resource "aws_db_instance" "default" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "myrdsdb"
username = "lakshya"
password = "hmctask6password"
parameter_group_name = "default.mysql5.7"
publicly_accessible = true
skip_final_snapshot = true
vpc_security_group_ids = [aws_security_group.mysql-rds.id]
tags = {
name = "RDS"
}
depends_on = [
aws_security_group.mysql-rds,
]
}

4. Create Deployment of Wordpress.

resource "kubernetes_deployment" "wp" {
depends_on = [
aws_db_instance.default,
]
metadata {
name = "wordpress"
labels = {
app = "wordpress"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "wordpress"
}
}
template {
metadata {
labels = {
app = "wordpress"
}
}
spec {
container {
image = "wordpress"
name = "wp"
}
}
}
}
}

5. Creating Nodeport Service for Wordpress Deployment.

resource "kubernetes_service" "wp-expose"{
depends_on = [
kubernetes_deployment.wp,
]
metadata {
name = "wp-expose"
}
spec {
selector = {
app = kubernetes_deployment.wp.metadata.0.labels.app
}
port {
node_port = 30001
port = 80
target_port = 80
}
type = "NodePort"
}
}

Commands to run the code :

On the terminal, just run the following commands -

# To initialize the plugins
Terraform init
# To validate the configuration file in the directory
Terraform validate
# To create the infrastructure
Terraform apply -auto-approve
#To destroy the infrastructure
Terraform destroy -auto-approve

Output :

--

--