Tutorial: How to deploy a VPC in AWS using Terraform
This is the first of a series of blogs covering AWS services and Terraform as IaC. In this tutorial I cover step by step how to deploy a VPC with private and public subnets
8/14/20242 min read
First, I used variables to define key aspects such as the VPC name, CIDR block, the number of private and public subnets, and their respective names. For this example, I set the following default values:
VPC Name: "two-tier-vpc"
CIDR Block: "150.0.0.0/16"
Private Subnets: "private_subnet_1", "private_subnet_2"
Public Subnets: "public_subnet_1", "public_subnet_2"
Here's what the variables file looks like:
Step 1: Defining Variables


This configuration allows for the creation of as many VPCs as required, with the ability to change the values of the variables to suit specific project needs. As a best practice, I also created a common_tags variable to apply consistent and organized tags across all resources.
Step 2: Deploying the VPC in the London Region
Next, I deployed the VPC in the eu-west-2 (London) region. I used the for_each argument to simplify the process of creating multiple subnets, dynamically generating a subnet for each entry in the private_vpc_subnets and public_vpc_subnets variables.
To allocate multiple network addresses efficiently, I leveraged the tolist() function to generate a list of availability zones and the cidrsubnet() function to allocate subnet addresses.
Step 3: Configuring Internet Gateway and Route Tables
After setting up the subnets, I created an Internet Gateway to allow internet access to the VPC. I then defined both private and public route tables, associating them with their respective subnets. The public route table includes a route that allows traffic from any source (0.0.0.0/0) via the Internet Gateway.




In this project, I chose to create a separate configuration file for my Virtual Private Cloud (VPC). One of the key benefits of using Infrastructure as Code (IaC) is the ability to reuse and repurpose configurations, which saves time in future projects. With that in mind, I built this VPC to be easily adaptable for other deployments.
You can find the Terraform code files for this tutorial in my public repository:
github.com/tales-menezes/two-tier-solution/
Final Thoughts
This tutorial demonstrates how easy it is to deploy a private virtual machine (VM) on AWS using Terraform. Additionally, you can leverage a Terraform module from the official registry to streamline the process. For more details, you can check the VPC module here: Terraform AWS VPC Module.
Prerequisites
To follow this step-by-step guide, ensure that:
You are using a machine with Terraform version 1.9.0 or higher.
Your AWS account is set up and integrated with Terraform for infrastructure provisioning.
Providers
In this project, I used the following provider versions:


Cloud Professional
Optimizing cloud solutions for security and performance.
need a cloud solution?
Let's have a chat:
© 2024. All rights reserved.