---
title: "Building an AWS Terraform VPC Step-by-Step"
description: "In this step by step guide, you will learn how to build and run a Terraform configuration to build a Terraform VPC."
canonical: "https://adamtheautomator.com/terraform-vpc/"
---

# Building an AWS Terraform VPC Step-by-Step

> In this step by step guide, you will learn how to build and run a Terraform configuration to build a Terraform VPC.

Source: https://adamtheautomator.com/terraform-vpc/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Building an AWS Terraform VPC Step-by-Step](https://adamtheautomator.com/wp-content/uploads/2021/04/Building-an-AWS-VPC-with-Terraform-Step-by-Step.jpg)

# Building an AWS Terraform VPC Step-by-Step

[![](https://secure.gravatar.com/avatar/2beb65fca997135120ed98dc6a2e57dcdf1a7d7d2f5ff687b5d91dc7ccd7a6b5?s=192&d=mm&r=g)Sagar](https://adamtheautomator.com/author/shanky-mendiratta/)20 April 20216 min. read

Categories: [DevOps](/category/devops/)

Tags:[Terraform](/tag/terraform/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Understanding AWS VPCs](#h-understanding-aws-vpcs)
*   [Building the Terraform Configuration for an AWS VPC](#h-building-the-terraform-configuration-for-an-aws-vpc)
*   [Running Terraform to Create the AWS VPC](#h-running-terraform-to-create-the-aws-vpc)
*   [Verifying the AWS Terraform VPC](#h-verifying-the-aws-vpc)
*   [Conclusion](#h-conclusion)

If you need to set up [AWS Virtual Private Cloud (VPC)](https://aws.amazon.com/vpc/?vpc-blogs.sort-by=item.additionalFields.createdDate&vpc-blogs.sort-order=desc), you _could_ make it happen via the AWS Management Console but automating it is so much more fun! In a DevOps scenario, building AWS services via tools like [Terraform](https://www.terraform.io/intro/index.html) is a more scalable and automated approach to cloud resource provisioning.

In this tutorial, you will learn how to build and run a Terraform configuration to build a Terraform VPC from scratch!

## Prerequisites

This post will be a step-by-step tutorial. If you’d like to follow along, ensure you have the following in place:

*   An [AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/)
    
*   [Terraform](https://www.terraform.io/docs/enterprise/install/index.html) – This tutorial will use Terraform v0.14.9 running on Ubuntu 18.04.5 LTS, but any operating system with [Terraform](https://adamtheautomator.com/terraform-windows/) should work.
    

Related:[How to Install Terraform on Windows](https://adamtheautomator.com/terraform-windows/)

*   A code editor – Even though you can use any text editor to work with Terraform configuration files, you should have one that understands the HCL Terraform language. Try out [Visual Studio (VS) Code](https://code.visualstudio.com/).

## Understanding AWS VPCs

The AWS cloud has dozens of various services from compute, storage, networking, and more. Each of these services can communicate with each other just like an on-prem datacenter’s services. But, each of these services is independent of one another, and they are not necessarily isolated from other services. The AWS VPC changes that.

An AWS VPC is a single network that allows you to launch AWS services within a single isolated network. Technically, an AWS VPC is almost the same as owning a datacenter but with built-in additional benefits of scalability, fault-tolerance, unlimited storage, etc.

![AWS VPC architecture](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-66-1.png)

AWS VPC architecture

AWS VPCs are restricted by region. You cannot have one VPC spanning across regions with up to five VPCs supported per region.

## Building the Terraform Configuration for an AWS VPC

Enough talk, let’s get down to building!

1\. To start, create a folder to store your Terraform configuration files in. This tutorial will create a folder called _terraform-vpc-demo_ in your home directory.

```bash
mkdir ~/terraform-vpc-demo
 cd ~/terraform-vpc-demo
```

2\. Open your favorite code editor and copy/paste the following configuration already created for you saving the file as _main.tf_ inside of the _~/terraform-vpc-demo_ directory. Information about each resource to create is inline.

The _main.tf_ file contains all the resources which are required to be provisioned.

> _Terraform uses several different types of configuration files. Each file is written in either plain text format or JSON format. They have a specific naming convention of either [.tf or .tfjson format](https://www.terraform.io/docs/language/files/index.html)._

The Terraform configuration below:

*   Creates a VPC
    
*   Creates an [Internet Gateway](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html) and attaches it to the VPC to allow traffic within the VPC to be reachable by the outside world.
    
*   Creates a public and private subnet
    

> [Subnets](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Subnets.html#vpc-subnet-basics) are networks within networks. They are designed to help network traffic flow be more efficient and provide smaller, more manageable ‘chunks’ of IP addresses

*   Creates a route table for the public and private subnets and associates the table with both subnets
    
*   Creates a [NAT Gateway](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html) to enable private subnets to reach out to the internet without needing an externally routable IP address assigned to each resource.
    

```yaml
Create the VPC
 resource "aws_vpc" "Main" {                # Creating VPC here
   cidr_block       = var.main_vpc_cidr     # Defining the CIDR block use 10.0.0.0/24 for demo
   instance_tenancy = "default"
 }
 Create Internet Gateway and attach it to VPC
 resource "aws_internet_gateway" "IGW" {    # Creating Internet Gateway
    vpc_id =  aws_vpc.Main.id               # vpc_id will be generated after we create VPC
 }
 Create a Public Subnets.
 resource "aws_subnet" "publicsubnets" {    # Creating Public Subnets
   vpc_id =  aws_vpc.Main.id
   cidr_block = "${var.public_subnets}"        # CIDR block of public subnets
 }
 Create a Private Subnet                   # Creating Private Subnets
 resource "aws_subnet" "privatesubnets" {
   vpc_id =  aws_vpc.Main.id
   cidr_block = "${var.private_subnets}"          # CIDR block of private subnets
 }
 Route table for Public Subnet's
 resource "aws_route_table" "PublicRT" {    # Creating RT for Public Subnet
    vpc_id =  aws_vpc.Main.id
         route {
    cidr_block = "0.0.0.0/0"               # Traffic from Public Subnet reaches Internet via Internet Gateway
    gateway_id = aws_internet_gateway.IGW.id
     }
 }
 Route table for Private Subnet's
 resource "aws_route_table" "PrivateRT" {    # Creating RT for Private Subnet
   vpc_id = aws_vpc.Main.id
   route {
   cidr_block = "0.0.0.0/0"             # Traffic from Private Subnet reaches Internet via NAT Gateway
   nat_gateway_id = aws_nat_gateway.NATgw.id
   }
 }
 Route table Association with Public Subnet's
 resource "aws_route_table_association" "PublicRTassociation" {
    subnet_id = aws_subnet.publicsubnets.id
    route_table_id = aws_route_table.PublicRT.id
 }
 Route table Association with Private Subnet's
 resource "aws_route_table_association" "PrivateRTassociation" {
    subnet_id = aws_subnet.privatesubnets.id
    route_table_id = aws_route_table.PrivateRT.id
 }
 resource "aws_eip" "nateIP" {
   vpc   = true
 }
 Creating the NAT Gateway using subnet_id and allocation_id
 resource "aws_nat_gateway" "NATgw" {
   allocation_id = aws_eip.nateIP.id
   subnet_id = aws_subnet.publicsubnets.id
 }
```

3\. Now, create another file inside _~/terraform-vpc-demo_ directory, name it _vars.tf_ and paste the content below.

> _Vars.tf is a [Terraform variables file](https://www.terraform.io/docs/language/values/variables.html) that contains all the variables that the configuration file references._

You can see variables references in the configuration file by:

*   `var.region`
*   `var.main_vpc_cidr`
*   `var.public_subnets`
*   `var.private_subnets`

```bash
variable "region" {}
 variable "main_vpc_cidr" {}
 variable "public_subnets" {}
 variable "private_subnets" {}
```

> _It is possible to include all configuration values in a single configuration file. To keep things clear and make it easier for developers and admins, it is important to break things up._

4\. Create one more file inside _~/terraform-vpc-demo_ directory, paste in the following code, and name it as _provider.tf_ to define the AWS provider. The tutorial will be creating resources in the `us-east-2` region.

> _The providers file defines providers such as AWS, Oracle or Azure, etc., so that Terraform can connect with the correct cloud services._ `provider "aws" { region = "us-east-2" }`

```yaml
provider "aws" {
   region = "us-east-2"
 }
```

5\. Finally, create one more file inside the _~/terraform-vpc-demo_ directory, name it _terraform.tfvars,_ and paste the code below. This variables file contains the values that Terraform will use to replace the variable references inside of the configuration file.

```yaml
main_vpc_cidr = "10.0.0.0/24"
 public_subnets = "10.0.0.128/26"
 private_subnets = "10.0.0.192/26"
```

Now, open your favorite terminal (Bash in this case) and verify all of the required files below are contained in the folder by running the [`tree`](https://en.wikipedia.org/wiki/Tree_\(command\)) command.

```bash
tree terraform-vpc-demo  
```

![Folder structure of terraform files](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-67-2.png)

Folder structure of terraform files

## Running Terraform to Create the AWS VPC

Now that you have the Terraform configuration file and variables files ready to go, it’s time to initiate Terraform and create the VPC! To provision, a Terraform configuration, Terraform typically uses a three-stage approach [terraform init](https://www.terraform.io/docs/cli/commands/init.html) → [terraform plan](https://www.terraform.io/docs/cli/commands/plan.html) → [terraform apply](https://www.terraform.io/docs/cli/commands/apply.html). Let’s walk through each stage now.

1\. Open a terminal and navigate to the _~\\terraform-vpc-demo_ directory.

```bash
cd ~\terraform-vpc-demo
```

2\. Run the `terraform init` command in the same directory. The [`terraform init`](https://www.terraform.io/docs/cli/commands/init.html) command initializes the plugins and providers which are required to work with resources.

```bash
terraform init
```

If all goes well, you should see the message `Terraform has been successfully initialized` in the output, as shown below.

![Terraform initialized successfully](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-68-1.png)

Terraform initialized successfully

3\. Now, run the [`terraform plan`](https://www.terraform.io/docs/cli/commands/plan.html) command. This is an optional, yet recommended action to ensure your configuration’s syntax is correct and gives you an overview of which resources will be provisioned in your infrastructure. `terraform plan`

```yaml
terraform plan
```

If successful, you should see a message like `Plan: "X" to add, "Y" to change, or "Z" to destroy` in the output to indicate the command was successful. You will also see every AWS resource Terraform intends to create.

![Plan command execution](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-69-1.png)

Plan command execution

4\. Next, tell Terraform actually to provision the AWS VPC and resources using `terraform apply`. When you invoke `terraform apply`, Terraform will read the configuration (_main.tf_) and the other files to compile a configuration. It will then send that configuration up to AWS as instructions to build the VPC and other components.

```yaml
terraform apply
```

![Terraform apply command execution ](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-70-1.png)

Terraform apply command execution

> _Notice the IDs defined in the Terraform output. You’ll need these IDs to correlate the resources created in the next section._

The Terraform command executed successfully, so let’s switch over to the AWS Management Console to confirm that the VPC and components were created successfully.

## Verifying the AWS Terraform VPC

By now, you should have created the VPC with Terraform. Let’s verify by manually checking for the VPC in the AWS Management Console.

1\. Open your favorite web browser and navigate to the [AWS Management Console](https://aws.amazon.com/console/) and log in.

2\. While in the Console, click on the search bar at the top, search for ‘vpc’, and click on the **VPC** menu item.

![Navigating to the VPC service](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-71-1.png)

Navigating to the VPC service

3\. Once on the VPC page, click on **Your VPCs**. You should see the VPC created with the same ID Terraform returned earlier.

![VPC created](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-72-1.png)

VPC created

4\. Since Terraform created more than just the VPC resource but all of the resources required for the VPC, you should then find each resource on this page also.

![Subnets created](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-74-1.png)

Subnets created

![Route tables created ](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-75-1.png)

Route tables created

![Internet Gateway created](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-76.png)

Internet Gateway created

![NAT Gateway created](https://adamtheautomator.com/wp-content/uploads/2021/04/Untitled-77.png)

NAT Gateway created

With all the components successfully created, using Terraform, this VPC is ready to go!

## Conclusion

In this tutorial, you’ve learned how to use Terraform to deploy an AWS VPC and its components using Terraform.

Building an Amazon Virtual Private Cloud with Terraform allows you to create resources quickly, easily, and predictably. You are now ready to use this knowledge with other AWS services and build powerful services on top of it.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-vpc%2F&text=Building%20an%20AWS%20Terraform%20VPC%20Step-by-Step)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fterraform-vpc%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fterraform-vpc%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2023/02/terraform-best-practices.jpg)

### [Ensuring Success by Learning Terraform Best Practices](/terraform-best-practices/)

Discover Terraform best practices and ensure that your DevOps environment is being utilized safely and securely to it’s fullest potential!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Which-Tool-Terraform-vs-Cloudformation-in-the-Fight-for-AWS.jpg)

### [Which Tool? Terraform vs. Cloudformation in the Fight for AWS](/terraform-vs-cloudformation/)

Learn which tool, Terraform vs Cloudformation is right for infrastructure as configuration with AWS in this ATA Learning tutorial!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/A-Definitive-Guide-to-Leveraging-the-AWS-Terraform-IAM-Role.jpg)

### [A Definitive Guide to Leveraging the AWS Terraform IAM Role](/terraform-iam-role/)

Discover this definitive guide to the AWS Terraform IAM role and take your devops game to the next level in this ATA Learning tutorial.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
