CloudWiki

Amazon Web Service (AWS)

Auto Scaling Group

Compute
Auto Scaling Group (ASG) is a logical collection of EC2 instances used for management and scaling purposes. With Auto Scaling Group, you can use EC2 Auto Scaling features such as health check replacements and scaling policies to improve fault tolerance and increase availability with the right amount of compute capacity.
aws_autoscaling_group
Auto Scaling Group
attributes:

  • name - (Optional) Name of the Auto Scaling Group. By default generated by Terraform. Conflicts with name_prefix.
  • name_prefix - (Optional) Creates a unique name beginning with the specified prefix. Conflicts with name.
  • max_size - (Required) Maximum size of the Auto Scaling Group.
  • min_size - (Required) Minimum size of the Auto Scaling Group. (See also Waiting for Capacity below.)
  • availability_zones - (Optional) List of one or more availability zones for the group. Used for EC2-Classic, attaching a network interface via id from a launch template and default subnets when not specified with vpc_zone_identifier argument. Conflicts with vpc_zone_identifier.
  • capacity_rebalance - (Optional) Whether capacity rebalance is enabled. Otherwise, capacity rebalance is disabled.
  • context - (Optional) Reserved.
  • default_cooldown - (Optional) Amount of time, in seconds, after a scaling activity completes before another scaling activity can start.
  • default_instance_warmup - (Optional) Amount of time, in seconds, until a newly launched instance can contribute to the Amazon CloudWatch metrics. This delay lets an instance finish initializing before Amazon EC2 Auto Scaling aggregates instance metrics, resulting in more reliable usage data. Set this value equal to the amount of time that it takes for resource consumption to become stable after an instance reaches the InService state. (See Set the default instance warmup for an Auto Scaling group)
  • launch_configuration - (Optional) Name of the launch configuration to use.
  • launch_template - (Optional) Nested argument with Launch template specification to use to launch instances. See Launch Template below for more details.
  • mixed_instances_policy (Optional) Configuration block containing settings to define launch targets for Auto Scaling groups. See Mixed Instances Policy below for more details.
  • initial_lifecycle_hook - (Optional) One or more Lifecycle Hooks to attach to the Auto Scaling Group before instances are launched. The syntax is exactly the same as the separate aws_autoscaling_lifecycle_hook resource, without the autoscaling_group_name attribute. Please note that this will only work when creating a new Auto Scaling Group. For all other use-cases, please use aws_autoscaling_lifecycle_hook resource.
  • health_check_grace_period - (Optional, Default: 300) Time (in seconds) after instance comes into service before checking health.
  • health_check_type - (Optional) "EC2" or "ELB". Controls how health checking is done.
  • desired_capacity - (Optional) Number of Amazon EC2 instances that should be running in the group. (See also Waiting for Capacity below.)
  • force_delete - (Optional) Allows deleting the Auto Scaling Group without waiting for all instances in the pool to terminate. You can force an Auto Scaling Group to delete even if it's in the process of scaling a resource. Normally, Terraform drains all the instances before deleting the group. This bypasses that behavior and potentially leaves resources dangling.
  • load_balancers (Optional) List of elastic load balancer names to add to the autoscaling group names. Only valid for classic load balancers. For ALBs, use target_group_arns instead.
  • vpc_zone_identifier (Optional) List of subnet IDs to launch resources in. Subnets automatically determine which availability zones the group will reside. Conflicts with availability_zones.
  • target_group_arns (Optional) Set of aws_alb_target_group ARNs, for use with Application or Network Load Balancing.
  • termination_policies (Optional) List of policies to decide how the instances in the Auto Scaling Group should be terminated. The allowed values are OldestInstance, NewestInstance, OldestLaunchConfiguration, ClosestToNextInstanceHour, OldestLaunchTemplate, AllocationStrategy, Default. Additionally, the ARN of a Lambda function can be specified for custom termination policies.
  • suspended_processes - (Optional) List of processes to suspend for the Auto Scaling Group. The allowed values are Launch, Terminate, HealthCheck, ReplaceUnhealthy, AZRebalance, AlarmNotification, ScheduledActions, AddToLoadBalancer, InstanceRefresh. Note that if you suspend either the Launch or Terminate process types, it can prevent your Auto Scaling Group from functioning properly.
  • tag (Optional) Configuration block(s) containing resource tags. Conflicts with tags. See Tag below for more details.
  • tags (Optional, Deprecated use tag instead) Set of maps containing resource tags. Conflicts with tag. See Tags below for more details.
  • placement_group (Optional) Name of the placement group into which you'll launch your instances, if any.
  • metrics_granularity - (Optional) Granularity to associate with the metrics to collect. The only valid value is 1Minute. Default is 1Minute.
  • enabled_metrics - (Optional) List of metrics to collect. The allowed values are defined by the underlying AWS API.
  • wait_for_capacity_timeout (Default: "10m") Maximum duration that Terraform should wait for ASG instances to be healthy before timing out. (See also Waiting for Capacity below.) Setting this to "0" causes Terraform to skip all Capacity Waiting behavior.
  • min_elb_capacity - (Optional) Setting this causes Terraform to wait for this number of instances from this Auto Scaling Group to show up healthy in the ELB only on creation. Updates will not wait on ELB instance number changes. (See also Waiting for Capacity below.)
  • wait_for_elb_capacity - (Optional) Setting this will cause Terraform to wait for exactly this number of healthy instances from this Auto Scaling Group in all attached load balancers on both create and update operations. (Takes precedence over min_elb_capacity behavior.) (See also Waiting for Capacity below.)
  • protect_from_scale_in (Optional) Whether newly launched instances are automatically protected from termination by Amazon EC2 Auto Scaling when scaling in. For more information about preventing instances from terminating on scale in, see Using instance scale-in protection in the Amazon EC2 Auto Scaling User Guide.
  • service_linked_role_arn (Optional) ARN of the service-linked role that the ASG will use to call other AWS services
  • max_instance_lifetime (Optional) Maximum amount of time, in seconds, that an instance can be in service, values must be either equal to 0 or between 86400 and 31536000 seconds.
  • instance_refresh - (Optional) If this block is configured, start an Instance Refresh when this Auto Scaling Group is updated. Defined below.
  • warm_pool - (Optional) If this block is configured, add a Warm Pool to the specified Auto Scaling group. Defined below

