Create AWS VPC, public subnet, using terraform

In my previous post I showed you how to create EC2 instance using terraform. In this post I will show you how to create VPC, create public subnet in two availability zones and then add load balancer and an internet gateway to allow traffic. Also add EC2 instances and run Apache server on them and allow traffic to the servers through load balancer. And I will show you how to create this infrastructure as code by using Terraform.

VPC (Virtual Private Cloud) is an isolated group of infrastructure resources in a public cloud environment. It is a virtual network same as on premises, but on public cloud and the benefit of VPC is scalable infrastructure. In this example I will create a VPC and add two public subnets in two availability zones. Then I will add EC2 instances in each availability zone and add application load balancer. On top I will add internet gateway to allow traffic from internet. Below diagram will explain the VPC and resources we are going to create. I named my VPC Zoro, you can name any.


I am going to create this new VPC in US East region of AWS. In my previous post I separate the variables in a ‘’ file. So I will do the same thing here. I define region, access keys and availability zones. I am creating zones in ‘US-EAST-1A’ and ‘US-EAST-1B’. The complete code of variables file shown below.

In the file after setting the region and secret key, I created the VPC. CIDR (Classless Inter Domain Routing) block IP address rang I set to ‘’. Instance tenancy I set to default. With default option the VPC will be created on shared resources. If you set the ‘instance_tenancy’ value to ‘dedicated’, AWS will reserve dedicated resources for the VPC. For enterprise projects its a good idea. But for small projects or for demo I would suggest to set instance_tenancy to default so your resource will remain in free tier or it cost you minimal. In the next step I created two subnets in two availability zones. And keep them under the VPC by using the VPC Id ‘${}’.

Internet gateway allow internet traffic to communicate with our resources. But we will allow internet traffic to communicate with our instances only through load balancer. As you can see in the above diagram. In the below code you, I created a internet gateway. Then create a route table, and create its association with two subnets.

Next I created a load balancer security group which allow incoming traffic (ingress) on port 80 and allow all out going traffic (egress). And assign this security group to the load balancer in the next step. And assign two subnets to my new load balancer zoro-lb. Then I created target group and assign the target group to load balancer via load balancer listener.

EC2 instance T2 micro comes under free tier. I created two EC2 machines in two zones. The EC2 machines will read the user data from file. User data will update the instance and install apache server, create a index page which will return the machine IP address. A new security group is created which allow TCP traffic at port 80 and it will allow traffic only through load balancer. In the end newly created instances are added to load balancer target group.

Now run the terraform file and you will see that it will create the VPC, subnets load balancer and EC2 instances. The security groups are also get created. You can find the complete code here.

Tagged with: , , , , , ,