Document

SUBSCRIBE TO GET FULL ACCESS TO THE E-BOOKS FOR FREE 🎁SUBSCRIBE NOW

Professional Dropdown with Icon

SUBSCRIBE NOW TO GET FREE ACCESS TO EBOOKS

Terraform Autoscaling

 

 

variables.tf

variable “count1” {
    default = 1
  }
variable “region” {
  description = “AWS region for hosting our your network”
  default = “us-east-1”
}

variable “key_name” {
  description = “Key name for SSHing into EC2”
  default = “newkey”
}
variable “amis” {
  description = “Base AMI to launch the instances”
 
  default = “ami-05fa00d4c63e32376”
 
}

main.tf


provider “aws” {
  region = “us-east-1”
}
data “aws_availability_zones” “all” {}
### Creating EC2 instance
resource “aws_instance” “web” {
  ami               = var.amis
  count             = ${var.count1}
  key_name               = ${var.key_name}
 
  source_dest_check = false
  instance_type = “t2.micro”

}
### Creating Security Group for EC2
resource “aws_security_group” “instance” {
  name = “terraform-example-instance”
  ingress {
    from_port = 8080
    to_port = 8080
    protocol = “tcp”
    cidr_blocks = [“0.0.0.0/0”]
  }
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = “tcp”
    cidr_blocks = [“0.0.0.0/0”]
  }
}
## Creating Launch Configuration
resource “aws_launch_configuration” “example” {
  image_id               = var.amis
  instance_type          = “t2.micro”
  security_groups        = [${aws_security_group.instance.id}]
  key_name               = ${var.key_name}
  user_data = <<-EOF
              #!/bin/bash
              echo “Hello, World” > index.html
              nohup busybox httpd -f -p 8080 &
              EOF
  lifecycle {
    create_before_destroy = true
  }
}
## Creating AutoScaling Group
resource “aws_autoscaling_group” “example” {
  launch_configuration = ${aws_launch_configuration.example.id}
  availability_zones = [“us-east-1a”]
  min_size = 2
  max_size = 10
  load_balancers = [${aws_elb.example.name}]
  health_check_type = “ELB”
  tag {
    key = “Name”
    value = “terraform-asg-example”
    propagate_at_launch = true
  }
}
## Security Group for ELB
resource “aws_security_group” “elb” {
  name = “terraform-example-elb”
  egress {
    from_port = 0
    to_port = 0
    protocol = “-1”
    cidr_blocks = [“0.0.0.0/0”]
  }
  ingress {
    from_port = 80
    to_port = 80
    protocol = “tcp”
    cidr_blocks = [“0.0.0.0/0”]
  }
}
### Creating ELB
resource “aws_elb” “example” {
  name = “terraform-asg-example”
  security_groups = [${aws_security_group.elb.id}]
  availability_zones = [“us-east-1a”]
  health_check {
    healthy_threshold = 2
    unhealthy_threshold = 2
    timeout = 3
    interval = 30
    target = “HTTP:8080/”
  }
  listener {
    lb_port = 80
    lb_protocol = “http”
    instance_port = “8080”
    instance_protocol = “http”
  }
}
Share your love

Leave a Reply

Your email address will not be published. Required fields are marked *