launch_template

NOTE:

Either id or name must be specified.

The top-level launch_template block supports the following:

  • id - (Optional) ID of the launch template. Conflicts with name.
  • name - (Optional) Name of the launch template. Conflicts with id.
  • version - (Optional) Template version. Can be version number, $Latest, or $Default. (Default: $Default).

mixed_instances_policy

  • instances_distribution - (Optional) Nested argument containing settings on how to mix on-demand and Spot instances in the Auto Scaling group. Defined below.
  • launch_template - (Required) Nested argument containing launch template settings along with the overrides to specify multiple instance types and weights. Defined below.

mixed_instances_policy instances_distribution

This configuration block supports the following:

  • on_demand_allocation_strategy - (Optional) Strategy to use when launching on-demand instances. Valid values: prioritized. Default: prioritized.
  • on_demand_base_capacity - (Optional) Absolute minimum amount of desired capacity that must be fulfilled by on-demand instances. Default: 0.
  • on_demand_percentage_above_base_capacity - (Optional) Percentage split between on-demand and Spot instances above the base on-demand capacity. Default: 100.
  • spot_allocation_strategy - (Optional) How to allocate capacity across the Spot pools. Valid values: lowest-price, capacity-optimized, capacity-optimized-prioritized. Default: lowest-price.
  • spot_instance_pools - (Optional) Number of Spot pools per availability zone to allocate capacity. EC2 Auto Scaling selects the cheapest Spot pools and evenly allocates Spot capacity across the number of Spot pools that you specify. Only available with spot_allocation_strategy set to lowest-price. Otherwise it must be set to 0, if it has been defined before. Default: 2.
  • spot_max_price - (Optional) Maximum price per unit hour that the user is willing to pay for the Spot instances. Default: an empty string which means the on-demand price.

mixed_instances_policy launch_template

This configuration block supports the following:

  • launch_template_specification - (Required) Nested argument defines the Launch Template. Defined below.
  • override - (Optional) List of nested arguments provides the ability to specify multiple instance types. This will override the same parameter in the launch template. For on-demand instances, Auto Scaling considers the order of preference of instance types to launch based on the order specified in the overrides list. Defined below.
mixed_instances_policy launch_template launch_template_specification

NOTE:

Either launch_template_id or launch_template_name must be specified.

This configuration block supports the following:

  • launch_template_id - (Optional) ID of the launch template. Conflicts with launch_template_name.
  • launch_template_name - (Optional) Name of the launch template. Conflicts with launch_template_id.
  • version - (Optional) Template version. Can be version number, $Latest, or $Default. (Default: $Default).
mixed_instances_policy launch_template override

