Automation through Terraform to create VPCs & host a Wordpress Site through VPCs & Subnets(TASK 3)

Automation through Terraform to create VPCs & host a Wordpress Site through VPCs & Subnets(TASK 3)

Amazon Virtual Private Cloud (Amazon VPC) lets you provision 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. 

Before moving on to the task , I want to explain you some concepts of networking needed to connect to any server.

There are some pre-requisites before connecting to any server in networking :

1) Wire/Wifi

2) Network Card (NIC)/ LAN

3) Router & Switch

4) Routing Table for further connections

=> Whenever the NetMask of two servers are same , then we use a switch. It is a  networking hardware that connects devices on a computer network by using packet switching to receive and forward data to the destination device.

=> Whenever the NetMask of two servers are different , then we use a router. Router is a networking device that forwards data packets between computer networks .Routers perform the traffic directing functions on the Internet.

=> Router and Switch can be created using a software known as Bridge. If a device is a layer 3 device then it should know the IP address of the server.Router is also an example of layer 3 device of OSI model.

NOW LET'S MOVE TO OUR TASK !!

OBJECTIVES :

1) Write a Infrastructure as code using terraform, which automatically create a VPC.

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

  a) public subnet [ Accessible for Public World! ] 

  b) 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.

Let's do this . I will be explaining each & every part during creation of each resource.

First we have to configure aws :-

No alt text provided for this image


Create a folder in that folder create a file with .tf extension and in that file we will write all our code.Then we have to initialize terraform using "terraform init" command.

Now let's create a vpc with enabled dns hostnames for our task .

No alt text provided for this image


=> According to the task , we have to create two subnets : 1) one is public subnet which will be accessible to the public world & where our client can connect to the wordpress site.2) other will be the private subnet which will be restricted to the public world & where our mysql instance will be running.

( Subnet is nothing but a lab where our whole networking configuration is done. Whenever we create a subnet , internally a switch is created. Dynamically a network card is attached to the OS whenever the OS launch. Subnets also provide us with the DHCP which will provide the unique IP to the app & server.)

Terraform code of public subnet for wordpress instance:-

No alt text provided for this image


Terraform code of private subnet for mysql instance:-

No alt text provided for this image


Now we create a public facing Internet Gateway.

( Internet Gateway is just like a router which contains one rule for DNATING in router. Internet gateway is always belongs to VPC and there is only one internet gateway present in one VPC. If internet gateway is not there in the instance , the instance will not have any publlic IP & therefore NATING cannot be done.)

Terraform code for creating Internet Gateway :-

No alt text provided for this image


Moving forward to create Routing table.

( All the OS which belong to the network go to the internet , should have a routing table. Routing table gives more information to the client that through which dns hostnames the client can connect.)

Terraform code for the Routing table :-

No alt text provided for this image


Now we create a Security Group with ssh and http port enabled for wordpress instance.

(Security group you all know guys that 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. When you launch an instance, you can specify one or more security groups.)

Terraform code for wordpress instance with ssh and http port enabled :-


No alt text provided for this image


Terraform code for mysql instance with Mysql Aurora port enabled:-

No alt text provided for this image


Then launching the wordpress and mysql instances with security groups & vpc's attached.Here in my case i have created one instance name "Wordpress" in a public subnet that has an internet facing accessibility.

Terraform code for wordpress instance :-

No alt text provided for this image


Terraform code for mysql instance :-

No alt text provided for this image


SO OUR WHOLE INFRASTRUCTURE IS DONE USING TERRAFORM !!

Now we just have to run a below command to setup this whole infrastructure through terraform :-

terraform apply

After it is done we can see the following outputs on AWS

OUR VPC IS CREATED .

No alt text provided for this image
No alt text provided for this image
No alt text provided for this image


 Successfully created two subnets - a public subnet and a private subnet.

No alt text provided for this image


ROUTING TABLE IS CREATED !!

No alt text provided for this image


Our Internet Gateway is created.

No alt text provided for this image


Security Group for wordpress & mysql is created.

No alt text provided for this image


You can see here our two Instances are running & the security groups , VPC , subnet are already attached to them .

No alt text provided for this image


Now anyone who know this IP can connect to the wordpress site on internet.

That's all After successful launch you will see the wordpress setup page.

No alt text provided for this image


for destroying the complete infrastructure we have to write only one code :-

No alt text provided for this image


terraform destroy


THANKS FOR READING :-)

To view or add a comment, sign in

More articles by Aryan Bansal

Insights from the community

Others also viewed

Explore topics