Skip to content

This Terraform module creates a highly customizable AWS EC2 Auto Scaling Group (ASG), primarily using Launch Templates (recommended) but also supporting legacy Launch Configurations.

License

Notifications You must be signed in to change notification settings

jhermesn/AWS_AS_Module.tf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AWS EC2 Auto Scaling Module in Terraform

This Terraform module creates a highly customizable AWS EC2 Auto Scaling Group (ASG), primarily using Launch Templates (recommended) but also supporting legacy Launch Configurations. It facilitates the setup of scalable and resilient application tiers on AWS.

Features

  • Launch Template Support: Creates or uses existing Launch Templates (preferred method).
  • Launch Configuration Support: Creates or uses existing Launch Configurations (legacy).
  • Flexible Scaling: Defines min/max/desired instance counts.
  • Health Checks: Supports EC2 and ELB health checks with configurable grace periods.
  • ELB Integration: Easily attaches instances to Classic Load Balancers (via load_balancers) or Application/Network Load Balancer Target Groups (via target_group_arns).
  • Scaling Policies: Supports Simple, Step, and Target Tracking scaling policies via a map input (scaling_policies).
  • Scheduled Actions: Defines scheduled scaling actions based on time or cron expressions (scheduled_actions).
  • Lifecycle Hooks: Configures hooks for custom actions during instance launch or termination (lifecycle_hooks).
  • Instance Refresh: Manages rolling updates and replacements based on Launch Template changes (instance_refresh).
  • Warm Pools: Pre-initializes instances for faster scale-out (warm_pool).
  • Notifications: Sends ASG event notifications to an SNS topic.
  • Customization: Extensive input variables for fine-tuning Launch Templates/Configurations and ASG behavior.
  • Tagging: Applies consistent tagging to the ASG and optionally propagates tags to instances.

Prerequisites

  • Terraform v1.11.0 or later.
  • AWS Provider configured with appropriate credentials.
  • Existing VPC Subnets (provide IDs via vpc_zone_identifier).
  • Existing Security Groups (provide IDs via lt_security_group_ids).
  • Optionally, an existing AMI (lt_image_id), IAM Instance Profile (lt_iam_instance_profile_arn/name), SNS Topic (notification_topic_arn), ELB Target Groups (target_group_arns).

Inputs

(Refer to variables.tf for detailed descriptions, types, defaults, and validation rules for all inputs.)

Core Configuration:

Name Description Required Default
name_prefix Unique prefix for resource names. Yes n/a
vpc_zone_identifier List of subnet IDs for the ASG. Yes n/a
min_size Minimum number of instances. Yes n/a
max_size Maximum number of instances. Yes n/a
desired_capacity Desired number of instances (defaults to min). No null
tags Map of tags applied to all resources. No {}

Launch Template Configuration:

Name Description Required Default
lt_image_id AMI ID for instances (required if creating new LT/LC). Yes* n/a
lt_instance_type Instance type to use (required if creating new LT/LC). Yes* n/a
lt_security_group_ids List of security group IDs for instances. No []
lt_metadata_options IMDSv2 settings (default: IMDSv2 required, hop limit 1). No {}
lt_root_block_device Root volume configuration (size, type, encryption). No {}
notification_types List of event types to send to SNS (default: all available types). No ["autoscaling:EC2_INSTANCE_LAUNCH", "autoscaling:EC2_INSTANCE_TERMINATE", ...]

Outputs

Name Description
autoscaling_group_id The ID of the Auto Scaling Group.
autoscaling_group_name The name of the Auto Scaling Group.
autoscaling_group_arn The ARN of the Auto Scaling Group.
launch_template_id The ID of the Launch Template created or used (if applicable).
launch_template_arn The ARN of the Launch Template created or used (if applicable).
launch_template_latest_version The latest version number of the Launch Template (if applicable).
launch_configuration_name The name of the Launch Configuration created or used (if applicable).
scaling_policy_arns Map of scaling policy names to their ARNs.
scheduled_action_arns Map of scheduled action names to their ARNs.
lifecycle_hook_names Map of lifecycle hook input keys to their actual AWS names.

Usage

module "web_asg" {
  source = "github.com/jhermesn/AWS_AS_Module.tf"

  name_prefix        = "web-app"
  vpc_zone_identifier = ["subnet-abc123", "subnet-def456", "subnet-ghi789"]
  min_size           = 2
  max_size           = 10
  desired_capacity   = 3
  health_check_type  = "ELB"

  # Launch Template configuration
  use_launch_template     = true
  create_launch_template  = true
  lt_image_id             = "ami-0123456789abcdef0"
  lt_instance_type        = "t3.medium"
  lt_security_group_ids   = ["sg-abc123"]
  lt_key_name             = "my-key-pair"
  lt_user_data_base64     = base64encode(<<-EOF
    #!/bin/bash
    echo "Hello World" > /var/www/html/index.html
    systemctl start httpd
    EOF
  )

  # Root volume configuration
  lt_root_block_device = {
    volume_size = 30
    volume_type = "gp3"
    encrypted   = true
  }

  # Load balancer integration
  target_group_arns = ["arn:aws:elasticloadbalancing:us-west-2:123456789012:targetgroup/my-targets/abc123"]

  # Simple scaling policy example
  scaling_policies = {
    cpu-high = {
      policy_type        = "TargetTrackingScaling"
      target_tracking_configuration = {
        predefined_metric_specification = {
          predefined_metric_type = "ASGAverageCPUUtilization"
        }
        target_value = 70.0
      }
    }
  }

  tags = {
    Environment = "production"
    Project     = "web-app"
  }
}

License

This project is licensed under the MIT License.

About

This Terraform module creates a highly customizable AWS EC2 Auto Scaling Group (ASG), primarily using Launch Templates (recommended) but also supporting legacy Launch Configurations.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages