Deploy RDS(AWS) +Wordpress(Minikube) using Terraform

Ritesh Singh
4 min readOct 14, 2020

Problem Statement:

  • Write an Infrastructure as code using Terraform, which automatically deploy the Wordpress application
  • On AWS, use RDS service for the relational database for Wordpress application.
  • Deploy the Wordpress as a container on top of Minikube.
  • The Wordpress application should be accessible from the public world, expose it.

Prerequisites:

  • AWS CLIv2 installed and configured with IAM user.
  • Minikube, Terraform installed.
  • kubectl configured.

Step-1)Create a Security group for RDS:

  • Code:
resource "aws_security_group" "mysql-rds" {name        = "RDS-Security-Group"
description = "MySQL Ports"

ingress {
description = "Mysql RDS"
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 = "Mysql-RDS"
}
}
  • Use resource aws_security_group for making the Security group, and in this SGs add one rule for MySQL port(3306) number.

Step-2)Launch RDS:

  • Code:
resource "aws_db_instance" "default" {
allocated_storage = 20
storage_type = "gp2"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t2.micro"
name = "mydb"
username = "user1"
password = "password"
parameter_group_name = "default.mysql5.7"
publicly_accessible = true
skip_final_snapshot = true
vpc_security_group_ids = [aws_security_group.mysql-rds.id]
tags = {
name = "RDS"
}
}
  • Use resource aws_db_instance for creating RDS, and pass the id of SGs in vpc_security_group_ids
  • Use publicly_accessible so that we are able to access it.

Step-3)Create Deployment of Wordpress:

  • Code
provider "kubernetes" {
config_context_cluster = "minikube"
}
resource "kubernetes_deployment" "wp" {
metadata {
name = "wordpress"
labels = {
app = "wordpress"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "wordpress"
}
}
template {
metadata {
labels = {
app = "wordpress"
}
}
spec {
container {
image = "wordpress"
name = "wp"
}
}
}
}
}
  • In this code, we have to select the provider as Kubernetes because we are going to create a deployment in k8s.
  • Use resource kubernetes_deployment for making deployment.
  • Now provide the metadata, template, selector, and image name(Wordpress).

Step-4)Creating Nodeport Service for Wordpress Deployment:

  • Code:
resource "kubernetes_service" "wp-expose"{
depends_on = [kubernetes_deployment.wp]
metadata {
name = "wp-expose"
}
spec {
selector = {
app = kubernetes_deployment.wp.metadata.0.labels.app
}
port {
node_port = 30001
port = 80
target_port = 80
}
type = "NodePort"
}
}
  • Use resource kubernetes_service for making services of deployment.
  • Now use type as Nodeport to expose the Wordpress.

Command to run code:

terraform init
  • init for Initialization terraform plugins.
terraform apply --auto-approve
  • apply to run the terraform code.

Output

RDS
wordpress

Wordpress Setup:

Select language
  • Now you need to provide the DB, user, password, and for Database Host use RDS Endpoint.
Run the Installation
Fill the information
Log in via username and password
Wordpress Dashboard
Blog

--

--