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

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687958805244/d58c692d-52c9-4962-b985-2939f36e370f.png align="center")

Step 2: Inside the directory, create a new file named [`terraform.tf`](http://main.tf).

Step 3: In the [`terraform.tf`](http://main.tf) file, add the following code:

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

Now create `vpc.tf`

```bash
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`](http://main.tf) file.

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959107677/bba00b46-a34a-485c-9504-dbabef64a05b.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959307487/ec85484d-d012-4224-81d9-fcd09eb1477c.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959271330/22c67913-5d1d-46b8-b1aa-1b9622bea1e1.png align="center")

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`

```bash
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`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959705648/8d87f4e5-22ee-4e60-8a2b-e32d16867b5e.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959717251/a9d88b5b-37d4-4bb9-aa10-323e3783cd0c.png align="center")

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

create private\_subnet.tf

```bash
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"
  }
}
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959949327/a01884cb-4237-4382-98d6-f941e3b1f8da.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687959961604/1f499965-53cc-422d-9491-31288b47332e.png align="center")

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

create internet\_gateway.tf

```bash
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

```bash
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`

```bash
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`

```bash
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`

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687961394620/4c7823e7-1d85-4234-8692-57a2acccb835.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687961410600/b2f19dcc-d915-4af5-ad71-b165cead2fa6.png align="center")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687971474950/df1339cd-7360-471f-99cf-00be0653ee1e.png align="center")

This will be the files structure for whole folder

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1687972021593/258c6223-8233-425f-a439-d6824182c37f.png align="center")

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