Terraform Series - Classic Loadbalancer

 


This post is all about automation of https://rsinfomindss.blogspot.com/2023/02/aws-classic-loadbalancer.html

I have created 4 TF Files:

1) Classiclb.tf - Contains Loadbalancer configuration.

2) Securitygroup.tf - Contains security group to allow incoming traffic on port 80.

3) Application_EC2.tf - Contains EC2 configuration.

4) appinstall.sh - Shell script to setup web server.

module "elb" {
  source  = "terraform-aws-modules/elb/aws"
  version = "4.0.1"
  name = "testlb"
  depends_on = [
    aws_instance.ec2demo
  ]
  subnets = ["subnet-0cfdbb9ad42fcf3c9"]
  security_groups = [module.loadbalancer_sg.this_security_group_id]
  #security_groups = ["sg-0495b116a9de10f0c"]
  internal = false
  listener = [
    {
      instance_port     = 80
      instance_protocol = "HTTP"
      lb_port           = 80
      lb_protocol       = "HTTP"
    }
]
 health_check = {
    target              = "HTTP:80/index.html"
    interval            = 30
    healthy_threshold   = 2
    unhealthy_threshold = 2
    timeout             = 5
  }
number_of_instances = 1
instances = [ aws_instance.ec2demo.id]
}


# Create a Security group to allow port 80
module "loadbalancer_sg" {
  source  = "terraform-aws-modules/security-group/aws"
  version = "3.18.0"
  name        = "loadbalancer-sg"
  description = "Security group with HTTP port open for everybody (IPv4 CIDR)"
  vpc_id      = "vpc-0fe3cacc96d8eca7b"
  # Ingress Rules & CIDR Block  
  ingress_rules = ["http-80-tcp"]
  ingress_cidr_blocks = ["0.0.0.0/0"]
  # Egress Rule - all-all open
  egress_rules = ["all-all"]
}
# Terraform Block
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}
# Provider Block
provider "aws" {
  profile = "default" # AWS Credentials configured via AWS CLI.
  region  = "us-east-1"
}

# Resource Block
resource "aws_instance" "ec2demo" {
  ami           = "ami-0b5eea76982371e91" # Amazon Linux in us-east-1
  instance_type = "t2.micro"
  user_data = file("${path.module}/appinstall.sh")
  subnet_id = "subnet-0cfdbb9ad42fcf3c9"
  security_groups = [ module.loadbalancer_sg.this_security_group_id]
  tags = {
    "Name" = "webserver1"
  }
}




#! /bin/bash
# Instance Identity Metadata Reference - https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
sudo yum update -y
sudo yum install -y httpd
sudo systemctl enable httpd
sudo service httpd start  
sudo echo '<h1>Welcome to My App - APP-1</h1>' | sudo tee /var/www/html/index.html
sudo mkdir /var/www/html/app1
sudo echo '<!DOCTYPE html> <html> <body style="background-color:rgb(250, 210, 210);"> <h1>Welcome to APP-1</h1> <p>Terraform Demo</p> <p>Application Version: V1</p> </body></html>' | sudo tee /var/www/html/app1/index.html
sudo curl http://169.254.169.254/latest/dynamic/instance-identity/document -o /var/www/html/app1/metadata.html


Comments

Popular posts from this blog

SRE/DevOps Syllabus

AWS Code Commit - CI/CD Series Part 1

Docker - Preventing IP overlapping