Integrating MySQL and WordPress to AWS instance using Terraform

Hello, readers. Today we will be seeing the integration of MySQL and Wordpress to an AWS Instance.

Terraform and AWS

Terraform is a tool for building, changing, versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. The infrastructure terraform can manage includes low-level components such as compute instances, storage, and networking, as well as high-level components such as DNS entries, SaaS features, etc.

Amazon Web Services (AWS) is a subsidiary of Amazon that provides on-demand cloud computing platforms and APIs to individuals, companies, and governments, on a metered pay-as-you-go basis. In Aggregate, these cloud computing web services provide a set of primitive abstract technical infrastructure and distributed computing building blocks and tools.

The task at hand:

1. Create an AWS EC2 instance.

2. Configure the instance with Apache Webserver. Download PHP application name ""WordPress"". WordPress stores data at the backend in the MySQL Database Server. Therefore, you need to set up a MySQL server using AWS RDS service using Free Tier.

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

To solve the above task, we take the following steps,

Step 1: Creating VPC using Terraform 

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

//creating the vpc
resource "aws_vpc" "main" {
  cidr_block = "192.168.0.0/16"
  instance_tenancy = "default"
  tags = {
Name = "task3-vpc"
}
}

Now, by doing terraform init in the terminal, the command will install a required plugin for AWS. And then by executing terraform apply command, the VPC will be created in AWS.


Step 2: Creating two subnets in the VPC already created earlier

//creating subnet-1
resource "aws_subnet" "main" {
  vpc_id = "vpc-565b9c3d"
  availability_zone = "ap-south-1a"
  cidr_block = "192.168.0.0/24"

  tags = {
    Name = "subnet-1"
  }
}

//creating subnet-2
resource "aws_subnet" "main1" {
  vpc_id = "vpc-565b9c3d"
  availability_zone = "ap-south-1a"
  cidr_block = "192.168.0.0/24"

  tags = {
    Name = "subnet-2"
  }
}

Thus, when the above code is executed, two subnets will be created within the VPC



Step 3: Creating the Internet Gateway which will give VPC connectivity to the outside world (Internet Gateway)

resource "aws_internet_gateway" "gw1" {
  vpc_id = "vpc-565b9c3d"

  tags = {
    Name = "my-router"
  }
}

When we execute the above code, the Internet Gateway gets created and gets attached to the VPC.


Step 4: Creating Routing Table for the Gateway

Even though the Gateway has been created, it will still not have connectivity to the internet just yet. So for making a path for the Internet Gateway for going to the outside world, we need to create a routing table.

resource "aws_route_table" "r" {
  vpc_id = "vpc-565b9c3d"
  route {
      cidr_block = "0.0.0.0/0"
      gateway_id = "igw-6acc5c02"
  }
  tags = {
    Name = "my-routing-table"
  }
}

We want that Subnet-1 should have connectivity with Internet World so now we will be creating Subnet Association for Subnet-1

//creating association for subnet-1
resource "aws_route_table_association" "a" {
  subnet_id = "subnet-ed6538a1"
  route_table_id = "rtb-34f6725f"
} 


Step 5: Integrating WordPress with MySQL

The next step is to create security groups for WordPress and MySQL. WordPress will be launched with Subnet-1 due to our earlier set up which has connectivity for the Internet.

MySQL will be launched in Subnet-2 which has no Internet Connectivity.

Creating the security group for WordPress.

//Creating Security-Group for Word-Press. 
resource "aws_security_group" "SG1" {
  name        = "Word-Press-SG"
  description = "Allow SSH,HTTP"
  vpc_id      = "vpc-565b9c3d"


  ingress {
    description = "SSH"
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    description = "HTTP"
    from_port   = 80
    to_port     = 80
    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 = "My-SecGrp"
  }
}

Creating Security Group for MySQL.

/Creating Security-Group for MySQL allowing port 3306.description
resource "aws_security_group" "SG2" {
  name        = "MySQL-SG"
  description = "Allow port 3306"
  vpc_id      = "vpc-565b9c3d"


  ingress {
    description = "MySQL-port"
    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-SecGrp"
  }
}



Step 6: Launching EC2 instance with WordPress already set up, followed by launching MySQL instance in the private subnet

EC2 instance for WordPress.

//Launching an Instance which has WordPress already setup. 
resource aws_instance "myin1" {
  ami           = "ami-0979674e4a8c6ea0c"
  instance_type = "t2.micro"
  key_name      = "faiz-key"
  security_groups = "${aws_security_group.SG1.id}"]
  subnet_id = "subnet-ed6538a1"
  


  tags = {
    Name = "WordPress-OS"
  }
}

MySQL instance in the private subnet.

resource aws_instance "myin" {
  ami           = "ami-76166b19"
  instance_type = "t2.micro"
  key_name      = "faiz-key"
  security_groups = "${aws_security_group.SG2.id}"]
  subnet_id = "subnet-cb3ed2a0"
  


  tags = {
    Name = "MySQL-OS"
  }
}

Thus both the O.S. will be launched.


Output: Now using the Public DNS of WordPress to launch the website


Thank You for viewing the post!

Comments

Popular posts from this blog

How Unilever Benefitted by Transforming into Cloud-Based Digital-Market Empire

Creating custom Terraform module - creating submodule for s3 bucket resource from the AWS provider.