Need help with terraform-aws-autoscaling?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

terraform-aws-modules
199 Stars 355 Forks Apache License 2.0 90 Commits 4 Opened issues

Description

Terraform module which creates Auto Scaling resources on AWS

Services available

!
?

Need anything else?

Contributors list

AWS Auto Scaling Group (ASG) Terraform module

Terraform module which creates Auto Scaling resources on AWS.

Available features

  • Autoscaling group with launch configuration - either created by the module or utilizing an existing launch configuration
  • Autoscaling group with launch template - either created by the module or utilizing an existing launch template
  • Autoscaling group utilizing mixed instances policy
  • Ability to configure autoscaling groups to set instance refresh configuration and add lifecycle hooks

Usage

module "asg" {
  source  = "terraform-aws-modules/autoscaling/aws"
  version = "~> 4.0"

Autoscaling group

name = "example-asg"

min_size = 0 max_size = 1 desired_capacity = 1 wait_for_capacity_timeout = 0 health_check_type = "EC2" vpc_zone_identifier = ["subnet-1235678", "subnet-87654321"]

initial_lifecycle_hooks = [ { name = "ExampleStartupLifeCycleHook" default_result = "CONTINUE" heartbeat_timeout = 60 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" notification_metadata = jsonencode({ "hello" = "world" }) }, { name = "ExampleTerminationLifeCycleHook" default_result = "CONTINUE" heartbeat_timeout = 180 lifecycle_transition = "autoscaling:EC2_INSTANCE_TERMINATING" notification_metadata = jsonencode({ "goodbye" = "world" }) } ]

instance_refresh = { strategy = "Rolling" preferences = { min_healthy_percentage = 50 } triggers = ["tag"] }

Launch template

lt_name = "example-asg" description = "Launch template example" update_default_version = true

use_lt = true create_lt = true

image_id = "ami-ebd02392" instance_type = "t3.micro" ebs_optimized = true enable_monitoring = true

block_device_mappings = [ { # Root volume device_name = "/dev/xvda" no_device = 0 ebs = { delete_on_termination = true encrypted = true volume_size = 20 volume_type = "gp2" } }, { device_name = "/dev/sda1" no_device = 1 ebs = { delete_on_termination = true encrypted = true volume_size = 30 volume_type = "gp2" } } ]

capacity_reservation_specification = { capacity_reservation_preference = "open" }

cpu_options = { core_count = 1 threads_per_core = 1 }

credit_specification = { cpu_credits = "standard" }

instance_market_options = { market_type = "spot" spot_options = { block_duration_minutes = 60 } }

metadata_options = { http_endpoint = "enabled" http_tokens = "required" http_put_response_hop_limit = 32 }

network_interfaces = [ { delete_on_termination = true description = "eth0" device_index = 0 security_groups = ["sg-12345678"] }, { delete_on_termination = true description = "eth1" device_index = 1 security_groups = ["sg-12345678"] } ]

placement = { availability_zone = "us-west-1b" }

tag_specifications = [ { resource_type = "instance" tags = { WhatAmI = "Instance" } }, { resource_type = "volume" tags = { WhatAmI = "Volume" } }, { resource_type = "spot-instances-request" tags = { WhatAmI = "SpotInstanceRequest" } } ]

tags = [ { key = "Environment" value = "dev" propagate_at_launch = true }, { key = "Project" value = "megasecret" propagate_at_launch = true }, ]

tags_as_map = { extra_tag1 = "extra_value1" extra_tag2 = "extra_value2" } }

Conditional creation

The following combinations are supported to conditionally create resources and/or use externally created resources within the module:

  • Disable resource creation (no resources created):
  create_asg = false
  • Create only a launch configuration:
  create_asg = false
  create_lc  = true
  • Create only a launch template:
  create_asg = false
  create_lt  = true
  • Create both the autoscaling group and launch configuration:
  use_lc    = true
  create_lc = true
  • Create both the autoscaling group and launch template:
  use_lt    = true
  create_lt = true
  • Create the autoscaling group using an externally created launch configuration:
  use_lc               = true
  launch_configuration = aws_launch_configuration.my_launch_config.name
  • Create the autoscaling group using an externally created launch template:
  use_lt          = true
  launch_template = aws_launch_template.my_launch_template.name

Tags

There are two ways to specify tags for auto-scaling group in this module -

tags
and
tags_as_map
.
  tags = [
    {
      key                 = "Project"
      value               = "megasecret"
      propagate_at_launch = true
    },
    {
      key                 = "foo"
      value               = ""
      propagate_at_launch = true
    },
  ]

tags_as_map = { Owner = "user" Environment = "dev" }

Examples

  • Complete - Creates several variations of resources for autoscaling groups, launch templates, launch configurations.

Requirements

| Name | Version | |------|---------| | terraform | >= 0.13.1 | | aws | >= 3.53 | | null | >= 2.0 |

Providers

| Name | Version | |------|---------| | aws | >= 3.53 | | null | >= 2.0 |

Modules

No modules.

Resources

| Name | Type | |------|------| | awsautoscalinggroup.this | resource | | awsautoscalingschedule.this | resource | | awslaunchconfiguration.this | resource | | awslaunchtemplate.this | resource | | nullresource.tagsaslistof_maps | resource |

Inputs

| Name | Description | Type | Default | Required | |------|-------------|------|---------|:--------:| | associate_public_ip_address | (LC) Associate a public ip address with an instance in a VPC |

bool
|
null
| no | | availability_zone | A list of one or more availability zones for the group. Used for EC2-Classic and default subnets when not specified with
vpc_zone_identifier
argument. Conflicts with
vpc_zone_identifier
|
list(string)
|
null
| no | | block_device_mappings | (LT) Specify volumes to attach to the instance besides the volumes specified by the AMI |
list(any)
|
[]
| no | | capacity_rebalance | Indicates whether capacity rebalance is enabled |
bool
|
null
| no | | capacity_reservation_specification | (LT) Targeting for EC2 capacity reservations |
any
|
null
| no | | cpu_options | (LT) The CPU options for the instance |
map(string)
|
null
| no | | create_asg | Determines whether to create autoscaling group or not |
bool
|
true
| no | | create_lc | Determines whether to create launch configuration or not |
bool
|
false
| no | | create_lt | Determines whether to create launch template or not |
bool
|
false
| no | | create_schedule | Determines whether to create autoscaling group schedule or not |
bool
|
true
| no | | credit_specification | (LT) Customize the credit specification of the instance |
map(string)
|
null
| no | | default_cooldown | The amount of time, in seconds, after a scaling activity completes before another scaling activity can start |
number
|
null
| no | | default_version | (LT) Default Version of the launch template |
string
|
null
| no | | delete_timeout | Delete timeout to wait for destroying autoscaling group |
string
|
null
| no | | description | (LT) Description of the launch template |
string
|
null
| no | | desired_capacity | The number of Amazon EC2 instances that should be running in the autoscaling group |
number
|
null
| no | | disable_api_termination | (LT) If true, enables EC2 instance termination protection |
bool
|
null
| no | | ebs_block_device | (LC) Additional EBS block devices to attach to the instance |
list(map(string))
|
[]
| no | | ebs_optimized | If true, the launched EC2 instance will be EBS-optimized |
bool
|
null
| no | | elastic_gpu_specifications | (LT) The elastic GPU to attach to the instance |
map(string)
|
null
| no | | elastic_inference_accelerator | (LT) Configuration block containing an Elastic Inference Accelerator to attach to the instance |
map(string)
|
null
| no | | enable_monitoring | Enables/disables detailed monitoring |
bool
|
null
| no | | enabled_metrics | A list of metrics to collect. The allowed values are
GroupDesiredCapacity
,
GroupInServiceCapacity
,
GroupPendingCapacity
,
GroupMinSize
,
GroupMaxSize
,
GroupInServiceInstances
,
GroupPendingInstances
,
GroupStandbyInstances
,
GroupStandbyCapacity
,
GroupTerminatingCapacity
,
GroupTerminatingInstances
,
GroupTotalCapacity
,
GroupTotalInstances
|
list(string)
|
null
| no | | enclave_options | (LT) Enable Nitro Enclaves on launched instances |
map(string)
|
null
| no | | ephemeral_block_device | (LC) Customize Ephemeral (also known as 'Instance Store') volumes on the instance |
list(map(string))
|
[]
| no | | force_delete | 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 |
bool
|
null
| no | | health_check_grace_period | Time (in seconds) after instance comes into service before checking health |
number
|
null
| no | | health_check_type |
EC2
or
ELB
. Controls how health checking is done |
string
|
null
| no | | hibernation_options | (LT) The hibernation options for the instance |
map(string)
|
null
| no | | iam_instance_profile_arn | (LT) The IAM Instance Profile ARN to launch the instance with |
string
|
null
| no | | iam_instance_profile_name | The name attribute of the IAM instance profile to associate with launched instances |
string
|
null
| no | | image_id | The AMI from which to launch the instance |
string
|
""
| no | | initial_lifecycle_hooks | 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 |
list(map(string))
|
[]
| no | | instance_initiated_shutdown_behavior | (LT) Shutdown behavior for the instance. Can be
stop
or
terminate
. (Default:
stop
) |
string
|
null
| no | | instance_market_options | (LT) The market (purchasing) option for the instance |
any
|
null
| no | | instance_refresh | If this block is configured, start an Instance Refresh when this Auto Scaling Group is updated |
any
|
null
| no | | instance_type | The type of the instance to launch |
string
|
""
| no | | kernel_id | (LT) The kernel ID |
string
|
null
| no | | key_name | The key name that should be used for the instance |
string
|
null
| no | | launch_configuration | Name of an existing launch configuration to be used (created outside of this module) |
string
|
null
| no | | launch_template | Name of an existing launch template to be used (created outside of this module) |
string
|
null
| no | | lc_name | Name of launch configuration to be created |
string
|
""
| no | | lc_use_name_prefix | Determines whether to use
lc_name
as is or create a unique name beginning with the
lc_name
as the prefix |
bool
|
true
| no | | license_specifications | (LT) A list of license specifications to associate with |
map(string)
|
null
| no | | load_balancers | A 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 |
list(string)
|
[]
| no | | lt_name | Name of launch template to be created |
string
|
""
| no | | lt_use_name_prefix | Determines whether to use
lt_name
as is or create a unique name beginning with the
lt_name
as the prefix |
bool
|
true
| no | | lt_version | Launch template version. Can be version number,
$Latest
, or
$Default
|
string
|
null
| no | | max_instance_lifetime | The 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 |
number
|
null
| no | | max_size | The maximum size of the autoscaling group |
number
|
null
| no | | metadata_options | Customize the metadata options for the instance |
map(string)
|
null
| no | | metrics_granularity | The granularity to associate with the metrics to collect. The only valid value is
1Minute
|
string
|
null
| no | | min_elb_capacity | Setting this causes Terraform to wait for this number of instances to show up healthy in the ELB only on creation. Updates will not wait on ELB instance number changes |
number
|
null
| no | | min_size | The minimum size of the autoscaling group |
number
|
null
| no | | mixed_instances_policy | Configuration block containing settings to define launch targets for Auto Scaling groups |
any
|
null
| no | | name | Name used across the resources created |
string
| n/a | yes | | network_interfaces | (LT) Customize network interfaces to be attached at instance boot time |
list(any)
|
[]
| no | | placement | (LT) The placement of the instance |
map(string)
|
null
| no | | placement_group | The name of the placement group into which you'll launch your instances, if any |
string
|
null
| no | | placement_tenancy | (LC) The tenancy of the instance. Valid values are
default
or
dedicated
|
string
|
null
| no | | propagate_name | Determines whether to propagate the ASG Name tag or not |
bool
|
true
| no | | protect_from_scale_in | Allows setting instance protection. The autoscaling group will not select instances with this setting for termination during scale in events. |
bool
|
false
| no | | ram_disk_id | (LT) The ID of the ram disk |
string
|
null
| no | | root_block_device | (LC) Customize details about the root block device of the instance |
list(map(string))
|
[]
| no | | schedules | Map of autoscaling group schedule to create |
map(any)
|
{}
| no | | security_groups | A list of security group IDs to associate |
list(string)
|
null
| no | | service_linked_role_arn | The ARN of the service-linked role that the ASG will use to call other AWS services |
string
|
null
| no | | spot_price | (LC) The maximum price to use for reserving spot instances (defaults to on-demand price) |
string
|
null
| no | | suspended_processes | A list of processes to suspend for the Auto Scaling Group. The allowed values are
Launch
,
Terminate
,
HealthCheck
,
ReplaceUnhealthy
,
AZRebalance
,
AlarmNotification
,
ScheduledActions
,
AddToLoadBalancer
. Note that if you suspend either the
Launch
or
Terminate
process types, it can prevent your Auto Scaling Group from functioning properly |
list(string)
|
null
| no | | tag_specifications | (LT) The tags to apply to the resources during launch |
list(any)
|
[]
| no | | tags | A list of tag blocks. Each element should have keys named key, value, and propagate_at_launch |
list(map(string))
|
[]
| no | | tags_as_map | A map of tags and values in the same format as other resources accept. This will be converted into the non-standard format that the aws_autoscaling_group requires. |
map(string)
|
{}
| no | | target_group_arns | A set of
aws_alb_target_group
ARNs, for use with Application or Network Load Balancing |
list(string)
|
[]
| no | | termination_policies | A 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
|
list(string)
|
null
| no | | update_default_version | (LT) Whether to update Default Version each update. Conflicts with
default_version
|
string
|
null
| no | | use_lc | Determines whether to use a launch configuration in the autoscaling group or not |
bool
|
false
| no | | use_lt | Determines whether to use a launch template in the autoscaling group or not |
bool
|
false
| no | | use_mixed_instances_policy | Determines whether to use a mixed instances policy in the autoscaling group or not |
bool
|
false
| no | | use_name_prefix | Determines whether to use
name
as is or create a unique name beginning with the
name
as the prefix |
bool
|
true
| no | | user_data | (LC) The user data to provide when launching the instance. Do not pass gzip-compressed data via this argument nor when using Launch Templates; see
user_data_base64
instead |
string
|
null
| no | | user_data_base64 | The Base64-encoded user data to provide when launching the instance. You should use this for Launch Templates instead user_data |
string
|
null
| no | | vpc_zone_identifier | A list of subnet IDs to launch resources in. Subnets automatically determine which availability zones the group will reside. Conflicts with
availability_zones
|
list(string)
|
null
| no | | wait_for_capacity_timeout | A 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. |
string
|
null
| no | | wait_for_elb_capacity | Setting this will cause Terraform to wait for exactly this number of healthy instances in all attached load balancers on both create and update operations. Takes precedence over
min_elb_capacity
behavior. |
number
|
null
| no | | warm_pool | If this block is configured, add a Warm Pool to the specified Auto Scaling group |
any
|
null
| no |

Outputs

| Name | Description | |------|-------------| | autoscaling_group_arn | The ARN for this AutoScaling Group | | autoscaling_group_availability_zones | The availability zones of the autoscale group | | autoscaling_group_default_cooldown | Time between a scaling activity and the succeeding scaling activity | | autoscaling_group_desired_capacity | The number of Amazon EC2 instances that should be running in the group | | autoscaling_group_health_check_grace_period | Time after instance comes into service before checking health | | autoscaling_group_health_check_type | EC2 or ELB. Controls how health checking is done | | autoscaling_group_id | The autoscaling group id | | autoscaling_group_load_balancers | The load balancer names associated with the autoscaling group | | autoscaling_group_max_size | The maximum size of the autoscale group | | autoscaling_group_min_size | The minimum size of the autoscale group | | autoscaling_group_name | The autoscaling group name | | autoscaling_group_target_group_arns | List of Target Group ARNs that apply to this AutoScaling Group | | autoscaling_group_vpc_zone_identifier | The VPC zone identifier | | autoscaling_schedule_arns | ARNs of autoscaling group schedules | | launch_configuration_arn | The ARN of the launch configuration | | launch_configuration_id | The ID of the launch configuration | | launch_configuration_name | The name of the launch configuration | | launch_template_arn | The ARN of the launch template | | launch_template_id | The ID of the launch template | | launch_template_latest_version | The latest version of the launch template | <!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

Authors

Module is maintained by Anton Babenko with help from these awesome contributors.

License

Apache 2 Licensed. See LICENSE for full details.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.