Terraform is an open-source infrastructure as a code software tool created by HashiCorp. It enables users to define and provision a data center infrastructure using a high-level configuration language known as Hashicorp Configuration Language (HCL).

When we create a VPC we need to manage all the resources in VPC like CIDR block e.g 10.0.0.0/16, Subnets, Route table, Internet gateway, subnet association, Security groups, Ec2 instance inside the VPC, and subnet associated with it. Now we are going to create all these resources with Terraform.


Below are the resources that we are going to create with Terraform:-

  1. VPC
  2. Subnet inside created VPC
  3. Internet gateway with VPC
  4. Route table inside VPC with a route that will help to access the internet
  5. Route table associated with our subnet.
  6. Security group inside VPC

Let’s get started. Here I am using multiple tf files

Create one directory named “any_name” in my case my directory name is AWS_VPC which contain multiple tf files

Step 1::- Create a provider.tf file which contains provider AWS, access, and secret key, and region

provider "aws" {
access_key = "ACCESS_KEY_HERE"
secret_key = "SECRET_KEY_HERE"
region = "us-east-2"
}

Once the provider is set the next step is to create a variable.tf which we are going to use in our next steps

Step 2:- Create a variable.tf file which contains variables that we use in our next step

variable "cidr_vpc" {
description = "CIDR block for the VPC"
default = "10.1.0.0/16"
}
variable "cidr_subnet" {
description = "CIDR block for the subnet"
default = "10.1.0.0/24"
}
variable "availability_zone" {
description = "availability zone to create subnet"
default = "us-east-2a"
}
variable "instance_ami" {
description = "AMI for aws EC2 instance"
default = "ami-0cf31d971a3ca20d6"
}
variable "instance_type" {
description = "type for aws EC2 instance"
default = "t2.micro"
}
variable "bangmetric_tag" {
description = "Bangmetric tag"
default = "Bangmetric"
}

Once your variable is set we are going to use it in our VPC creation and other resources

Step 3:- Create a file name vpc.tf

resource "aws_vpc" "vpc" {
cidr_block = "${var.cidr_vpc}"
enable_dns_support = true
enable_dns_hostnames = true
tags {
"Environment" = "${var.bangmetric_tag}"
}
}

Step 4:- Create a file name igw.tf which is used by subnet to access the internet

resource "aws_internet_gateway" "igw" {
vpc_id = "${aws_vpc.vpc.id}"
tags {
"Environment" = "${var.bangmetric_tag}"
}
}

Step 5:- Now create a file subnet.tf which is inside the VPC

resource "aws_subnet" "subnet_public" {
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "${var.cidr_subnet}"
map_public_ip_on_launch = "true"
availability_zone = "${var.availability_zone}"
tags {
"Environment" = "${var.bangmetric_tag}"
}
}

Step 6:- Create a file route.tf which is used to access the internet from inside

resource "aws_route_table" "rtb_public" {
vpc_id = "${aws_vpc.vpc.id}"
route {
cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.igw.id}"
}
tags {
"Environment" = "${var.bangmetric_tag}"
}
}

Step 7:- Create a file subnetass.tf which is used to make our subnet public

resource "aws_route_table_association" "rta_subnet_public" {
subnet_id = "${aws_subnet.subnet_public.id}"
route_table_id = "${aws_route_table.rtb_public.id}"
}

Step 8:- create a file sg.tf for accessing the EC2 instance on port 22

resource "aws_security_group" "bangmetric ssh" {
name = "bangmetric ssh"
vpc_id = "${aws_vpc.vpc.id}"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags {
"Environment" = "${var.bangmetric_tag}"
}
}

Setup 9:- At last create a file ec2.tf which we used to Ec2 Instance in our subnet, VPC

resource "aws_instance" "testInstance" {
ami = "${var.instance_ami}"
instance_type = "${var.instance_type}"
subnet_id = "${aws_subnet.subnet_public.id}"
vpc_security_group_ids = ["${aws_security_group.sg_22.id}"]
key_name = "testing"
tags {
Environment = "${var.bangmetric_tag}"
}
}

Execute the command terraform init in the current directory

Execute the command terraform plan in the current directory

At last, execute the terraform apply command which will create all your resource

Congratulations… you follow all the steps and set up VPC resource using Terraform

Reference:-

  • https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/vpc

Your email address will not be published. Required fields are marked *