# Day 68 - Scaling with Terraform

Before proceeding, we have to set up AWS Provider, Region, VPC, InternetGateway, Security Group, and RouteTable.

Prerequisite : If you have not setup terraform please follow below blog:

%[https://hashnode.com/post/cljbzmnad000109mv11753vyu] 

1. create `terraform.tf`
    

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

1. 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"
    }
}
```

1. 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"
  }
}

resource "aws_subnet" "public_subnet_1b" {
   vpc_id     = aws_vpc.main.id
   cidr_block = "10.0.3.0/24"
   availability_zone = "us-east-1b"
   tags = {
     Name = "public-subnet-2"
   }
 }
```

1. create `internet_gateway.tf`
    

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

  tags = {
    Name = "My IGW"
  }
}
```

1. 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
}

resource "aws_route_table_association" "public_subnet_association_1b" {
    subnet_id      = aws_subnet.public_subnet_1b.id
    route_table_id = aws_route_table.public_route_table.id
}
```

1. 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"]
   }
}
```

1. create `as_group.tf`
    

```bash
resource "aws_launch_configuration" "web_server_as" {
    image_id           = "ami-053b0d53c279acc90"
    instance_type = "t2.micro"
    security_groups = [aws_security_group.ec2_security_group.id]

    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 Terraform Scaling.</h1></body></html>" >/var/www/html/index.html
   EOF
  }

  resource "aws_elb" "web_server_lb"{
     name = "web-server-lb"
     security_groups = [aws_security_group.ec2_security_group.id]
     subnets = [aws_subnet.public_subnet.id,aws_subnet.public_subnet_1b.id]
     listener {
      instance_port     = 80
      instance_protocol = "http"
      lb_port           = 80
      lb_protocol       = "http"
    }
    tags = {
      Name = "terraform-elb"
    }
  }

  resource "aws_autoscaling_group" "web_server_asg" {
    name                 = "web-server-asg"
    launch_configuration = aws_launch_configuration.web_server_as.name
    min_size             = 1
    max_size             = 3
    desired_capacity     = 2
    health_check_type    = "EC2"
    load_balancers       = [aws_elb.web_server_lb.name]
    vpc_zone_identifier  = [aws_subnet.public_subnet.id, aws_subnet.public_subnet_1b.id]
  }
```

Now run `terraform init` then `terraform plan`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688101102648/9bb8021f-8edd-4c77-a54e-ab9266ae3e31.png align="center")

then `terraform apply`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688101146500/b8bb6405-5ab5-45ef-88ad-b6d39af31cce.png align="center")

Once Terraform is done with execution two new EC2 instances are created as desired capacity is set to 2.

You will see 1 auto scaling group named web-server-asg and 1 load balancer named web-server-lb

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688101281113/2b4db0a1-391e-4173-ab3e-0b8089bd39ec.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688101325153/ed9108d9-29df-4a2b-ad61-4afb2d67db85.png align="center")

Now if you check your load balancer DNS URL:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1688103770741/a1b523b1-64c6-49da-8e36-06a2da03ed02.png align="center")

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