DEV Community

Grit Coding
Grit Coding

Posted on

Building Infrastructure from Scratch: Creating a VPC with Terraform

Image description

Embarking on a journey to build cloud infrastructure from scratch can be both exciting and daunting. In this series, we focus on leveraging Infrastructure as Code (IaC) tools like Terraform to set up essential components in the cloud. To start, let's set up a Virtual Private Cloud (VPC) in AWS using Terraform.

Prerequisites

Before we start, you'll need to have a few tools and access ready:

Step1: Crafting the main.tf File

Our first step is to create a main.tf file. This file will serve as the foundation of our Terraform configuration. Here’s a breakdown of each section in the script:

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"  // Specifies the provider source, here it's AWS provided by HashiCorp.
      version = "~> 4.16"        
    }
  }

  required_version = ">= 1.2.0"   // Ensures that Terraform version 1.2.0 or higher is used.
}

provider "aws" {
  region  = "us-east-1"            // Sets the AWS region where the resources will be created. This can be changed as needed.
}

variable "environment" {
  description = "The deployment environment (e.g., dev, qa, prod)"
  type        = string             // Defines a string variable for the deployment environment.

  validation {
    condition     = contains(["dev", "qa", "prod"], var.environment)
    error_message = "The environment must be one of: dev, qa, or prod."  // Validates the environment variable value.
  }
}

variable "env_cidr_map" {
  description = "Map of environment names to CIDR block second octet"
  type        = map(string)        // Defines a map variable, mapping environment names to parts of the CIDR block.
  default = {
    "dev"     = "0"
    "qa"      = "10"
    "prod"    = "20"
  }
}

resource "aws_vpc" "main" {
  cidr_block = "10.${lookup(var.env_cidr_map, var.environment, "0")}.0.0/16" // Sets the VPC CIDR block dynamically based on the environment. 
  // check the 'Learn More' section for details.
  assign_generated_ipv6_cidr_block = true  // Enables the assignment of an IPv6 CIDR block to the VPC.
}

Enter fullscreen mode Exit fullscreen mode

Step 2: Deploying AWS VPC

In this section, we'll walk through the steps to deploy your AWS VPC using Terraform.

Initialise Terraform

terraform init
Enter fullscreen mode Exit fullscreen mode

Image description

Apply the Terraform Configuration

terraform apply
Enter fullscreen mode Exit fullscreen mode

We have set a custom condition for the environment variable. If you provide a value that is not specified in the contains function, an error message will be triggered.

Image description

Let's enter dev, and then confirm the action by typing yes.

Image description

Verify VPC Creation

Once you receive the completion message, refresh the VPC section in the AWS console to check if a new VPC has been created.

Image description

As you can see, the CIDR block for IPv4 is 10.0.0.0/16 which is mapped correctly with the environment value dev in the lookup function.

Clean Up Resources

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Image description

Learn More

For those who prefer hands-on learning, you can fork and explore the source code from this repository: GitHub.

If you found this guide or the repository useful, a star or a reaction would be much appreciated.😄

For further reading on AWS & Terraform documentation, refer to:

Top comments (0)