Skip to content

Terraform Level 1, Task 3: Building My First Cloud Network (VPC)

After creating smaller resources like key pairs and security groups, today's task was a huge step up. I used Terraform to create the single most fundamental component of any cloud infrastructure: a Virtual Private Cloud (VPC). This is the private, isolated network where all my other resources will eventually live.

This was an exciting task because it felt like I was laying the foundation for an entire datacenter in the cloud. I learned how to define this network in code, including its private IP address space, and then deploy it with the standard Terraform workflow. This document is my detailed, beginner-friendly explanation of that entire process.

Table of Contents


The Task

My objective was to use Terraform to create a new AWS VPC. The specific 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. The VPC needed to be created in the us-east-1 region. 4. I had to assign it an IPv4 CIDR block of my choice.


My Step-by-Step Solution

The process followed the same three-phase workflow I'm now becoming familiar with: Write, Plan, Apply.

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 to avoid the "duplicate provider" error I encountered in a previous task. I wrote the following code to define my VPC.

# 1. Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

# 2. Define the VPC Resource
resource "aws_vpc" "datacenter_vpc_resource" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "datacenter-vpc"
  }
}

Phase 2: The Terraform Workflow

From my terminal in the same directory, I executed the three core commands.

  1. Initialize: This downloaded the AWS provider plugin, preparing Terraform to communicate with AWS.

    terraform init
    

  2. Plan: This "dry run" command showed me exactly what Terraform was going to do. The output clearly stated it would create one aws_vpc resource, which was exactly what I wanted.

    terraform plan
    

  3. Apply: This command executed the plan. After I confirmed with yes, Terraform built my VPC in the cloud.

    terraform apply
    
    The success message, Apply 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 the most fundamental concept in cloud networking. I learned to think of a VPC as my own private, isolated datacenter inside the AWS cloud. By default, resources inside my VPC cannot communicate with the public internet or with resources in other VPCs. This isolation is the foundation of cloud security. It's the first thing I need to create before I can launch any servers, databases, or other resources. - CIDR Block (Classless Inter-Domain Routing): This looked complicated at first, but the concept is simple. The CIDR block is the private IP address range for my VPC. - 10.0.0.0/16 is the range I chose. - 10.0.0.0 is the starting IP address. - The /16 is the "netmask." It defines the size of the network. A /16 network gives me a pool of 65,536 private IP addresses (10.0.0.0 to 10.0.255.255) for my resources. This is a very common size for a new VPC. - Tags: Tags are simple key-value labels that are essential for managing cloud resources. The Name tag is special because its value is what I see as the display name for the resource in the AWS Management Console. Tagging everything is a crucial best practice for keeping your infrastructure organized.


Deep Dive: Decoding the aws_vpc Resource

Understanding the structure of the Terraform code was key to this task. Here's my line-by-line breakdown for a complete beginner.

[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. It tells the provider which AWS
  # datacenter region to create my resources in.
  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.
  cidr_block = "10.0.0.0/16"

  # 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

- Invalid CIDR Block: If I had entered an invalid CIDR format (like 10.0.0.0) or a public IP range, Terraform would have returned an error from the AWS API during the plan or apply phase. - Overlapping CIDR Blocks: In a real-world scenario with multiple VPCs, if I tried to create a new VPC with a CIDR block that overlapped with an existing one, it would cause serious networking problems later on. - 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. For this task, it showed Plan: 1 to add, 0 to change, 0 to destroy. - 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.