In this article we are going to discuss how to create vpc , subnet , Internet gateway , routing table and launch Wordpress and MySQL instances on the top of public cloud aws using Terraform.

Pre-Requisites

  • Terraform should be installed on the Local VM.

Amazon Resources Created Using Terraform

So, we use Wordpress software with dedicated database server.

Database should not be accessible from the outside world for security purposes.

We only need to public the WordPress to clients.

So here are the steps for proper understanding!

Steps:

2) In that VPC we have to create 2 subnets:

  • Public subnet [ Accessible for Public World! ]
  • Private subnet [ Restricted for Public World! ]

3) Create a public facing internet gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC.

4) Create a routing table for Internet gateway so that instance can connect to outside world, update and associate it with public subnet.

5) Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 so that our client can connect to our WordPress site.

Also attach the key to instance for further login into it.

6) Launch an ec2 instance which has MYSQL setup already with security group allowing port 3306 in private subnet so that our wordpress vm can connect with the same.

Also attach the key with the same.

Note: Wordpress instance has to be part of public subnet so that our client can connect our site.

MySQL instance has to be part of private subnet so that outside world can’t connect to it.

Don’t forgot to add auto ip assign and auto dns name assignment option to be enabled.

AWS VPC

Time to Work fellas

Step-1 — Create VPC

Below Terraform code is to create aws VPC in which

cidr_block — is, When we create a VPC, you must specify a range of IPv4 addresses for the VPC in the form of a Classless Inter-Domain Routing (CIDR) block; for example, 10.0. 0.0/16.

Step-2-a — Create public subnet

Step-2-a :- Create private subnet

now both the subnet are private, to make public one of the above subnet we need to create route table and Internet gateway for vpc and then associate to public subnet.

Step-3 — Create Internet gateway

Step-4 — Create Routing table

After creating route table associate it with the one of the subnet to make it public.

Step-5 — Create security group and launch Wordpress instances

A security group acts as a virtual firewall for your EC2 instances to control incoming and outgoing traffic.

Below is the code to launch WordPress EC2 instances.

Step-5 — Create security group and launch MySQL instances

Below code is to launch MySQL database EC2 instances.

now initialize the tera tera directory using command.

terraform init

now to run the code use

terraform apply -auto-approve

now access the WordPress using WordPress public ip .

and to connect WordPress with MySQL database we need instance id of MySQL .

This is the final web page of WordPress .

and good practice to check things manually. Whether our terraform code has configured everything correctly or not .

VPC

Public Subnet

Private Subnet

Internet Gateway

Routing Table

Security Group

Everything looks great !! And here comes a pro tip —

“ You can also take this same setup to another level, much more secured”

Read this for detailed instruction —

All the codes discussed earlier can be found on my Github Repo & connect with me on Linkedin !!

Thank you Everyone for reading .!! Bella Ciao