What is WordPress ?

WordPress is a free, open-source website creation platform which requires access to database to store information. Therefore we need database to make WordPress accessible.

What is Amazon RDS ?

Amazon Relational Database Service (Amazon RDS) makes it easy to set up, operate, and scale a relational database in the cloud.

  • Here we will use Amazon RDS as database for WordPress application.

Getting Started

Step 1: Create an AWS EC2 instance.

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

resource "aws_instance" "wp" {
    ami = "ami-06a0b4e3b7eb7a300"
    key_name = "task-2" 
    instance_type = "t2.micro"


    tags = {
        Name = "Wordpress_db_task3"
          }

Step 2: Configure the instance with Apache Webserver. Download PHP application name “”WordPress””. As WordPress stores data at the backend in MySQL Database server. Therefore, we need to setup a my SQL server using AWS RDS service using Free Tier.

resource "aws_instance" "wp" {
    ami = "ami-06a0b4e3b7eb7a300"
    key_name = "task-2" 
    instance_type = "t2.micro"


    tags = {
        Name = "Wordpress_db_task3"
          }


connection {
    type = "ssh"
    user = "ec2-user"
    private_key = file("C:/Users/sujag/Downloads/task-2.pem")
    host = aws_instance.wp.public_ip   
    }


provisioner "remote-exec" {
    inline = [
        "sudo yum install httpd php php-mysqlnd php-json wget -y", 
        "sudo wget https://wordpress.org/latest.tar.gz",
        "tar -xzvf latest.tar.gz",
        "sudo mv wordpress/* /var/www/html/",
        "sudo chown -R apache.apache /var/www/html",
        "sudo setenforce 0",
        "sudo systemctl start httpd"
           ]
       }
}


output "wp_public_ip" {
    value = aws_instance.wp.public_ip
}
  • Terraform file which creates EC2 instance with WordPress installed in it. Contains commands which install WordPress application.
  • Terraform file which creates RDS database instance. RDS database instance is of db.t2.micro type and contains username and password as admin and redhat123 respectively stored in the respective variables.
  • You can change it by updating username and password parameter under aws_db_instance resource.
resource "aws_db_instance" "wp_db" {
  depends_on = [
    aws_instance.wp
  ]
  allocated_storage    = 10
  identifier = "wordpress-database"
  engine               = "mysql"
  engine_version       = "5.7"
  instance_class       = "db.t2.micro"
  name                 = "wordpress"
  username             = var.db_username
  password             = var.db_pass
  parameter_group_name = "default.mysql5.7"
  publicly_accessible = true
  skip_final_snapshot  = true
}

output "Endpoint_string" {
  value = aws_db_instance.wp_db.endpoint

}

Step 3: Provide the endpoint/connection string to the WordPress application to make it work.

The outputs will be known after the whole plan finishes successfully.

Step 4: Terraform init, plan and apply

RESULTS

  • Give Database Name – wordpress (Created in RDS instance)
  • Give Username – admin (If you have not changed)
  • Give Password – redhat123 (If you have not changed)
  • Give Database Host – Endpoint of RDS instance
  • Click on Submit button.
  • Then click Run the installation button.
  • Fill the required details and click on Install WordPress button.
  • Login using credentials just created.
  • Hurray – your WordPress application is working.
  • Now your WordPress application is using Amazon RDS as a backend