Lab #3 [Scenario-1]: How to Create VPC using Terraform | AWS VPC using Terraform | AWS Terraform

3 min read 2 hours ago
Published on Oct 17, 2024 This response is partially generated with the help of AI. It may contain inaccuracies.

Table of Contents

Introduction

This tutorial provides a step-by-step guide on how to create a Virtual Private Cloud (VPC) using Terraform on AWS. It is designed for beginners and will walk you through the necessary components, including VPC creation, subnet configuration, internet gateway setup, and launching EC2 instances. By following these steps, you will gain practical experience in managing AWS resources using Terraform.

Step 1: Set Up Your Terraform Environment

  1. Install Terraform: Download and install Terraform from the official site.
  2. Configure AWS CLI: Ensure you have the AWS Command Line Interface (CLI) installed and configured with your AWS credentials.
  3. Create a New Directory: This will hold your Terraform configuration files.
    mkdir terraform-vpc
    cd terraform-vpc
    

Step 2: Create the VPC Configuration

  1. Create a main.tf file: This file will contain all your Terraform code.

    touch main.tf
    
  2. Add the AWS Provider: Specify the AWS region where you want to create your resources.

    provider "aws" {
      region = "ap-south-1"
    }
    
  3. Define the VPC Resource: Set up the VPC with your desired configurations.

    resource "aws_vpc" "dev" {
      cidr_block           = "10.0.0.0/16"
      instance_tenancy     = "default"
      enable_dns_support   = "true"
      enable_dns_hostnames = "true"
      enable_classiclink   = "false"
      tags = {
        Name = "dev"
      }
    }
    

Step 3: Create Public Subnets

  1. Add Public Subnet Resources: Define two public subnets within the VPC.
    resource "aws_subnet" "dev-public-1" {
      vpc_id                  = aws_vpc.dev.id
      cidr_block              = "10.0.1.0/24"
      map_public_ip_on_launch = "true"
      availability_zone       = "ap-south-1a"
      tags = {
        Name = "dev-public-1"
      }
    }
    
    resource "aws_subnet" "dev-public-2" {
      vpc_id                  = aws_vpc.dev.id
      cidr_block              = "10.0.2.0/24"
      map_public_ip_on_launch = "true"
      availability_zone       = "ap-south-1b"
      tags = {
        Name = "dev-public-2"
      }
    }
    

Step 4: Set Up Internet Gateway

  1. Define the Internet Gateway Resource: This allows public internet access to your VPC.
    resource "aws_internet_gateway" "dev-gw" {
      vpc_id = aws_vpc.dev.id
      tags = {
        Name = "dev"
      }
    }
    

Step 5: Create Route Tables

  1. Add a Route Table for Public Subnets: Ensure traffic can flow from the public subnets to the internet.

    resource "aws_route_table" "dev-public" {
      vpc_id = aws_vpc.dev.id
      route {
        cidr_block = "0.0.0.0/0"
        gateway_id = aws_internet_gateway.dev-gw.id
      }
      tags = {
        Name = "dev-public"
      }
    }
    
  2. Associate the Route Table with Subnets:

    resource "aws_route_table_association" "dev-public-1-a" {
      subnet_id      = aws_subnet.dev-public-1.id
      route_table_id = aws_route_table.dev-public.id
    }
    
    resource "aws_route_table_association" "dev-public-2-a" {
      subnet_id      = aws_subnet.dev-public-2.id
      route_table_id = aws_route_table.dev-public.id
    }
    

Step 6: Launch EC2 Instances

  1. Define EC2 Instance Resources: Create instances in the public subnets.
    resource "aws_instance" "public_inst_1" {
      ami           = "ami-0c1a7f89451184c8b"
      instance_type = "t2.micro"
      subnet_id = aws_subnet.dev-public-1.id
      key_name = "key11"
      tags = {
        Name = "public_inst_1"
      }
    }
    
    resource "aws_instance" "public_inst_2" {
      ami           = "ami-0c1a7f89451184c8b"
      instance_type = "t2.micro"
      subnet_id = aws_subnet.dev-public-2.id
      key_name = "key11"
      tags = {
        Name = "public_inst_2"
      }
    }
    

Step 7: Initialize Terraform and Apply Configuration

  1. Initialize Terraform: This downloads the necessary provider plugins.

    terraform init
    
  2. Plan Your Deployment: Review the changes Terraform will make.

    terraform plan
    
  3. Apply the Configuration: Create the resources defined in your configuration.

    terraform apply
    

Conclusion

You have successfully created a VPC using Terraform on AWS, including public subnets and EC2 instances. Make sure to monitor your resources in the AWS Management Console and clean up by running terraform destroy when you're done experimenting. This practice will enhance your understanding of infrastructure as code and the capability of Terraform in managing cloud environments.