Create a web portal on AWS through terraform also provide internet access to private subnet

Problem Statement

We have to create a web portal for our company with all the security as much as possible. 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 also add an additional feature to be added that is NAT Gateway to provide the internet access to instances running in the private subnet.

So here are the steps that we need to follow :

1. Write an 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. 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. Don’t forgot to add auto ip assign and auto dns name assignment option to be enabled.

Bastion Host ?

AWS Bastion Host is a server whose purpose is to provide access to a private network from an external network, such as the Internet. Because of its exposure to potential attack, a bastion host must minimize the chances of penetration.

Terraform Code:

1. We are using aws as provider so we need to setup region and profile.

2. Create the key pair.

3. Create the VPC.

4. Create two subnets.

5. Create the Internet Gateway

6. Create the route table.

7. Connect the route table to subnet.

8. Create Security group for instance.

9. Launch instances.

Now We have to first initialize a working directory with terraform init command.

Now we will run the terraform code:

Here is the final output:

Thank You !!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store