Create a VPC using Terraform

The Nautilus DevOps team is strategizing the migration of a portion of their infrastructure to the AWS cloud. Recognizing the scale of this undertaking, they have opted to approach the migration in incremental steps rather than as a single massive transition. To achieve this, they have segmented large tasks into smaller, more manageable units. This granular approach enables the team to execute the migration in gradual phases, ensuring smoother implementation and minimizing disruption to ongoing operations. By breaking down the migration into smaller tasks, the Nautilus DevOps team can systematically progress through each stage, allowing for better control, risk mitigation, and optimization of resources throughout the migration process.

Create a VPC named nautilus-vpc in region us-east-1 with any IPv4 CIDR block through terraform.

The Terraform working directory is /home/bob/terraform. Create the main.tf file

We are going to create a VPC named "nautilus-vpc" in the us-east-1 region.

The Terraform configuration will be written in the main.tf file at /home/bob/terraform.

We'll use the AWS provider and specify the region as "us-east-1". We'll create an AWS VPC resource with a given IPv4 CIDR block. We can choose any CIDR, for example, "10.0.0.0/16".

Since the requirement is to have the main.tf file and not any other .tf files, we will put everything in one file.

Steps:

1. Configure the AWS provider for region us-east-1.

2. Create a VPC resource with the name "nautilus-vpc" and assign a CIDR block.

Note: We are not required to use any specific module, so we will use the basic VPC resource.

Let's write the configuration in the main.tf file.

To create the specified VPC using Terraform, follow these steps:

  1. Create the Terraform directory and main.tf file:
mkdir -p /home/bob/terraform
cd /home/bob/terraform
touch main.tf
  1. Add the following configuration to /home/bob/terraform/main.tf:
provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "nautilus_vpc" {
  cidr_block = "10.0.0.0/16"
  tags = {
    Name = "nautilus-vpc"
  }
}
  1. Initialize and apply the configuration:
terraform init
terraform apply

This configuration will:

  • Configure the AWS provider for the us-east-1 region

  • Create a VPC named "nautilus-vpc" with:

    • IPv4 CIDR block: 10.0.0.0/16

    • Automatic DNS hostnames enabled

    • Default tenancy setting

  • Apply the required "Name" tag to identify the VPC

The CIDR block 10.0.0.0/16 provides:

  • 65,536 private IP addresses

  • A standard size for new VPCs

  • Room for subnetting in future expansion

verify the VPC creation in the AWS console or using:

0
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

Kunal Kumar Singh
Kunal Kumar Singh