Terraform Project

Terraform Project

Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC) Techniques(Interview Questions) ☁

  1. Create a main.tf file as shown below...

     terraform {
       required_providers {
         aws = {
           source  = "hashicorp/aws"
           version = "~> 5.0"
         }
       }
     }
    
     provider "aws" {
       region = "us-east-1"
     }
    
     resource "aws_vpc" "main" {
       cidr_block = "10.0.0.0/16"
    
       tags = {
         Name = "main"
       }
     }
    
     resource "aws_subnet" "public" {
       vpc_id            = aws_vpc.main.id
       cidr_block        = "10.0.16.0/20"
       availability_zone = "us-east-1a"
    
       tags = {
         Name = "public"
       }
     }
    
     resource "aws_subnet" "private" {
       vpc_id            = aws_vpc.main.id
       cidr_block        = "10.0.32.0/20"
       availability_zone = "us-east-1a"
    
       tags = {
         Name = "private"
       }
     }
    
     resource "aws_internet_gateway" "gw" {
       vpc_id = aws_vpc.main.id
    
       tags = {
         Name = "internet-gateway"
       }
     }
    
     resource "aws_route_table" "public" {
       vpc_id = aws_vpc.main.id
    
       route {
         cidr_block = "0.0.0.0/0"
         gateway_id = aws_internet_gateway.gw.id
       }
    
       tags = {
         Name = "public"
       }
     }
    
     resource "aws_route_table_association" "public_subnet_association" {
       subnet_id      = aws_subnet.public.id
       route_table_id = aws_route_table.public.id
     }
    
     resource "aws_security_group" "web_server" {
       name_prefix = "web-server-sg"
       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"]
       }
     }
    
     resource "aws_eip" "eip" {
       instance = aws_instance.example.id
    
       tags = {
         Name = "test-eip"
       }
     }
    
     resource "aws_instance" "example" {
       ami           = "ami-053b0d53c279acc90"
       instance_type = "t2.micro"
       subnet_id     = aws_subnet.public.id
    
       associate_public_ip_address = true
       security_groups = [aws_security_group.web_server.id]
    
       tags = {
         Name = "Terraform-Infra"
       }
    
       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>Hare Krishna!<br>Welcome to my website.</h1></body></html>" >/var/www/html/index.html
       EOF
     }
    
  2. Run Terraform Commands:

    Open your terminal and navigate to the directory containing your Terraform configuration file. Run the following commands:

    • terraform init (to initialize your Terraform working directory)

    • terraform apply (to create the resources as defined in your configuration)

  3. You can then access your website by entering the public IP address in a web browser to verify that the website is hosted successfully.


Thank you so much for reading

Follow me on LinkedIn to see interesting posts like this : )

Linkedin