2014-07-24 05:34:59 +02:00
---
layout: "aws"
page_title: "AWS: aws_autoscaling_group"
2015-05-30 16:42:24 +02:00
sidebar_current: "docs-aws-resource-autoscaling-group"
2014-10-22 05:21:56 +02:00
description: |-
Provides an AutoScaling Group resource.
2014-07-24 05:34:59 +02:00
---
# aws\_autoscaling\_group
Provides an AutoScaling Group resource.
## Example Usage
```
2015-11-02 16:44:29 +01:00
resource "aws_placement_group" "test" {
2016-11-14 21:06:00 +01:00
name = "test"
2015-11-02 16:44:29 +01:00
strategy = "cluster"
}
2014-07-24 05:34:59 +02:00
resource "aws_autoscaling_group" "bar" {
2016-11-14 21:06:00 +01:00
availability_zones = ["us-east-1a"]
name = "foobar3-terraform-test"
max_size = 5
min_size = 2
2014-07-24 05:34:59 +02:00
health_check_grace_period = 300
2016-11-14 21:06:00 +01:00
health_check_type = "ELB"
desired_capacity = 4
force_delete = true
placement_group = "${aws_placement_group.test.id}"
launch_configuration = "${aws_launch_configuration.foobar.name}"
initial_lifecycle_hook {
2017-02-18 23:48:50 +01:00
name = "foobar"
default_result = "CONTINUE"
heartbeat_timeout = 2000
lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING"
2016-11-14 21:06:00 +01:00
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"
}
2015-03-03 00:40:47 +01:00
tag {
2016-11-14 21:06:00 +01:00
key = "foo"
value = "bar"
2015-03-03 00:40:47 +01:00
propagate_at_launch = true
}
2016-11-14 21:06:00 +01:00
2015-03-03 00:40:47 +01:00
tag {
2016-11-14 21:06:00 +01:00
key = "lorem"
value = "ipsum"
2015-03-03 00:40:47 +01:00
propagate_at_launch = false
}
2014-07-24 05:34:59 +02:00
}
```
## Argument Reference
The following arguments are supported:
2015-10-31 10:24:46 +01:00
* `name` - (Optional) The name of the auto scale group. By default generated by terraform.
2014-07-24 05:34:59 +02:00
* `max_size` - (Required) The maximum size of the auto scale group.
2015-05-14 19:45:21 +02:00
* `min_size` - (Required) The minimum size of the auto scale group.
(See also [Waiting for Capacity ](#waiting-for-capacity ) below.)
2015-07-14 17:24:16 +02:00
* `availability_zones` - (Optional) A list of AZs to launch resources in.
Required only if you do not specify any `vpc_zone_identifier`
2017-02-15 11:46:17 +01:00
* `default_cooldown` - (Optional) The amount of time, in seconds, after a scaling activity completes before another scaling activity can start.
2015-08-17 16:24:25 +02:00
* `launch_configuration` - (Required) The name of the launch configuration to use.
2016-11-14 21:06:00 +01:00
* `initial_lifecycle_hook` - (Optional) One or more
[Lifecycle Hooks ](http://docs.aws.amazon.com/autoscaling/latest/userguide/lifecycle-hooks.html )
to attach to the autoscaling group **before** instances are launched. The
syntax is exactly the same as the separate
[`aws_autoscaling_lifecycle_hook` ](/docs/providers/aws/r/autoscaling_lifecycle_hooks.html )
2017-02-01 15:26:27 +01:00
resource, without the `autoscaling_group_name` attribute. Please note that this will only work when creating
a new autoscaling group. For all other use-cases, please use `aws_autoscaling_lifecycle_hook` resource.
2016-03-24 00:55:37 +01:00
* `health_check_grace_period` - (Optional, Default: 300) Time (in seconds) after instance comes into service before checking health.
2014-07-24 05:34:59 +02:00
* `health_check_type` - (Optional) "EC2" or "ELB". Controls how health checking is done.
2015-05-07 01:34:20 +02:00
* `desired_capacity` - (Optional) The number of Amazon EC2 instances that
2015-05-14 19:45:21 +02:00
should be running in the group. (See also [Waiting for
Capacity](#waiting-for-capacity) below.)
2014-07-24 05:34:59 +02:00
* `force_delete` - (Optional) Allows deleting the autoscaling group without waiting
2015-10-12 22:50:07 +02:00
for all instances in the pool to terminate. You can force an autoscaling 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.
2015-01-14 18:28:25 +01:00
* `load_balancers` (Optional) A list of load balancer names to add to the autoscaling
2014-08-20 19:40:43 +02:00
group names.
2015-01-14 18:28:25 +01:00
* `vpc_zone_identifier` (Optional) A list of subnet IDs to launch resources in.
2016-09-23 05:21:03 +02:00
* `target_group_arns` (Optional) A list of `aws_alb_target_group` ARNs, for use with
2016-08-19 21:07:53 +02:00
Application Load Balancing
2016-05-17 14:27:33 +02:00
* `termination_policies` (Optional) A list of policies to decide how the instances in the auto scale group should be terminated. The allowed values are `OldestInstance` , `NewestInstance` , `OldestLaunchConfiguration` , `ClosestToNextInstanceHour` , `Default` .
2016-11-21 16:02:20 +01:00
* `suspended_processes` - (Optional) A list of processes to suspend for the AutoScaling 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 autoscaling group from functioning properly.
2015-03-03 00:40:47 +01:00
* `tag` (Optional) A list of tag blocks. Tags documented below.
2015-11-02 16:44:29 +01:00
* `placement_group` (Optional) The name of the placement group into which you'll launch your instances, if any.
2016-03-18 19:22:57 +01:00
* `metrics_granularity` - (Optional) The granularity to associate with the metrics to collect. The only valid value is `1Minute` . Default is `1Minute` .
2016-05-08 19:03:24 +02:00
* `enabled_metrics` - (Optional) A list of metrics to collect. The allowed values are `GroupMinSize` , `GroupMaxSize` , `GroupDesiredCapacity` , `GroupInServiceInstances` , `GroupPendingInstances` , `GroupStandbyInstances` , `GroupTerminatingInstances` , `GroupTotalInstances` .
2015-09-08 20:15:30 +02:00
* `wait_for_capacity_timeout` (Default: "10m") A maximum
[duration ](https://golang.org/pkg/time/#ParseDuration ) that Terraform should
wait for ASG instances to be healthy before timing out. (See also [Waiting
for Capacity](#waiting-for-capacity) below.) Setting this to "0" causes
Terraform to skip all Capacity Waiting behavior.
2016-01-27 16:55:10 +01:00
* `min_elb_capacity` - (Optional) 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.
(See also [Waiting for Capacity ](#waiting-for-capacity ) below.)
2015-11-17 01:16:22 +01:00
* `wait_for_elb_capacity` - (Optional) Setting this will cause Terraform to wait
2016-01-27 16:55:10 +01:00
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.)
2015-11-17 01:16:22 +01:00
(See also [Waiting for Capacity ](#waiting-for-capacity ) below.)
2016-06-29 08:36:34 +02:00
* `protect_from_scale_in` (Optional) Allows setting instance protection. The
autoscaling group will not select instances with this setting for terminination
during scale in events.
2015-03-03 00:40:47 +01:00
Tags support the following:
* `key` - (Required) Key
* `value` - (Required) Value
* `propagate_at_launch` - (Required) Enables propagation of the tag to
Amazon EC2 instances launched via this ASG
2014-07-24 05:34:59 +02:00
## Attributes Reference
The following attributes are exported:
2016-09-30 22:41:15 +02:00
* `id` - The autoscaling group id.
2016-08-27 16:20:11 +02:00
* `arn` - The ARN for this AutoScaling Group
2014-07-24 05:34:59 +02:00
* `availability_zones` - The availability zones of the autoscale group.
* `min_size` - The minimum size of the autoscale group
* `max_size` - The maximum size of the autoscale group
* `default_cooldown` - Time between a scaling activity and the succeeding scaling activity.
* `name` - The name of the autoscale group
* `health_check_grace_period` - Time after instance comes into service before checking health.
* `health_check_type` - "EC2" or "ELB". Controls how health checking is done.
2014-07-29 08:51:24 +02:00
* `desired_capacity` -The number of Amazon EC2 instances that should be running in the group.
2014-07-24 05:34:59 +02:00
* `launch_configuration` - The launch configuration of the autoscale group
2016-03-11 07:41:25 +01:00
* `vpc_zone_identifier` (Optional) - The VPC zone identifier
2014-08-20 19:40:43 +02:00
* `load_balancers` (Optional) The load balancer names associated with the
autoscaling group.
2016-08-19 21:07:53 +02:00
* `target_group_arns` (Optional) list of Target Group ARNs that apply to this
AutoScaling Group
2015-11-17 01:16:22 +01:00
2016-11-14 21:06:00 +01:00
~> **NOTE:** When using `ELB` as the `health_check_type` , `health_check_grace_period` is required.
~> **NOTE:** Terraform has two types of ways you can add lifecycle hooks - via
the `initial_lifecycle_hook` attribute from this resource, or via the separate
[`aws_autoscaling_lifecycle_hook` ](/docs/providers/aws/r/autoscaling_lifecycle_hooks.html )
resource. `initial_lifecycle_hook` exists here because any lifecycle hooks
added with `aws_autoscaling_lifecycle_hook` will not be added until the
autoscaling group has been created, and depending on your
[capacity ](#waiting-for-capacity ) settings, after the initial instances have
been launched, creating unintended behavior. If you need hooks to run on all
instances, add them with `initial_lifecycle_hook` here, but take
care to not duplicate these hooks in `aws_autoscaling_lifecycle_hook` .
2015-05-14 19:45:21 +02:00
## Waiting for Capacity
A newly-created ASG is initially empty and begins to scale to `min_size` (or
`desired_capacity` , if specified) by launching instances using the provided
Launch Configuration. These instances take time to launch and boot.
2016-01-27 16:55:10 +01:00
On ASG Update, changes to these values also take time to result in the target
number of instances providing service.
2015-05-14 19:45:21 +02:00
Terraform provides two mechanisms to help consistently manage ASG scale up
time across dependent resources.
#### Waiting for ASG Capacity
The first is default behavior. Terraform waits after ASG creation for
`min_size` (or `desired_capacity` , if specified) healthy instances to show up
in the ASG before continuing.
2015-11-17 01:16:22 +01:00
If `min_size` or `desired_capacity` are changed in a subsequent update,
Terraform will also wait for the correct number of healthy instances before
continuing.
2015-05-14 19:45:21 +02:00
Terraform considers an instance "healthy" when the ASG reports `HealthStatus:
"Healthy"` and `LifecycleState: "InService"` . See the [AWS AutoScaling
Docs](https://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/AutoScalingGroupLifecycle.html)
for more information on an ASG's lifecycle.
2015-09-08 20:15:30 +02:00
Terraform will wait for healthy instances for up to
`wait_for_capacity_timeout` . If ASG creation is taking more than a few minutes,
it's worth investigating for scaling activity errors, which can be caused by
problems with the selected Launch Configuration.
Setting `wait_for_capacity_timeout` to `"0"` disables ASG Capacity waiting.
2015-05-14 19:45:21 +02:00
#### Waiting for ELB Capacity
2016-01-27 16:55:10 +01:00
The second mechanism is optional, and affects ASGs with attached ELBs specified
via the `load_balancers` attribute.
The `min_elb_capacity` parameter causes Terraform to wait for at least the
requested number of instances to show up `"InService"` in all attached ELBs
during ASG creation. It has no effect on ASG updates.
If `wait_for_elb_capacity` is set, Terraform will wait for exactly that number
of Instances to be `"InService"` in all attached ELBs on both creation and
updates.
These parameters can be used to ensure that service is being provided before
Terraform moves on. If new instances don't pass the ELB's health checks for any
reason, the Terraform apply will time out, and the ASG will be marked as
tainted (i.e. marked to be destroyed in a follow up run).
2015-05-14 19:45:21 +02:00
2015-09-08 20:15:30 +02:00
As with ASG Capacity, Terraform will wait for up to `wait_for_capacity_timeout`
2016-01-27 16:55:10 +01:00
for the proper number of instances to be healthy.
#### Troubleshooting Capacity Waiting Timeouts
If ASG creation takes more than a few minutes, this could indicate one of a
number of configuration problems. See the [AWS Docs on Load Balancer
2015-09-08 20:15:30 +02:00
Troubleshooting](https://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-troubleshooting.html)
2015-05-14 19:45:21 +02:00
for more information.
2016-07-21 00:28:59 +02:00
## Import
2017-02-18 23:48:50 +01:00
AutoScaling Groups can be imported using the `name` , e.g.
2016-07-21 00:28:59 +02:00
```
$ terraform import aws_autoscaling_group.web web-asg
2016-08-19 21:07:53 +02:00
```