Yet Another WordPress…

Task : Deploy the WordPress application on Kubernetes and AWS using Terraform

Piyush Mehta
4 min readOct 9, 2020

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.

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

First we’ll deploy WordPress on our local Kubernetes Cluster ( Minikube ):

provider "kubernetes" {config_context_cluster = "minikube"}resource "null_resource" "start_minikube"{provisioner "local-exec" {command = "minikube start"}}resource "kubernetes_deployment" "wordpress" {metadata {name= "wordpress"}spec {replicas = 1selector {match_labels = {env      = "frontend"region   = "IN"app      = "wordpress-kube"}match_expressions {key      = "env"operator = "In"values   = ["frontend", "webserver"]}}template {metadata {labels = {env    = "frontend"region = "IN"app    = "wordpress-kube"}}spec {container {image = "wordpress:5.1.1-php7.3-apache"name  = "wordpress-kube"}}}}}resource "kubernetes_service" "kube_" {metadata {name = "service"}spec {selector = {app = kubernetes_deployment.wordpress.spec.0.template.0.metadata[0].labels.app}port {node_port   = 32123port        = 80target_port = 80}type = "NodePort"}}

Second Step will be to create a Relational Database ( Amazon RDS ):

provider "aws" {profile = "Kaizoku"region = "ap-south-1"}resource "aws_security_group" "SG_DB" {name        = "Tf_SG_MySql"description = "allows inbound port 3306"ingress {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 = "SG_DB"}}resource "aws_db_instance" "RDS" {tags = {Name = "Tf_DB"}name                   = "wordpress_db"username               = "Kaizoku"password               = "redhatpass"allocated_storage      = 25vpc_security_group_ids = [aws_security_group.SG_DB.id]storage_type           = "gp2"identifier             = "mysql"engine                 = "mysql"engine_version         = "5.7"instance_class         = "db.t2.micro"publicly_accessible    = trueport                   = 3306parameter_group_name   = "default.mysql5.7"skip_final_snapshot    = true}output "display_dns" {value = aws_db_instance.RDS.address}

Note: The above 2 code snippets are independent of each other, they can either be stored in separate files and executed separately or both can be stored in same file and executed together. I’m using a single file to execute both.

For Execution : -

  • Initialize and install the required plugins
terraform init
  • Plans
terraform plan
  • Validate ( Checks for error )
terraform validate
  • Create the Infrastructure
terraform apply --auto-approve

Now our Infrastructure is set up:

Use the above highlighted IP and Port Number to access WordPress.

http:// 192.168.99.100:32123

Note: Copy the End Point URL from RDS (you can avoid doing it by copying it from the terminal, as our code displays it as an output after the successful execution of apply command )and paste it in the Database Host Field.

Awesome !! Run the installation and our WordPress Site is ready already!

Just complete a few small steps and…

Use the above set up for as long as you want, afterwards to remove the complete set up just run

terraform destroy --auto-approve

--

--