AWS WORDPRESS AND MYSQL THROUGH TERRAFORM

Rishabh Jain
7 min readAug 29, 2020

--

Hey! technology seekers here is something that you might never want to miss. A WordPress and MySQL is going to be launched in only going to be launched in just one click. It will be so secured as will be connected to VPC (Virtual Private Cloud), Private Subnet with MySQL , Public Subnet with WordPress Site, Route Table and Internet Gateway and moreover all the stuff and services mentioned above will be launched without accessing Web UI of AWS i.e. with Terraform.

First let me introduce you with some important Terminologies and then we will jump to create this awesome automated system.

  1. VPC => It is a logically isolated section of the AWS Cloud where you can launch AWS resources in a virtual network that you define. You have complete control over your virtual networking environment, including selection of your own IP address range, creation of subnets, and configuration of route tables and network gateways. You can use both IPv4 and IPv6 in your VPC for secure and easy access to resources and applications.
  2. Security Group => A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic. Inbound rules control the incoming traffic to your instance, and outbound rules control the outgoing traffic from your instance. If you don’t specify a security group, instance in cloud uses the default security group.
  3. Subnets => It is a logical subdivision of an IP network. The practice of dividing a network into two or more networks is called subnetting. AWS provides two types of subnetting one is Public which allow the internet to access the machine and another is private which is hidden from the internet.
  4. Internet Gateway => An internet gateway is a horizontally scaled, redundant, and highly available VPC component that allows communication between your VPC and the internet.An internet gateway serves two purposes: to provide a target in your VPC route tables for internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IPv4 addresses.
  5. Terraform => Terraform is an open-source infrastructure as code software tool created by HashiCorp. Users define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL), or optionally JSON. Basically it is used to write the code and as an output the coded stuff output is made inside the cloud itself.
  6. Routing Table =>A routing table is a set of rules, often viewed in table format, that is used to determine where data packets traveling over an Internet Protocol (IP) network will be directed. All IP-enabled devices, including routers and switches, use routing tables.A routing table contains the information necessary to forward a packet along the best path toward its destination.

If you are very new with AWS and its services please refer to this blog first. It is from scratch and will help you understand concepts and services in more better way.

Now let us jump to make this automated system. I will be providing the github link of my repository in which there will be the code for all the services launched.

STEP-1 Login to your Profile through CLI as shown below.

STEP-2 Let us create the VPC in the cloud through Terraform.

Terraform Code for VPC :

After writing the code save the file with extension .tf

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your VPC will be created in the AWS Cloud as shown below.

VPC CREATED

STEP-3 Now let us move to create the Subnets for the instances we will launching at the last(Public for WordPress and Private for MySQL).

map_public_ip_on_launch = “true” this actually tells whether the subnet is Public or not If it is true then Subnet is Public otherwise Private. The default value of this property is false.

Code For Public Subnet:

Code For Private Subnet:

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your two subnets will be created in the Cloud aand connceted to thr VPC as shown below:

SUBNETS CREATED

STEP-4 Now let us create Internet Gateway for the setup.

Code for Internet Gateway :

CODE FOR INTERNET GATEWAY

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your Internet Gateway will be created and connected to VPC as shown below.

INTERNET GATEWAY CREATED

STEP-5 Let us now make a Route Table so that route can be decided for the Instance.

CODE FOR ROUTE TABLE :

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

Your Route Table will be created and connected to Subnet (Public) , VPC and Internet Gateway.

ROUTE TABLE CREATED

STEP-6 Now let us go and create the Security Group for the Instances we are going to launch after this. In the Security Group we are allowing SSH, TCP Protocol and Port 3306 for MYSQL.

CODE FOR SECURITY GROUP :

SECURITY GROUP CODE PART-1
SECURITY GROUP CODE PART-2

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

You will see that a Security Group will be created and also connected with VPC with the required permissions as shown below.

SECURITY GROUP CREATED

STEP-7 Let us make a key which can be used to access the instance for further processes.

CODE FOR KEY :

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

STEP-8 Now let us move to create our instance which is major step. Just remember join the Public Subnet with WordPress Instance and Private Subnet with MySQL Instance which will be shown below.(Joining is done by giving the respective Subnet ID respectively).

CODE FOR INSTANCES :

Now go to the command prompt and write the following:

  1. terraform init
  2. terraform validate
  3. terraform apply or terraform apply -auto-approve

as shown below.

TERRAFORM INIT
TERRAFORM VALIDATE
TERRAFORM APPLY

When it will be completed two Instances will be launched in the cloud and the WORDPRESS Site will be opened up automatically in the browser as shown below.

INSTANCES RUNNING
WORDPRESS SITE

Here is the GitHub Repository link which consist of all the Terraform Code used above to create the services.

You can use the command to destroy everything i.e.

terraform destroy or terraform destroy -auto-approve

Thanks a lot!! Hope you enjoyed the System.

--

--

Rishabh Jain
Rishabh Jain

Written by Rishabh Jain

I am a tech enthusiast, researcher and an integration seeker. I love to explore and learn about the right technology and right concepts from its foundation.

No responses yet