This configuration block supports the following:

  • instance_type - (Optional) Override the instance type in the Launch Template.
  • instance_requirements - (Optional) Override the instance type in the Launch Template with instance types that satisfy the requirements.
  • launch_template_specification - (Optional) Override the instance launch template specification in the Launch Template.
  • weighted_capacity - (Optional) Number of capacity units, which gives the instance type a proportional weight to other instance types.
mixed_instances_policy launch_template override instance_requirements

This configuration block supports the following:

NOTE:

Both memory_mib.min and vcpu_count.min must be specified.

  • accelerator_count - (Optional) Block describing the minimum and maximum number of accelerators (GPUs, FPGAs, or AWS Inferentia chips). Default is no minimum or maximum.
  • min - (Optional) Minimum.
  • max - (Optional) Maximum. Set to 0 to exclude instance types with accelerators.
  • accelerator_manufacturers - (Optional) List of accelerator manufacturer names. Default is any manufacturer.
  • Valid names:
     * amazon-web-services
     * amd
     * nvidia
     * xilinx
  • accelerator_names - (Optional) List of accelerator names. Default is any acclerator.
  • Valid names:
     * a100            - NVIDIA A100 GPUs
     * v100            - NVIDIA V100 GPUs
     * k80             - NVIDIA K80 GPUs
     * t4              - NVIDIA T4 GPUs
     * m60             - NVIDIA M60 GPUs
     * radeon-pro-v520 - AMD Radeon Pro V520 GPUs
     * vu9p            - Xilinx VU9P FPGAs
  • accelerator_total_memory_mib - (Optional) Block describing the minimum and maximum total memory of the accelerators. Default is no minimum or maximum.
  • min - (Optional) Minimum.
  • max - (Optional) Maximum.
  • accelerator_types - (Optional) List of accelerator types. Default is any accelerator type.
  • Valid types:
     * fpga
     * gpu
     * inference
  • bare_metal - (Optional) Indicate whether bare metal instace types should be included, excluded, or required. Default is excluded.
  • baseline_ebs_bandwidth_mbps - (Optional) Block describing the minimum and maximum baseline EBS bandwidth, in Mbps. Default is no minimum or maximum.
  • min - (Optional) Minimum.
  • max - (Optional) Maximum.
  • burstable_performance - (Optional) Indicate whether burstable performance instance types should be included, excluded, or required. Default is excluded.
  • cpu_manufacturers (Optional) List of CPU manufacturer names. Default is any manufacturer.
  • NOTE:
  • Don't confuse the CPU hardware manufacturer with the CPU hardware architecture. Instances will be launched with a compatible CPU architecture based on the Amazon Machine Image (AMI) that you specify in your launch template.
  • Valid names:
     * amazon-web-services
     * amd
     * intel
  • excluded_instance_types - (Optional) List of instance types to exclude. You can use strings with one or more wild cards, represented by an asterisk (*). The following are examples: c5*, m5a.*, r*, *3*. For example, if you specify c5*, you are excluding the entire C5 instance family, which includes all C5a and C5n instance types. If you specify m5a.*, you are excluding all the M5a instance types, but not the M5n instance types. Maximum of 400 entries in the list; each entry is limited to 30 characters. Default is no excluded instance types.
  • instance_generations - (Optional) List of instance generation names. Default is any generation.
  • Valid names:
     * current  - Recommended for best performance.
     * previous - For existing applications optimized for older instance types.
  • local_storage - (Optional) Indicate whether instance types with local storage volumes are included, excluded, or required. Default is included.
  • local_storage_types - (Optional) List of local storage type names. Default any storage type.
  • Value names:
     * hdd - hard disk drive
     * ssd - solid state drive
  • memory_gib_per_vcpu - (Optional) Block describing the minimum and maximum amount of memory (GiB) per vCPU. Default is no minimum or maximum.
  • min - (Optional) Minimum. May be a decimal number, e.g. 0.5.
  • max - (Optional) Maximum. May be a decimal number, e.g. 0.5.
  • memory_mib - (Required) Block describing the minimum and maximum amount of memory (MiB). Default is no maximum.
  • min - (Required) Minimum.
  • max - (Optional) Maximum.
  • network_interface_count - (Optional) Block describing the minimum and maximum number of network interfaces. Default is no minimum or maximum.
  • min - (Optional) Minimum.
  • max - (Optional) Maximum.
  • on_demand_max_price_percentage_over_lowest_price - (Optional) Price protection threshold for On-Demand Instances. This is the maximum you’ll pay for an On-Demand Instance, expressed as a percentage higher than the cheapest M, C, or R instance type with your specified attributes. When Amazon EC2 Auto Scaling selects instance types with your attributes, we will exclude instance types whose price is higher than your threshold. The parameter accepts an integer, which Amazon EC2 Auto Scaling interprets as a percentage. To turn off price protection, specify a high value, such as 999999. Default is 20.
  • If you set DesiredCapacityType to vcpu or memory-mib, the price protection threshold is applied based on the per vCPU or per memory price instead of the per instance price.
  • require_hibernate_support - (Optional) Indicate whether instance types must support On-Demand Instance Hibernation, either true or false. Default is false.
  • spot_max_price_percentage_over_lowest_price - (Optional) Price protection threshold for Spot Instances. This is the maximum you’ll pay for a Spot Instance, expressed as a percentage higher than the cheapest M, C, or R instance type with your specified attributes. When Amazon EC2 Auto Scaling selects instance types with your attributes, we will exclude instance types whose price is higher than your threshold. The parameter accepts an integer, which Amazon EC2 Auto Scaling interprets as a percentage. To turn off price protection, specify a high value, such as 999999. Default is 100.
  • If you set DesiredCapacityType to vcpu or memory-mib, the price protection threshold is applied based on the per vCPU or per memory price instead of the per instance price.
  • total_local_storage_gb - (Optional) Block describing the minimum and maximum total local storage (GB). Default is no minimum or maximum.
  • min - (Optional) Minimum. May be a decimal number, e.g. 0.5.
  • max - (Optional) Maximum. May be a decimal number, e.g. 0.5.
  • vcpu_count - (Required) Block describing the minimum and maximum number of vCPUs. Default is no maximum.
  • min - (Required) Minimum.
  • max - (Optional) Maximum.

