In today’s cloud-driven world, managing your network infrastructure efficiently is essential, and creating a Virtual Private Cloud (VPC) is one of the foundational steps in building a secure environment on AWS. As an Infrastructure as Code (IaC) tool, Terraform enables you to define, deploy, and manage AWS resources, including VPCs, consistently and repeatably. This step-by-step guide will walk you through building a basic VPC using Terraform.

Introduction to Terraform for VPC Creation

Terraform is an open-source tool for writing declarative configurations for provisioning cloud infrastructure. It allows you to define AWS resources like VPCs, subnets, and route tables as code. This approach simplifies deploying and managing AWS infrastructure by providing a single source of truth, allowing for version control, scalability, and collaboration among teams.

When creating a VPC, it’s crucial to carefully define its network structure, subnets, and other components. Terraform automates the provisioning process, enabling you to focus on the logical design rather than manual setup.

Configuring AWS Provider and Variables

Before we create our VPC, the first step is configuring the AWS provider in Terraform. The provider defines the AWS credentials, region, and other settings for Terraform to interact with your AWS account. Variables will help you customize your configuration, making it reusable and flexible.

  1. Create a provider.tf file to configure the AWS provider:
    provider “aws” {

  region = var.aws_region

}

  1. Define variables in a variables.tf file for dynamic configuration:
    variable “aws_region” {

  default = “us-east-1”

}

variable “vpc_cidr” {

  default = “10.0.0.0/16”

}

This step ensures you can easily change regions or network ranges without altering the core configuration.

Creating the VPC Resource in Terraform

Now that the AWS provider and variables are configured, you can define the VPC resource. In Terraform, this is done through resource blocks, which describe the components you want to deploy.

Create a vpc.tf file to define the VPC:
resource “aws_vpc” “main” {

  cidr_block = var.vpc_cidr

  tags = {

    Name = “My-VPC”

  }

}

This configuration creates a basic VPC with the CIDR block defined in the variable file. To fully build out your VPC network, you can expand this configuration by adding other resources such as subnets, route tables, and Internet Gateways.

Storing Terraform State Files Remotely

By default, Terraform stores the state file (which tracks the infrastructure it manages) locally. However, storing state files remotely (e.g., in AWS S3) is a best practice, especially for team environments or when managing infrastructure at scale. It ensures your state file is secure, backed up, and accessible to other team members.

  1. Create a remote state storage configuration by defining an S3 backend in your backend.tf file:
    terraform {

  backend “s3” {

    bucket         = “my-terraform-state-bucket”

    key            = “vpc/terraform.tfstate”

    region         = “us-east-1”

    encrypt        = true

    dynamodb_table = “terraform-lock-table”

  }

}

  1. Create a DynamoDB table in AWS to manage state file locking and ensure only one user or process can modify the infrastructure at a time.

Applying the Terraform Configuration

Once everything is configured, the next step is applying the Terraform configuration to create your VPC. This process consists of initializing Terraform, reviewing the plan, and using it to provision resources in AWS.

  1. Initialize Terraform:
    terraform init

This command sets up the backend and prepares your working directory for other Terraform commands.

  1. Review the execution plan to ensure Terraform will create the desired infrastructure:
    terraform plan

This command shows the actions Terraform will take without actually performing them.

  1. Apply the configuration to create the VPC and other resources:
    terraform apply

Confirm the prompt, and Terraform will begin provisioning the VPC on AWS.

Conclusion

Following these steps, you’ve successfully created a basic VPC using Terraform. You have learned how to define the VPC resource and set up dynamic configurations using variables, configured remote state storage, and applied the configuration to AWS. This process provides a solid foundation for building more complex infrastructure as your cloud environment grows.

Terraform makes infrastructure management efficient and scalable, ensuring that your AWS environment is defined as code and version-controlled.

References

VPC with servers in private subnets and NAT

Plan your VPC