Skip to content

Instantly share code, notes, and snippets.

@SharmilaS22
Created June 18, 2023 15:10
Show Gist options
  • Select an option

  • Save SharmilaS22/1591b4d744b2c6157b7f4554ce5087eb to your computer and use it in GitHub Desktop.

Select an option

Save SharmilaS22/1591b4d744b2c6157b7f4554ce5087eb to your computer and use it in GitHub Desktop.
Autoscaling group with launch templates for creating EC2 instances
# ASG with Launch template
resource "aws_launch_template" "sh_ec2_launch_templ" {
name_prefix = "sh_ec2_launch_templ"
image_id = "ami-00c39f71452c08778" # To note: AMI is specific for each region
instance_type = "t2.micro"
user_data = filebase64("user_data.sh")
network_interfaces {
associate_public_ip_address = false
subnet_id = aws_subnet.sh_subnet_2.id
security_groups = [aws_security_group.sh_sg_for_ec2.id]
}
tag_specifications {
resource_type = "instance"
tags = {
Name = "Sharmi-instance" # Name for the EC2 instances
}
}
}
resource "aws_autoscaling_group" "sh_asg" {
# no of instances
desired_capacity = 1
max_size = 1
min_size = 1
# Connect to the target group
target_group_arns = [aws_lb_target_group.sh_alb_tg.arn]
vpc_zone_identifier = [ # Creating EC2 instances in private subnet
aws_subnet.sh_subnet_2.id
]
launch_template {
id = aws_launch_template.sh_ec2_launch_templ.id
version = "$Latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment