Deploy the Wordpress application on Kubernetes and AWS using Terraform

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

Terraform Code

1. Give provider details for aws and kubernetes

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

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

2. Create the VPC

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

3. Create the subnetes

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”
}
}

4. Create the Internet Gateway

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

tags = {
Name = “mygw1”
}
}

5. Create the Route Table

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”
}
}

6. Connect Route Table to subnets

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
}

7. Create the Subnet Group for DB

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”
}
}

8. Create Security Group for DB

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”
}
}

9. Launch the Database instance

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
}

10. Launch the wordpress deployment

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

11. Create service for wordpress

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

Now let see the output :

VPC, Subnet and Internet Gateway and Route Table

AWS RDS

Wordpress Site

Thank You !!

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Get on my level: How to get your computer and gemfile on the same Ruby version

A new way to reduce JSON API output without all the hassles of graphql | Restmap

Golang — CDC using AWS lambda and Aurora

How to use “Golf” debugging

Design Patterns: Singleton

Python vs JavaScript which one is better?

Producing and Consuming SOAP Webservice with Spring Boot — Part V

wsdl endpoint

An Introduction to Distributed SQL: Glossary of Terms — The Distributed SQL Blog

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 💻

More from Medium

Automated Static Site Deployment in AWS Using Terraform

How-to setup a HA/DR database in AWS? [3 — Simple database]

VITE : Website using AWS, Terraform and CircleCI

Terraform AWS Provider — Default Tags