Deploy the Wordpress application on Kubernetes and AWS using Terraform

  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.

// AWS Provider
provider “aws” {
profile = “aditya”
region = “ap-south-1”
}

// Kubernetes Provider
provider “kubernetes” {
config_context_cluster = “minikube”
}

resource “aws_vpc” “main” {
cidr_block = “10.10.0.0/16”
enable_dns_hostnames=true
enable_dns_support =true
tags = {
Name = “db_vpc”
}
}

resource “aws_subnet” “db_subnet1” {
vpc_id = aws_vpc.main.id
cidr_block = “10.10.0.0/24”
map_public_ip_on_launch = true
availability_zone = “ap-south-1a”
tags = {
Name = “subnet_db”
}
}

resource “aws_subnet” “db_subnet2” {
vpc_id = aws_vpc.main.id
cidr_block = “10.10.1.0/24”
map_public_ip_on_launch = true
availability_zone = “ap-south-1b”
tags = {
Name = “subnet_db”
}
}

resource “aws_internet_gateway” “gw” {
vpc_id = aws_vpc.main.id

tags = {
Name = “mygw1”
}
}

resource “aws_route_table” “r” {
vpc_id = aws_vpc.main.id

route {
cidr_block = “0.0.0.0/0”
gateway_id = aws_internet_gateway.gw.id
}

tags = {
Name = “route1”
}
}

resource “aws_route_table_association” “a” {
subnet_id = aws_subnet.db_subnet1.id
route_table_id = aws_route_table.r.id
}

resource “aws_route_table_association” “b” {
subnet_id = aws_subnet.db_subnet2.id
route_table_id = aws_route_table.r.id
}

resource “aws_db_subnet_group” “sub_ids” {
name = “main”
subnet_ids = [ “${aws_subnet.db_subnet1.id}”, “${aws_subnet.db_subnet2.id}” ]

tags = {
Name = “DB subnet group”
}
}

resource “aws_security_group” “db_sg” {
name = “allow_db”
description = “Allow WP to put data in DB”
vpc_id = aws_vpc.main.id

ingress {
description = “MySQL”
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 = “db-security”
}
}

resource “aws_db_instance” “rdsWP” {
engine = “mysql”
engine_version = “5.7”
identifier = “wordpress-db”
username = “aditya”
password = “redhat123”
instance_class = “db.t2.micro”
storage_type = “gp2”
allocated_storage = 20
db_subnet_group_name = aws_db_subnet_group.sub_ids.id
vpc_security_group_ids = [aws_security_group.db_sg.id]
publicly_accessible = true
name = “wpdb”
parameter_group_name = “default.mysql5.7”
skip_final_snapshot = true
}

resource “kubernetes_deployment” “wpDeploy” {
depends_on = [
aws_db_instance.rdsWP
]
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.rdsWP.endpoint
}
env {
name = “WORDPRESS_DB_DATABASE”
value = aws_db_instance.rdsWP.name
}
env {
name = “WORDPRESS_DB_USER”
value = aws_db_instance.rdsWP.username
}
env {
name = “WORDPRESS_DB_PASSWORD”
value = aws_db_instance.rdsWP.password
}
port {
container_port = 80
}
}
}
}
}
}

resource “kubernetes_service” “wpservice” {
depends_on = [
kubernetes_deployment.wpDeploy,
]
metadata {
name = “wp-service”
}
spec {
selector = {
app = “wordpress”
}
port {
port = 80
target_port = 80
node_port = 31002
}

type = “NodePort”
}
}

Now We have to first initialize a working directory with terraform init command.

terraform init

Now we will run the terraform code:

terraform apply -auto-approve

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aditya Raj

Aditya Raj

I'm passionate learner diving into the concepts of computing 💻