tag and tags

The tag attribute accepts exactly one tag declaration with the following fields:

  • key - (Required) Key
  • value - (Required) Value
  • propagate_at_launch - (Required) Enables propagation of the tag to Amazon EC2 instances launched via this ASG

To declare multiple tags additional tag blocks can be specified. Alternatively the tags attributes can be used, which accepts a list of maps containing the above field names as keys and their respective values. This allows the construction of dynamic lists of tags which is not possible using the single tag attribute. tag and tags are mutually exclusive, only one of them can be specified.

NOTE:

Other AWS APIs may automatically add special tags to their associated Auto Scaling Group for management purposes, such as ECS Capacity Providers adding the AmazonECSManaged tag. These generally should be included in the configuration so Terraform does not attempt to remove them and so if the min_size was greater than zero on creation, that these tag(s) are applied to any initial EC2 Instances in the Auto Scaling Group. If these tag(s) were missing in the Auto Scaling Group configuration on creation, affected EC2 Instances missing the tags may require manual intervention of adding the tags to ensure they work properly with the other AWS service.

instance_refresh

This configuration block supports the following:

  • strategy - (Required) Strategy to use for instance refresh. The only allowed value is Rolling. See StartInstanceRefresh Action for more information.
  • preferences - (Optional) Override default parameters for Instance Refresh.
  • checkpoint_delay - (Optional) Number of seconds to wait after a checkpoint. Defaults to 3600.
  • checkpoint_percentages - (Optional) List of percentages for each checkpoint. Values must be unique and in ascending order. To replace all instances, the final number must be 100.
  • instance_warmup - (Optional) Number of seconds until a newly launched instance is configured and ready to use. Default behavior is to use the Auto Scaling Group's health check grace period.
  • min_healthy_percentage - (Optional) Amount of capacity in the Auto Scaling group that must remain healthy during an instance refresh to allow the operation to continue, as a percentage of the desired capacity of the Auto Scaling group. Defaults to 90.
  • triggers - (Optional) Set of additional property names that will trigger an Instance Refresh. A refresh will always be triggered by a change in any of launch_configuration, launch_template, or mixed_instances_policy.

NOTE:

A refresh is started when any of the following Auto Scaling Group properties change: launch_configuration, launch_template, mixed_instances_policy. Additional properties can be specified in the triggers property of instance_refresh.

NOTE:

A refresh will not start when version = "$Latest" is configured in the launch_template block. To trigger the instance refresh when a launch template is changed, configure version to use the latest_version attribute of the aws_launch_template resource.

NOTE:

Auto Scaling Groups support up to one active instance refresh at a time. When this resource is updated, any existing refresh is cancelled.

NOTE:

Depending on health check settings and group size, an instance refresh may take a long time or fail. This resource does not wait for the instance refresh to complete.

