A Step-by-Step Guide to Creating VPC and Subnet on AWS Using Terraform AWS VPC module.
Tags: iaac, aws
Published: 2023-07-07 13:00:39

**Amazon Web Services** (AWS) provides a robust and scalable infrastructure for building and deploying applications. To effectively utilize AWS resources, it's crucial to understand how to set up Virtual Private Clouds (VPCs) and subnets. 
This blog post will walk you through the process of creating a VPC and subnet using **Terraform**, an _infrastructure as code_ tool.

**Requirements:**
make sure to have the following before beginning:
1. An AWS account with and generate your access key ID and secret access key with appropriate permissions to create VPCs and subnets. Refer to this [blog](https://dev.to/sirlawdin/enhancing-aws-security-a-step-by-step-guide-to-creating-an-iam-user-and-configuring-it-on-your-computer-ehm) for detailed instruction.
2. Install Terraform on your machine. You can download it from the official [website](https://www.terraform.io/downloads.html)
3. Create a new directory for your Terraform project and navigate to it in your terminal or command prompt.


## Initialize Terraform

Set up your Terraform working directory and download the required provider plugins. Follow these steps to initialize Terraform:

Create a new directory for your Terraform project:

```
mkdir my-terraform-project
cd my-terraform-project
```
Create a new file named main.tf and open it in a code editor.

Copy the following code into main.tf:

```
provider "aws" {
  region = "us-east-1"  # Replace with your desired region
}

module "vpc" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"

  name = "my-vpc"
  cidr = "10.0.0.0/16"
  azs             = ["eu-east-1a", "eu-east-1b"]
  private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]
  public_subnets  = ["10.0.101.0/24", "10.0.102.0/24"]

  # Database Subnets
  create_database_subnet_groups      = true
  create_database_subnet_route_table = true
  database_subnets                   = ["10.0.151.0/24", "10.0.152.0/24"]

  # NAT Gateways - Outbound
  enable_nat_gateway    = true
  single_nat_gateway    = true

  # VPC DNS Parameters
  enable_dns_hostname   = true
  enable_dns_support    = true

  public_subnet_tags = {
    Name = "public_subnets"   
  }

  private_subnet_tags = {
    Name = "private_subnets"
  }

  tags = {
    Owner       = "Sirlawin"
    Environment = "dev"
  }

  vpc_tag = {
    Name = "vpc_dev"
  }
}
```

You can modify the code to suit your requirements. For example, replace the region, availability zones, CIDR blocks, and tags with your desired values. You can access the [Terraform AWS VPC Module documentation](https://registry.terraform.io/modules/terraform-aws-modules/vpc/aws/latest) for more details on how to customize the VPC configs. 

Save the main.tf file.

Open a terminal or command prompt and navigate to your project directory (my-terraform-project).

Run the following command to initialize the working directory containing Terraform configuration files and install any required plugins:

```
terraform init
```

## Apply the Terraform Configuration

The next step is to apply the Terraform configuration to create the VPC and subnets on AWS.

The following command will apply the Terraform configuration:

```
terraform apply
```

It is advisable to review the changes that Terraform will apply. Check if everything is correct, type yes and press Enter to confirm and proceed.

Terraform may take a few minutes to provision the VPC and subnets on AWS.

You have successfully created a VPC and subnets on AWS using the Terraform VPC module. You can now leverage this infrastructure to deploy and manage your applications.

Creating a VPC and subnets is crucial in building your AWS infrastructure as VPC houses them. With Terraform and the Terraform VPC module, you can automate the process and easily define your network configuration as code. By following this step-by-step guide, you have learned how to create a VPC and subnets on AWS using the Terraform VPC module. Feel free to explore further and customize the configuration to suit your specific requirements.