Launch Instances in Public & Private Subnets Using Terraform

Vanshita Mittal
4 min readAug 28, 2020

In the Task 3 of Hybrid Multi cloud :

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.

So here are the steps for proper understanding!

Task Steps:

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.

Solutions:

  1. First we make a directory to save our files and data in a separate workspace :

2. first we have to set a provider we will use aws as a provider. In provider we have to give our region and profile :

provider “aws” {
region = “ap-south-1”
profile = “profile_name”
}

3. Launch Key :

4. Now we will create one VPC :

5. Now we will create a security group :

6. Now we will create two subnets one for wordpress instance and one for mysql instance :

7. Now we will create one internet gateway :

8. Now we will create one route table which manages route for instances :

9. Launch instances one for wordpress and one for mysql :

10. Firstly initialize terraform to install plugins :

11. Now , to run above code :

Instances :

Internet gateway :

VPC :

Subnet :

Route :

12. Now using wordpress instance public ip :

13. After all, to destroy all these :

Finally , task is successfully completed !!

Thanks !!

--

--