Creating a InfraStructure using our own VPC with public, private subnets and Nat gateway

Mohit Verma
5 min readJul 18, 2020

It is basically an upgraded version of my previous article. I just implemented the same with a NAT Gateway.

Task:

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

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

(i). Public subnet [ Accessible for Public World! ]

(ii). 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. Create a NAT gateway for connect our VPC/Network to the internet world and attach this gateway to our VPC in the public network

6. Update the routing table of the private subnet, so that to access the internet it uses the nat gateway created in the public subnet

7. Launch an ec2 instance which has Wordpress setup already having the security group allowing port 80 sothat our client can connect to our wordpress site. Also attach the key to instance for further login into it.

8. 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.

also add auto ip assign and auto dns name assignment option to be enabled.

Steps:

Step 1: Configuring Profile and Provider

Step 2: Creating a new VPC

Step 3: Creating a security group for Wordpress Instance

Step 4: Creating a security group for Bastion Instance

Step 5: Creating a security group for MySQL Server

Step 6: Creating Subnet 1

Step 7: Creating Subnet 2

Step 8: Creating Internet Gateway

Step 9: Creating and allocating Elastic IP

Step 10: Creating a NAT Gateway

Step 11: Creating Route Tables

Step 12: Launching WordPress Instance

Step 13: Launching Bastion Instance

Step 14: Launching MySQL Server

And Done!!

Step 15: Now to launch the whole InfraStructure save all the code into a “task.tf” file.

And Use following commands to Launch the InfraStructure.

# terraform init

# terraform validate

# terraform apply -auto-approve

Step 16: Verification

Now we need to transfer key into bashtion/wordpress host so that we can login to mysql server available on our local network only

Now the key has been transfered. So will now login into MySQL server using private IP

Checking Outbound connectivity for MySQL server by updating it.

Step 17: Destroying the whole Infrasrtructure

Thanks

All suggestions are welcome to make the article and code better.

Link to my Previous Article:

GITHUB REPO :

Below you can find the complete code -

Connect with me on LinkedIn:

--

--

Mohit Verma
0 Followers

Hybrid Multi CLoud | Amazon EKS | Flutter