terraform-aws-modules Auto Scaling Group

repository·master·Indexed 43 days ago

https://github.com/terraform-aws-modules/terraform-aws-autoscaling

A Terraform module for creating and managing AWS Auto Scaling Groups (ASG), including launch templates, IAM roles, scaling policies, and instance refresh configurations. Supports mixed instance policies, warm pools, Elastic Fabric Adapter (EFA), and lifecycle hooks. Requires Terraform >= 1.5.7 and AWS Provider >= 6.56.

Tokens
13.1K
Snippets
29
Records
57
Agent score
55%

What's inside terraform-aws-autoscaling

  1. Understand the different Auto Scaling Group configurations

    master

    The complete example demonstrates eight distinct configuration patterns for using this module:

    • disabled: Completely disables the creation of both the ASG and the associated launch template.
    • template only: Creates a launch template without creating an ASG.
    • default: Uses the bare minimum information required to create an ASG, relying on default values.
    • external: Uses an existing, externally created launch template for the ASG.
    • complete: Demonstrates the full range of available module functionality.
    • mixed instance: Configures the ASG to use a mixed instance policy.
    • warm pool: Demonstrates using warm pools with capacity reservations.
    • efa: Demonstrates using Elastic Fabric Adapter (EFA) type network interfaces.
  2. Control resource creation modes

    master

    The module allows for conditional resource creation to support different deployment patterns:

    • Disable all resources: Set create = false and create_launch_template = false.
    • Create only a launch template: Set create = false.
    • Use an existing launch template: Set create_launch_template = false and provide the name via launch_template = aws_launch_template.my_launch_template.name.
    • Use Mixed Instance Policy: Set use_mixed_instances_policy = true.
  3. Use `ignore_desired_capacity_changes` to manage ASG desired capacity

    master

    In v5.x, a new feature allows you to create an Auto Scaling Group (ASG) that ignores changes to the desired_capacity setting. This is useful when you want to use an ASG that is managed by other scaling mechanisms (like predictive scaling or external controllers) without Terraform attempting to revert the capacity to the value defined in your configuration.

    To use this, set ignore_desired_capacity_changes = true.

    State Migration for ignore_desired_capacity_changes

    If you are upgrading from a version where you had a standard ASG and want to switch to the idc (ignore desired capacity) resource type in your state, you must perform a terraform state mv command.

    Command:

    terraform state mv 'module.<xxx>.aws_autoscaling_group.this[0]' 'module.<xxx>.aws_autoscaling_group.idc[0]'
    module "example" {
      source  = "terraform-aws-modules/autoscaling/aws"
      version = "~> 5.x"
    
      # <your configs>
    
      ignore_desired_capacity_changes = true
    }
  4. Triggering launch template refreshes

    master
    When using an external launch template, setting launch_template_version to $Latest will prevent Terraform from initiating a refresh when the external template changes. To ensure the Auto Scaling Group refreshes when the external launch template is updated, set launch_template_version to the specific latest_version of the aws_launch_template resource instead.
  5. Use Instance Requirements instead of Instance Type

    master

    Instead of specifying a fixed instance_type, you can use instance_requirements to define the attributes your instances must have.

    Note: You cannot use both instance_type and instance_requirements in the same configuration.

    Supported requirements include:

    • cpu_manufacturers, vcpu_count, memory_mib (with min/max)
    • network_bandwidth_gbps, network_interface_count
    • accelerator_count, accelerator_types, accelerator_names
    • local_storage, local_storage_types, total_local_storage_gb
    • ebs_bandwidth_mbps
    • allowed_instance_types, excluded_instance_types
    • burstable_performance, bare_metal, require_hibernate_support
  6. Use the module wrapper for managing multiple instances

    master
    The wrappers module implements a single module wrapper pattern. This allows you to manage multiple instances of the root Auto Scaling module within a single configuration block. This is particularly useful in environments like Terragrunt where native Terraform for_each might not be feasible, or when you want to manage multiple resources without duplicating configuration files.
  7. Use Instance Refresh instead of ASG Recreate (v4.x)

    master

    In v3.x, recreating the ASG was often forced using a random_pet resource and the recreate_asg_when_lc_changes variable. In v4.x, this is replaced by the native AWS instance_refresh functionality.

    By default, an instance refresh is triggered by changes to launch_configuration, launch_template, or mixed_instances_policy. To trigger a refresh on additional changes, add the relevant attribute to the triggers list. This allows instances to be replaced without destroying and recreating the entire Auto Scaling Group.

    instance_refresh = {
      strategy = "Rolling"
      preferences = {
        min_healthy_percentage = 50
      }
      triggers = ["tag"]
    }
  8. Migrate state for `ignore_desired_capacity_changes` feature

    master

    If you enable ignore_desired_capacity_changes = true in your v5.x configuration, you may need to move your existing Auto Scaling Group resource in the Terraform state from the this resource to the idc resource to avoid recreation.

    Example Migration Command:

    terraform state mv 'module.example.aws_autoscaling_group.this[0]' 'module.example.aws_autoscaling_group.idc[0]'
  9. Migrate traffic source attachments from v7.x to v8.x

    master

    In version 8.x, the module replaced individual traffic source variables with a single map variable called traffic_source_attachments. This change allows for multiple attachments, whereas v7.x only supported one.

    Backwards Incompatible Changes:

    • target_group_arns and load_balancers are no longer supported on the ASG.
    • Use traffic_source_attachments to manage load balancer or target group attachments.
    • The minimum supported Terraform version is now v1.3.

    Variable Changes:

    • Removed: load_balancers, target_group_arns, create_traffic_source_attachment, traffic_source_identifier, and traffic_source_type.
    • Added: traffic_source_attachments (a map of objects).

    Output Changes:

    • Removed: launch_configuration_id, launch_configuration_arn, and launch_configuration_name.
    ### Before 7.x Example
    ```hcl
    module "asg" {
      source  = "terraform-aws-modules/autoscaling/aws"
      version = "~> 7.0"
    
      # Truncated for brevity
    
      # Traffic source attachment
      create_traffic_source_attachment = true
      traffic_source_identifier        = "arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/app/my-load-balancer/1234567890123456"
      traffic_source_type              = "elbv2"
    
      ...
    }

    After 8.x Example

    module "asg" {
      source  = "terraform-aws-modules/autoscaling/aws"
      version = "~> 8.0"
    
      # Truncated for brevity
    
      traffic_source_attachments = {
        ex-alb = {
          traffic_source_identifier = "arn:aws:elasticloadbalancing:us-east-2:123456789012:loadbalancer/app/my-load-balancer/1234567890123456"
          traffic_source_type       = "elbv2"
        }
        ...
      }
      ...
    }
  10. Configure the wrapper module with Terraform

    master

    To use the wrapper directly in Terraform, call the module and provide defaults and items arguments. The items map keys define the unique identifiers for each instance, and the values are the configuration arguments for the root module.

    module "wrapper" {
      source = "terraform-aws-modules/autoscaling/aws//wrappers"
    
      defaults = {
        create = true
        tags = {
          Terraform   = "true"
          Environment = "dev"
        }
      }
    
      items = {
        my-item = {
          # can be any argument supported by the root module
        }
        my-second-item = {
          # can be any argument supported by the root module
        }
      }
    }
  11. Migrate from v3.x to v4.x

    master

    When upgrading from version 3.x to 4.x, be aware that due to the default use of name_prefix, most upgrade paths will require recreating the resources provisioned by the module.

    Launch Configuration Configuration

    If you want the module to create the launch configuration (the most common use case), add these variables:

      lc_name   = "module-will-create"
      use_lc    = true
      create_lc = true

    If you are providing an externally created launch configuration, use:

      lc_name   = "externally-created"
      use_lc    = true
      # For module-managed launch configuration
      lc_name   = "module-will-create"
      use_lc    = true
      create_lc = true
    
      # OR for externally managed launch configuration
      # lc_name   = "externally-created"
      # use_lc    = true