Terraform Level 1, Task 4: Defining a VPC with a Specific Network Range¶
Today's task was a great reinforcement of the previous lesson on creating AWS VPCs, but with an important twist: I had to use a specific, smaller network range (192.168.0.0/24). This was a practical exercise in following architectural requirements and understanding how CIDR notation affects the size of a network.
I wrote a simple Terraform script to declare this VPC and then used the standard init, plan, apply workflow to deploy it. This document is my detailed, beginner-friendly explanation of the process, with a special focus on explaining the CIDR block in more detail.
Table of Contents¶
- The Task
- My Step-by-Step Solution
- Why Did I Do This? (The "What & Why")
- Deep Dive: Decoding the
aws_vpcResource and the CIDR Block - Common Pitfalls
- Exploring the Commands Used
The Task¶
My objective was to use Terraform to create a new AWS VPC with very specific parameters. The requirements were:
1. All code had to be in a single main.tf file.
2. The VPC's name in AWS had to be datacenter-vpc.
3. It needed to be created in the us-east-1 region.
4. It required a specific IPv4 CIDR block: 192.168.0.0/24.
My Step-by-Step Solution¶
The process followed the standard, predictable Terraform workflow.
Phase 1: Writing the Code¶
In the /home/bob/terraform directory, I created my main.tf file. I was careful to ensure no other .tf files were present. I wrote the following code to define my VPC exactly as requested.
# 1. Configure the AWS Provider to set the region
provider "aws" {
region = "us-east-1"
}
# 2. Define the VPC Resource with the specified CIDR
resource "aws_vpc" "datacenter_vpc_resource" {
cidr_block = "192.168.0.0/24"
tags = {
Name = "datacenter-vpc"
}
}
Phase 2: The Terraform Workflow¶
From my terminal in the same directory, I executed the three core commands.
-
Initialize: This downloaded the AWS provider plugin.
terraform init -
Plan: This "dry run" command showed me a preview, confirming that Terraform would create one
aws_vpcresource with the192.168.0.0/24CIDR block.terraform plan -
Apply: This command executed the plan. After I confirmed with
yes, Terraform built my VPC in the cloud.The success message,terraform applyApply complete! Resources: 1 added, 0 changed, 0 destroyed., was my confirmation that the task was done.
Why Did I Do This? (The "What & Why")¶
- VPC (Virtual Private Cloud): This is my private, isolated network in the AWS cloud. It's the foundational container for all other resources like servers and databases, providing network-level security and control.
- CIDR Block (Classless Inter-Domain Routing): This is the private IP address range for my VPC. The specific range, 192.168.0.0/24, was the key requirement of this task.
- What /24 means: An IP address is 32 bits long. The /24 is a "netmask" that means the first 24 bits are fixed for the network address, and the remaining 8 bits (32 - 24 = 8) are available for host addresses within that network.
- How many IPs? The number of available addresses is 2 to the power of the host bits, so 2^8 = 256 addresses (from 192.168.0.0 to 192.168.0.255). This is a much smaller network than the /16 I used previously (which has 65,536 addresses), making it suitable for a smaller, more segmented environment. This precision is crucial for good network design.
Deep Dive: Decoding the aws_vpc Resource and the CIDR Block¶
Understanding the structure of the Terraform code was key to this task.
[Image of an AWS Virtual Private Cloud diagram]
# The provider block tells Terraform which cloud API it needs to talk to.
# I'm telling it to use the "aws" provider for all "aws_" resources.
provider "aws" {
# The 'region' argument is required and was specified in the task.
region = "us-east-1"
}
# This is the resource block, the core of Terraform. It defines one
# piece of my infrastructure.
# "aws_vpc" is the Resource TYPE, telling Terraform I want a VPC.
# "datacenter_vpc_resource" is the local NAME. I use this name to refer to
# this specific VPC elsewhere in my Terraform code. It does not affect
# the name in AWS.
resource "aws_vpc" "datacenter_vpc_resource" {
# This is an 'argument' for the resource. The 'cidr_block' argument
# is required for a VPC and sets its private IP address range.
# I used the exact value from the task requirements.
cidr_block = "192.168.0.0/24"
# The 'tags' argument is a map of key-value pairs. This is how I
# label my resources in the cloud.
tags = {
# The "Name" tag is special. Its value is used as the display name
# in the AWS Console, which is incredibly helpful for identification.
Name = "datacenter-vpc"
}
}
Common Pitfalls¶
- Using the Wrong CIDR: The most common mistake on this specific task would be to use a different CIDR block than 192.168.0.0/24. The validation script would have failed if I had used the 10.0.0.0/16 from the previous task.
- Invalid CIDR Syntax: Entering the CIDR incorrectly (e.g., forgetting the /24) would cause Terraform to fail during the plan or apply phase, as the AWS API would reject the invalid value.
- Forgetting terraform init: As always, trying to run plan or apply in a new project without first running init will fail because the necessary provider plugins won't be downloaded.
Exploring the Commands Used¶
- terraform init: The first command to run in any new Terraform project. It prepares the working directory by downloading the provider plugins defined in the code (in this case, the aws provider).
- terraform plan: A "dry run" command. It reads my code and the current state of my cloud infrastructure and shows me a detailed plan of what it will create.
- terraform apply: The command that executes the plan. It builds the VPC in my AWS account, asking for a final yes for safety before making any changes.