README

ec2-autoscaling

Creates an EC2 Auto Scaling Group with a versioned launch template, target-tracking scaling policies, lifecycle hooks, and an optional warm pool.

Provisions a launch template (IMDSv2, encrypted root volume, detailed monitoring), an ASG with configurable min/max/desired, target-tracking CPU scaling, ASG lifecycle hooks for draining and warming, ALB/NLB target group registration, and optional warm pool for reduced scale-out latency.

Usage

module "asg" {
  source  = "hcassc.jfrog.io/iac-terraform-modules-virtual/compute/ec2-autoscaling/aws"
  version = "0.1.0"

  name                     = "prod-app-asg"
  ami_id                   = data.aws_ami.app.id
  instance_type            = "m5.large"
  security_group_ids       = [module.security_groups.ids["app"]]
  vpc_zone_identifier      = module.vpc.private_subnet_id_list
  iam_instance_profile_arn = module.iam_role.instance_profile_arn
  target_group_arns        = [module.load_balancer.target_group_arns["app"]]
  max_size                 = 10
}

Requirements

Name Version
terraform ~> 1.5
aws ~> 6.50

Providers

Name Version
aws 6.50.0

Resources

Name Type
aws_autoscaling_group.this resource
aws_autoscaling_lifecycle_hook.this resource
aws_autoscaling_policy.this resource
aws_launch_template.this resource

Inputs

Name Description Type Default Required
ami_id AMI ID for the launch template string n/a yes
instance_type EC2 instance type string n/a yes
name Name prefix for the ASG and launch template string n/a yes
security_group_ids Security group IDs for launch template list(string) n/a yes
vpc_zone_identifier List of subnet IDs for the ASG list(string) n/a yes
desired_capacity Desired number of instances number 1 no
health_check_type EC2 or ELB health check type string "EC2" no
iam_instance_profile_arn ARN of the IAM instance profile string null no
key_name EC2 key pair name string null no
lifecycle_hooks Map of lifecycle hooks map(object({ lifecycle_transition = string heartbeat_timeout = optional(number, 60) default_result = optional(string, "CONTINUE") notification_arn = optional(string, null) })) {} no
max_size Maximum number of instances number 3 no
metadata_http_tokens IMDSv2 setting — required or optional string "required" no
min_size Minimum number of instances number 1 no
root_volume_kms_key_id KMS key ID for root volume encryption string null no
root_volume_size Root EBS volume size in GiB number 20 no
scaling_policies Map of target-tracking scaling policies map(object({ predefined_metric_type = optional(string, "ASGAverageCPUUtilization") target_value = optional(number, 60) cooldown = optional(number, 300) })) {} no
tags Resource tags to apply to all resources map(string) {} no
target_group_arns ALB/NLB target group ARNs to associate list(string) [] no
user_data Base64-encoded user data string null no
warm_pool_enabled Enable warm pool for faster scale-out bool false no
warm_pool_min_size Minimum number of instances in the warm pool number 1 no

Outputs

Name Description
asg_arn Auto Scaling Group ARN
asg_id Auto Scaling Group name (ID)
launch_template_arn Launch template ARN
launch_template_id Launch template ID
launch_template_latest_version Latest launch template version number