Effortless Provisioning of VPC on AWS with Terraform
Tags: aws
Published: 2024-01-15 06:53:38

> Forget the struggle of manual AWS VPC configuration. Terraform steps in as your automation hero, empowering you to effortlessly build your network with concise code and precise control.

## Terraform's Advantages:

**- Automation Maestro**: Ditch the tedious clicks and configure your VPC in a single file. Simply describe your desired infrastructure, and Terraform orchestrates the magic behind the scenes.
**- Error-proofing Fortress**: Bid farewell to configuration woes. Terraform's validation checks catch potential issues before deployment, ensuring a smooth and successful ride.

## Crafting Your VPC Step-by-Step:

For easier, you can copy my code. Then I will explain about it.
`/main.tf`

```
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.16"
    }
  }

  required_version = ">= 1.2.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "networkABC" {
  cidr_block = "10.0.0.0/16"

  tags = {
    "author" = "vuongbach"
  }
}

resource "aws_internet_gateway" "networkABC_IGW" {
  vpc_id = aws_vpc.networkABC.id
}

resource "aws_subnet" "networkABC_public_subnets" {
  count      = length(var.public_subnet_cidrs)
  vpc_id     = aws_vpc.networkABC.id
  cidr_block = var.public_subnet_cidrs[count.index]
}

resource "aws_subnet" "networkABC_private_subnets" {
  count      = length(var.private_subnet_cidrs)
  vpc_id     = aws_vpc.networkABC.id
  cidr_block = var.private_subnet_cidrs[count.index]
}

resource "aws_route_table" "networkABC_RT_public" {
  vpc_id = aws_vpc.networkABC.id
  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.networkABC_IGW.id
  }
}

resource "aws_route_table_association" "networkABC_RT_public_association" {
  count          = length(aws_subnet.networkABC_public_subnets)
  subnet_id      = aws_subnet.networkABC_public_subnets[count.index].id
  route_table_id = aws_route_table.networkABC_RT_public.id
}

```

`/vars.tf`

```
variable "vpc_cidr" {
  description = "CIDR block for the VPC"
  type        = string
  default     = "10.0.0.0/16"
}

variable "public_subnet_cidrs" {
  description = "CIDR blocks for the public subnets"
  type        = list(string)
  default     = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
}

variable "private_subnet_cidrs" {
  description = "CIDR blocks for the private subnets"
  type        = list(string)
  default     = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]
}

```
**Code Structure:**

`main.tf`: This is the stage where your VPC setup takes center stage. Define providers, CIDR blocks, gateways, subnets, and route tables, crafting the blueprint for your network.

`vars.tf`: Imagine this as the dressing room - store variables like CIDR blocks and subnet configurations for easy modification and cleaner code in main.tf.
Code Breakdown - Building Blocks of Your VPC:
Provider Declaration: Specify the AWS provider and region, setting the stage for your cloud deployment.
VPC Establishment: Define the heart of your network - the VPC - with its CIDR block and a nametag for easy identification.

**VPC resource in Terraform code**

- Internet Gateway: Create a gateway for venturing into the vast internet, attaching it to your VPC for seamless external connectivity.

- Subnets: Designate both public and private subnets within your VPC, assigning them individual CIDR blocks to segment your network for optimized security and control.

- Route Table: Establish a public route table, directing traffic through the internet gateway for your public subnets.

- Association: Connect your public subnets with the public route table, ensuring they have access to the wider world.

**Commanding the Stage:**

Once your code is ready, it's time to bring your VPC to life!

- `terraform init`: Initialize Terraform within your project directory.
- `terraform fmt`: Ensure your code is neat and tidy for better readability.
- `terraform validate`: Double-check your configuration for any lurking errors.
- `terraform apply`: Let Terraform work its magic, deploying your VPC infrastructure to the AWS cloud.

Upon successful deployment, witness your VPC network come to life, ready to host your applications and services.