Terraforming AWS VPC
Published: 2020-10-21 13:43:59

---
title: Terraforming AWS VPC
published: true
date: 2020-10-19 00:00:00 UTC
tags: 
canonical_url: https://bitsofknowledge.net/2020/10/19/terraforming-aws-vpc/
---

[AWS VPC](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html) stands for Virtual Private Cloud and it represents the networking layer for the AWS EC2 services (computing). In this post, we are going to cover how to automate the configuration of AWS VPC using [Terraform](https://www.terraform.io/). If you are not familiar with Terraform, you can check my introductory post [here](https://bitsofknowledge.net/2020/09/30/first-steps-with-terraform-in-aws/).

If you want to go straight to the code, you can check it out at [aws-terraform-examples/03-terraforming-aws-vpc](https://github.com/renehernandez/aws-terraform-examples/tree/master/03-terraforming-aws-vpc).

Already back? Great! Let's dive in.

## Key concepts [#](#key-concepts)

Before getting into the implementation, let's briefly mention some of the essential concepts around networking and VPC.

#### Availability Zones (AZ) and Regions [#](#availability-zones-(az)-and-regions)

AWS has two main concepts to refer to the physical locations of their data centers:

- [Availability Zone (AZ)](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/): It represents one or more discrete data centers with redundant power, networking and connectivity in a particular AWS Region. All AZs are connected with high-bandwidth, low-latency networking with fully redundant and dedicated fiber communications, and traffic is encrypted.
- [Region](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/): Consists of multiple, isolated and physically separate AZ's within a geographic area.

#### Virtual Private Cloud (VPC) [#](#virtual-private-cloud-(vpc))

There are several key components in a VPC:

- [Subnet](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Subnets.html): A range of IP addresses. Can be either public or private.
- [Route table](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html): A set of rules that are used to determine where network traffic is directed. Each subnet is associated with a route table.
- [Internet Gateway](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html): Allows enabling communication between resources in your VPC and the internet and it serves two main purposes:
  - Provides a target in your VPC route tables for internet-routable traffic.
  - Performs network address translation (NAT) for instances that have been assigned public IPv4 addresses.
- [DNS in VPC](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html): AWS provides an AWS Route53 Resolver to act as a DNS server for a given VPC. The key considerations are:
  - Public and private DNS hostnames are provided for corresponding IPv4 addresses for each instance.
  - Managing DNS in the VPC is done through the attributes `enableDNSHostnames` (indicates if instances with public IP addresses get corresponding DNS hostnames) and `enableDNSSupport` (indicates whether DNS resolution is supported).
- [NAT](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat.html): A Network Translation Gateway (NAT) allows instances in a private subnet to connect to the internet while preventing external agents from initiating a request to the instance.

#### Terraform module [#](#terraform-module)

In this post, we are going to use a Terraform module to facilitate the VPC declaration. According to the [docs](https://www.terraform.io/docs/modules/index.html), a _module_ is a container for multiple resources that work together. _Modules_ can be used to create lightweight abstractions to describe infrastructure in terms of its architecture, rather than directly in terms of physical objects

Uff!! After a heavy dose of concepts, let's get right into the implementation.

## Implementation [#](#implementation)

The VPC declaration in Terraform is relatively short since we are leveraging the [VPC Module](https://github.com/terraform-aws-modules/terraform-aws-vpc)) maintained by the Terraform community, which comes packed with sane abstractions and useful defaults.

To create the VPC, we need to obtain the existing Availability Zones in the current region. We can either manually specify the AZ names, or leverage instead the [aws\_availability\_zones data source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones). This will allow Terraform to dynamically obtain the list of AZ from the region configured in the provider.

```
data "aws_availability_zones" "available" {}
```

Afterwards, we proceed to configure our new VPC as shown below:

```
module "vpc" { source = "terraform-aws-modules/vpc/aws" version = "~> 2.0" name = "example" cidr = "10.0.0.0/16" azs = data.aws_availability_zones.available.names private_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"] public_subnets = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"] enable_nat_gateway = true single_nat_gateway = true enable_dns_hostnames = true tags = { Terraform = "true" Environment = "dev" }}
```

Let's discuss some of the fields in the module declaration above:

#### cidr field [#](#cidr-field)

- Represents the range of IPv4 addresses that are going to be available for this VPC. Generally should refer to IPs considered to be a private range (e.g. 10.0.0.0, 172.16.0.0 - 172.31.255.25, 192.168.0.0 - 192.168.255.255).
- By assigning `10.0.0.0/16` to it, it means that the VPC will have access to 65536 (2^16) different IP addresses

#### Subnets fields [#](#subnets-fields)

- Both `private_subnets` and `public_subnets` are specifying 3 different subnets to be created, each one with 256 (2^8) IP addresses allocated to it.
- For both subnets, the module will make sure that the appropriate route tables and NAT gateways are created and attached.
- For the public subnets, the module will wire them up with the Internet Gateway associated with the VPC.

#### NAT gateways fields [#](#nat-gateways-fields)

- `enable_nat_gateway` informs the module that NAT Gateways should be provisioned for the private networks
- `single_nat_gateway` informs the module that all private subnets will share a single NAT Gateway

#### enable\_dns\_hostnames field [#](#enable_dns_hostnames-field)

- Combined with `enable_dns_support` (which is enabled by default), this informs the module to configure DNS resolution for the public IP addresses associated with instances in the VPC.

## Resources [#](#resources)

Below, it is a condensed list of all the resources mentioned throughout the post as well as a few others I consider may be of interest to deepen your knowledge.

**AWS VPC:**

- [AWS VPC](https://docs.aws.amazon.com/vpc/latest/userguide/what-is-amazon-vpc.html)
- [Route tables](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Route_Tables.html)
- [Subnets](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Subnets.html)
- [DNS in VPC](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-dns.html)
- [NAT in VPC](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat.html)
- [NAT Gateways](https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html)
- [Internet Gateways](https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Internet_Gateway.html)

**AWS AZ:**

- [AZ and region](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/)

**Terraform:**

- [VPC Module](https://github.com/terraform-aws-modules/terraform-aws-vpc)
- [AZ Data Source](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/availability_zones)

## Conclusion [#](#conclusion)

Let's sum up what we discussed in this post. First, we looked at some of the basic concepts around AWS VPC and AZ. Next, we dove into how to declare the VPC by leveraging the VPC module from Terraform and analyzed some of the fields that were specified as part of the module declaration. Finally, we listed the online resources used to create the post in case you want to go deeper in the topic.

Thank you so much for reading this post. Hope you enjoyed reading it as much as I did writing it. See you soon and stay tuned for more!!