Create a VPC using IPv4 CIDR


In Day 3 we have seen how we can create a VPC today we will see how we can create a VPC using specified IPv4 CIDR address.
It is very small task for today we just need a slight change from the Day 3.
Let’s do it.
Scenario :
The Nautilus DevOps team is strategically planning the migration of a portion of their infrastructure to the AWS cloud. Acknowledging the magnitude of this endeavor, they have chosen to tackle the migration incrementally rather than as a single, massive transition. Their approach involves creating Virtual Private Clouds (VPCs) as the initial step, as they will be provisioning various services under different VPCs.
Create a VPC named datacenter-vpc in us-east-1 region with 192.168.0.0/24 IPv4 CIDR using terraform.
The Terraform working directory is /home/bob/terraform.
We are going to create a VPC in the us-east-1 region with the specified CIDR block.
The resource will be named "datacenter-vpc".
We are to create the main.tf file in the directory /home/bob/terraform.
Steps:
1. Specify the AWS provider and set the region to us-east-1.
2. Define an AWS VPC resource with the given CIDR and name.
Note: We are only creating the main.tf file. Let's write the configuration accordingly.
To create the specified VPC using Terraform, follow these steps:
- Navigate to the Terraform working directory:
cd /home/bob/terraform
- Create or edit the main.tf file with the following content:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "6.6.0"
}
}
}
provider "aws" {
region = "us-east-1"
}
resource "aws_vpc" "datacenter-vpc" {
cidr_block = "192.168.0.0/24"
tags = {
Name = "datacenter-vpc"
}
}
- Initialize Terraform in the directory:
terraform init
- Review the execution plan:
terraform plan
- Apply the configuration to create the VPC:
terraform apply
Key points about this configuration:
Uses AWS provider with us-east-1 region
Creates a VPC named datacenter-vpc
Uses CIDR block 192.168.0.0/24
Includes a Name tag for identification in AWS console
Stores all configuration in a single main.tf file
The VPC will be created with:
256 total IP addresses (192.168.0.0 - 192.168.0.255)
AWS-assigned default DNS support
Default tenancy (shared hardware)
No additional options enabled by default (DNS hostnames, etc.)
After creation, you can verify the VPC in the AWS console under VPC services. Remember to run terraform destroy when you no longer need this VPC to avoid unnecessary charges.
Subscribe to my newsletter
Read articles from Kunal Kumar Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
