Launching WordPress on Kubernetes and Database in AWS RDS using Terraform

Finally, I completed task 6 of Hybrid Multi Cloud given by Vimal Daga Sir .

Kubernetes :

RDS Database :

TASK :

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.

Try each step first manually and write Terraform code for the same to have a proper understanding of workflow of task.

STEPS :

Run command in cmd :

minikube start

2. Write an Infrastructure as code using terraform which automatically deploy the Wordpress Application on top of Kubernetes (Minikube) .

provider "kubernetes" {
config_context = "minikube"
}
resource "kubernetes_deployment" "wps" {
metadata {
name = "wordpress"
}
spec {
replicas = 3
selector {
match_labels = {
"env" = "development"
"loc" = "IN"
"site" = "Wordpress"
}
}
template {
metadata {
labels = {
"env" = "development"
"loc" = "IN"
"site" = "Wordpress"
}
}
spec {
container {
image = "wordpress"
name = "wp"
}
}
}
}
}
resource "kubernetes_service" "example" {
metadata {
name = "terraform-example"
}
spec {
selector = {
site = kubernetes_deployment.wps.spec[0].template[0].metadata[0].labels.site
}
port {
node_port = 30001
port = 8080
target_port = 80
}
type = "NodePort"
}
}

Firstly initialize the terraform to install plugins :

Now apply this code :

So we can see launched pods :

3. Now write a terraform code which automatically create a MySQL database using RDS of AWS :

to login in aws account :

provider "aws" {
region = "ap-south-1"
profile = "profile-name"
}

to launch database instance :

resource "aws_db_instance" "new"{
engine = "mysql"
engine_version = "5.7.30"
name = "mydb"
username = "root"
password = "redhat123"
instance_class = "db.t2.micro"
allocated_storage = 10
storage_type = "gp2"
port = 3306
auto_minor_version_upgrade = true
publicly_accessible = true
parameter_group_name = "default.mysql5.7"
skip_final_snapshot= true
}

to see output :

output "db"{
value = aws_db_instance.new.name
}
output "username"{
value = aws_db_instance.new.username
}
output "password"{
value = aws_db_instance.new.password
}
output "RDS" {
value = aws_db_instance.new.endpoint
}

to install plugins and check code is valid or not , use these commands :

terraform init

terraform validate

Now , to apply the code :

Output :

Wordpress :

4. To destroy whole infrastructure :

Finally , Task is successfully completed !!

Thanks !