Deploying a WordPress Application on K8s Cluster with AWS RDS automation using Terraform
Deploy the Wordpress application on Kubernetes and AWS using terraform including the following steps:
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.
Amazon Relational Database Service (Amazon RDS)
It 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.
Minikube
it is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a Virtual Machine (VM) on your laptop for users looking to try out Kubernetes or develop with it day-to-day
Word_press.tf
provider "kubernetes" {}resource "kubernetes_service" "kubeservice" {
metadata {
name = "wp-service"
labels = {
app = "wordpress"
}
}
spec {
selector = {
app = "wordpress"
tier = "frontend"
}
port {
node_port = 30000
port = 80
target_port = 80
}
type = "NodePort"
}
}
resource "kubernetes_persistent_volume_claim" "mypvc" {
metadata {
name = "wp-pvc"
labels = {
app = "wordpress"
tier = "frontend"
}
}
spec {
access_modes = ["ReadWriteMany"]
resources {
requests = {
storage = "5Gi"
}
}
}
}
resource "kubernetes_deployment" "wp_deploy" {
metadata {
name = "wp_deploy"
labels = {
app = "wordpress"
tier = "frontend"
}
}
spec {
replicas = 4
selector {
match_labels = {
app = "wordpress"
tier = "frontend"
}
}
template {
metadata {
labels = {
app = "wordpress"
tier = "frontend"
}
}
spec {
volume {
name = "wordpress-persistent-storage"
persistent_volume_claim {
claim_name = kubernetes_persistent_volume_claim.mypvc.metadata.0.name
}
}
container {
image = "wordpress"
name = "wordpresscontainer"
port {
container_port = 80
}
volume_mount {
name = "wordpress-persistent-storage"
mount_path = "/var/www/html"
}
}
}
}
}}
RDS.tf
Make sure to use the following commands:
terraform init-terraform validate-terraform apply
provider "aws" {
region = "ap-south-1"
profile = "ashimanew"
}resource "aws_db_instance" "myDBInstance" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "myRDS"
username = "ashimacho"
password = "myRDS2000"
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
publicly_accessible = true vpc_security_group_ids =["${aws_security_group.allow_mysql.id}"]
apply_immediately = true
tags = {
Name = "ashimacho"
}
}
data "http" "myip" {
url = "http://ipv4.icanhazip.com"
}
resource "aws_security_group" "mysqlsg" {
name = "mysqlsg"
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = ["${chomp(data.http.myip.body)}/32"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "mysqlsg"
}
}