VPC using Terraform
Tags: terraform, vpc, aws, infrastructureascode
Published: 2023-07-27 20:24:48

Create a VPC from scratch using Terraform (Infrastructure as a Code)

In this blog, we are going to build a VPC infrastructure with Public and Private subnets that span across all the availability zones (AZ) in a region and make reach public subnets to the internet with the help of an internet gateway.

Source Code — [Click Here](https://github.com/gokul98raj/Terraform-vpc)

![VPC](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19e9jw3z6k7mr4cx6mr0.png)

AWS following components which we required to create for this setup,
VPC

- 1 Internet Gateway
- 3 Public Subnets, one in each AZ
- 3 Private Subnets, one in each AZ
- Route table configurations

Let's get started to build a VPC with Terraform in AWS by following the steps.

**Step 1 - Create a VPC**

To begin with VPC resource in Terraform. To specify a range of IP addresses in a VPC, a CIDR block needs to be provided. We have also provided a Name tag for identification.

```
variable "vpc_cidr" {
  description = "CIDR Value"
  default     = "10.0.0.0/16"
}`

`resource "aws_vpc" "vpc_main" {
  cidr_block       = var.vpc_cidr
  instance_tenancy = "default"

  tags = {
    Name = vpc_main
  }
}
```

**Step 2 - Create Public and Private subnets**

Firstly, we identify the CIDR ranges to be associated with the six new subnets we need to create. In our example, based on the CIDR range of the VPC I have identified the CIDR ranges and defined a couple of variables in our Terraform code (variables.tf).

```
variable "public_subnets_cidr" {
  type        = list(string)
  description = "Public Subnets"
  default     = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]

}

variable "private_subnets_cidr" {
  type        = list(string)
  description = "Private Subnets"
  default     = ["10.0.4.0/24", "10.0.5.0/24", "10.0.6.0/24"]

}
```

In a resource block, we should provide the subnet configurations which include CIDR blocks and AZ details.

```
resource "aws_subnet" "public_subnets" {
  count             = length(var.public_subnets_cidr)
  vpc_id            = aws_vpc.vpc_main.id
  cidr_block        = element(var.public_subnets_cidr, count.index)
  availability_zone = element(var.availability_zone, count.index)

  tags = {
    Name = "Public Subnet ${count.index + 1}"
  }
}

resource "aws_subnet" "private_subnets" {
  count             = length(var.private_subnets_cidr)
  vpc_id            = aws_vpc.vpc_main.id
  cidr_block        = element(var.private_subnets_cidr, count.index)
  availability_zone = element(var.availability_zone, count.index)

  tags = {
    Name = "Private Subnet ${count.index + 1}"
  }
}
```

**Step 3 - Create an Internet Gateway**

Internet gateway will access the subnets to the internet. since we are using public subnets, we need to provide access to the internet in the given VPC.

```
resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.vpc_main.id
tags = {
    Name = "vpc_main_igw"
  }
}
```

**Step 4 - Create Route Table and RT association**

However, the route table is an important aspect of making the resource communicate with each other. By default, VPC has a local route table created when VPC is created. Now are going to create a new route table with a destination of the internet gateway and associate it with public subnets to get internet access.

```
resource "aws_route_table" "public_rt" {
  vpc_id = aws_vpc.vpc_main.id

  route {
    cidr_block           = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }

  tags = {
    Name = "Public Route "
  }

}

resource "aws_route_table_association" "public_rt_association" {
  count          = length(var.public_subnets_cidr)
  subnet_id      = element(aws_subnet.public_subnets[*].id, count.index)
  route_table_id = aws_route_table.public_rt.id

}
```

We have now successfully implemented the VPC design represented in the diagram using Terraform.

**_Will catch up on upcoming blogs…_**