--- layout: "aws" page_title: "AWS: aws_autoscaling_group" sidebar_current: "docs-aws-resource-autoscaling-group" description: |- Provides an AutoScaling Group resource. --- # aws\_autoscaling\_group Provides an AutoScaling Group resource. ## Example Usage ``` resource "aws_placement_group" "test" { name = "test" strategy = "cluster" } resource "aws_autoscaling_group" "bar" { availability_zones = ["us-east-1a"] 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}" initial_lifecycle_hook { name = "foobar" default_result = "CONTINUE" heartbeat_timeout = 2000 lifecycle_transition = "autoscaling:EC2_INSTANCE_LAUNCHING" notification_metadata = < **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`. ## 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. On ASG Update, changes to these values also take time to result in the target number of instances providing service. 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. 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. 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. 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. #### Waiting for ELB Capacity 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). As with ASG Capacity, Terraform will wait for up to `wait_for_capacity_timeout` 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 Troubleshooting](https://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/elb-troubleshooting.html) for more information. ## Import AutoScaling Groups can be imported using the `name`, e.g. ``` $ terraform import aws_autoscaling_group.web web-asg ```