warm_pool

This configuration block supports the following:

  • pool_state - (Optional) Sets the instance state to transition to after the lifecycle hooks finish. Valid values are: Stopped (default), Running or Hibernated.
  • min_size - (Optional) Minimum number of instances to maintain in the warm pool. This helps you to ensure that there is always a certain number of warmed instances available to handle traffic spikes. Defaults to 0 if not specified.
  • instance_reuse_policy - (Optional) Whether instances in the Auto Scaling group can be returned to the warm pool on scale in. The default is to terminate instances in the Auto Scaling group when the group scales in.
  • max_group_prepared_capacity - (Optional) Total maximum number of instances that are allowed to be in the warm pool or in any state except Terminated for the Auto Scaling group.
instance_reuse_policy

This configuration block supports the following:

  • reuse_on_scale_in - (Optional) Whether instances in the Auto Scaling group can be returned to the warm pool on scale in.

Associating resources with a
Auto Scaling Group
Resources do not "belong" to a
Auto Scaling Group
Rather, one or more Security Groups are associated to a resource.
Create
Auto Scaling Group
via Terraform:
The following HCL creates an Auto Scaling Group
Syntax:

resource "aws_placement_group" "test" {
 name     = "test"
 strategy = "cluster"
}

resource "aws_autoscaling_group" "bar" {
 name                      = "foobar3-terraform-test"
 max_size                  = 5
 min_size                  = 2
 health_check_grace_period = 300
 health_check_type         = "ELB"
 desired_capacity          = 4
 force_delete              = true
 placement_group           = aws_placement_group.test.id
 launch_configuration      = aws_launch_configuration.foobar.name
 vpc_zone_identifier       = [aws_subnet.example1.id, aws_subnet.example2.id]

 initial_lifecycle_hook {
   name                 = "foobar"
   default_result       = "CONTINUE"
   heartbeat_timeout    = 2000
   lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"

   notification_metadata = <<EOF
{
 "foo": "bar"
}
EOF

   notification_target_arn = "arn:aws:sqs:us-east-1:444455556666:queue1*"
   role_arn                = "arn:aws:iam::123456789012:role/S3Access"
 }

 tag {
   key                 = "foo"
   value               = "bar"
   propagate_at_launch = true
 }

 timeouts {
   delete = "15m"
 }

 tag {
   key                 = "lorem"
   value               = "ipsum"
   propagate_at_launch = false
 }
}

Create
Auto Scaling Group
via CLI:
Parametres:

create-auto-scaling-group
--auto-scaling-group-name <value>
[--launch-configuration-name <value>]
[--launch-template <value>]
[--mixed-instances-policy <value>]
[--instance-id <value>]
--min-size <value>
--max-size <value>
[--desired-capacity <value>]
[--default-cooldown <value>]
[--availability-zones <value>]
[--load-balancer-names <value>]
[--target-group-arns <value>]
[--health-check-type <value>]
[--health-check-grace-period <value>]
[--placement-group <value>]
[--vpc-zone-identifier <value>]
[--termination-policies <value>]
[--new-instances-protected-from-scale-in | --no-new-instances-protected-from-scale-in]
[--capacity-rebalance | --no-capacity-rebalance]
[--lifecycle-hook-specification-list <value>]
[--tags <value>]
[--service-linked-role-arn <value>]
[--max-instance-lifetime <value>]
[--context <value>]
[--desired-capacity-type <value>]
[--default-instance-warmup <value>]
[--cli-input-json | --cli-input-yaml]
[--generate-cli-skeleton <value>]
[--debug]
[--endpoint-url <value>]
[--no-verify-ssl]
[--no-paginate]
[--output <value>]
[--query <value>]
[--profile <value>]
[--region <value>]
[--version <value>]
[--color <value>]
[--no-sign-request]
[--ca-bundle <value>]
[--cli-read-timeout <value>]
[--cli-connect-timeout <value>]
[--cli-binary-format <value>]
[--no-cli-pager]
[--cli-auto-prompt]
[--no-cli-auto-prompt]

Example:

aws autoscaling create-auto-scaling-group \
   --auto-scaling-group-name my-asg \
   --launch-template LaunchTemplateId=lt-1234567890abcde12 \
   --min-size 1 \
   --max-size 5 \
   --vpc-zone-identifier "subnet-5ea0c127,subnet-6194ea3b,subnet-c934b782"

Best Practices for
Auto Scaling Group

Categorized by Availability, Security & Compliance and Cost

No items found.
Explore all the rules our platform covers
All Resources