Skip to main content

Command Palette

Search for a command to run...

Day 66 : Building AWS Infrastructure with Terraform: A Step by Step Guide

Updated
3 min read

Task: Create a VPC (Virtual Private Cloud) with CIDR block 10.0.0.0/16

Step 1: Create a new directory for your Terraform configuration files.

Step 2: Inside the directory, create a new file named terraform.tf.

Step 3: In the terraform.tf file, add the following code:

terraform {
    required_providers {
      aws = {
        source  = "hashicorp/aws"
        version = "~> 4.0"
      }
    }
  }

Now create vpc.tf

provider "aws" {
  region = "us-east-1"
}

resource "aws_vpc" "main" {
    cidr_block       = "10.0.0.0/16"
    tags = {
      Name = "ajay-vpc"
    }
}

Step 4: Save the vpc.tf file.

Step 5: Initialize the Terraform working directory by running the following command in your terminal:

Step 6: plan and apply the Terraform configuration to create the VPC by running the following command:

Terraform will then create the VPC with the specified CIDR block (10.0.0.0/16) using the terraform-aws-modules/vpc/aws module.

Task : 2 Create a public subnet with CIDR block 10.0.1.0/24 in the above VPC.

create public_subnet.tf

resource "aws_subnet" "public_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
  availability_zone = "us-east-1a"


  tags = {
    Name = "Public Subnet"
  }
}

now terraform plan and apply

Task 3 : Create a private subnet with CIDR block 10.0.2.0/24 in the above VPC.

create private_subnet.tf

resource "aws_subnet" "private_subnet" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.2.0/24"
  availability_zone = "us-east-1a"

  tags = {
    Name = "Private Subnet"
  }
}

Task : 4 Create an Internet Gateway (IGW) and attach it to the VPC.

create internet_gateway.tf

resource "aws_internet_gateway" "igw" {
  vpc_id = aws_vpc.main.id

  tags = {
    Name = "My IGW"
  }
}

Task 5 : Create a route table for the public subnet and associate it with the public subnet. This route table should have a route to the Internet Gateway.

create public_route_table.tf

resource "aws_route_table" "public_route_table" {
  vpc_id = aws_vpc.main.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw.id
  }

  tags = {
    Name = "Public Route Table"
  }
}

resource "aws_route_table_association" "public_subnet_association" {
  subnet_id      = aws_subnet.public_subnet.id
  route_table_id = aws_route_table.public_route_table.id
}

Task : 6 Launch an EC2 instance in the public subnet with the following details:

  • AMI: ami-024e6efaf93d85776

  • Instance type: t2.micro

  • Security group: Allow SSH access from anywhere

  • User data: Use a shell script to install Apache and host a simple website

  • Create an Elastic IP and associate it with the EC2 instance.

create ec2_security_group.tf

resource "aws_security_group" "ec2_security_group" {
  name        = "EC2 Security Group"
  description = "Allow SSH access"
  vpc_id      = aws_vpc.main.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  ingress {
     from_port   = 443
     to_port     = 443
     protocol    = "tcp"
     cidr_blocks = ["0.0.0.0/0"]
   }

  egress {
     from_port   = 0
     to_port     = 0
     protocol    = "-1"
     cidr_blocks = ["0.0.0.0/0"]
   }
}

create ec2.tf

resource "aws_instance" "ec2_instance" {
    ami           = "ami-0557a15b87f6559cf"
    instance_type = "t2.micro"
    key_name      = "Nginx-keypair"
    subnet_id     = aws_subnet.public_subnet.id
    vpc_security_group_ids = [aws_security_group.ec2_security_group.id]
    associate_public_ip_address = true

     user_data = <<-EOF
       #!/bin/bash
       sudo apt-get update -y
       sudo apt-get install apache2 -y
       sudo systemctl start apache2
       sudo systemctl enable apache2
       sudo systemctl restart apache2
       sudo chmod 766 /var/www/html/index.html
       sudo echo "<html><body><h1>Welcome to my website.</h1></body></html>" >/var/www/html/index.html
    EOF
    tags = {
      Name = "Terraform-Infra"
    }
  }

craete elastic_ip.tf

resource "aws_eip" "elastic_ip" {
  instance = aws_instance.ec2_instance.id

  tags = {
    Name = "My Elastic IP"
  }
}

Open the website URL in a browser to verify that the website is hosted successfully.

This will be the files structure for whole folder

That was all for today. If you have any queries/suggestions please write in comment. see you another day another challenge.

More from this blog

Ajay Patel

116 posts