How to Create Auto Scaling Group with Terraform
To create an
aws_autoscaling_group in Terraform, define a launch template or configuration, then specify the group with desired capacity and scaling policies. Use resource "aws_autoscaling_group" with parameters like min_size, max_size, and desired_capacity to manage scaling automatically.Syntax
The aws_autoscaling_group resource manages a group of instances that scale automatically. Key parts include:
- name: Unique name for the group.
- launch_template: Defines the instance configuration.
- min_size, max_size, desired_capacity: Control the number of instances.
- vpc_zone_identifier: List of subnet IDs for instances.
- tags: Optional metadata for instances.
terraform
resource "aws_autoscaling_group" "example" { name = "example-asg" launch_template { id = aws_launch_template.example.id version = "$Latest" } min_size = 1 max_size = 3 desired_capacity = 2 vpc_zone_identifier = ["subnet-12345678", "subnet-87654321"] tags = [ { key = "Name" value = "example-instance" propagate_at_launch = true } ] }
Example
This example creates a launch template and an auto scaling group with 2 instances running in specified subnets. It shows how to link the launch template and set scaling sizes.
terraform
provider "aws" { region = "us-east-1" } resource "aws_launch_template" "example" { name_prefix = "example-launch-template-" image_id = "ami-0c94855ba95c71c99" # Amazon Linux 2 AMI instance_type = "t2.micro" tag_specifications { resource_type = "instance" tags = { Name = "example-instance" } } } resource "aws_autoscaling_group" "example" { name = "example-asg" launch_template { id = aws_launch_template.example.id version = "$Latest" } min_size = 1 max_size = 3 desired_capacity = 2 vpc_zone_identifier = ["subnet-12345678", "subnet-87654321"] tag { key = "Name" value = "example-instance" propagate_at_launch = true } }
Output
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
Common Pitfalls
Common mistakes when creating auto scaling groups in Terraform include:
- Not specifying
vpc_zone_identifierfor subnets, causing launch failures. - Using a launch configuration instead of a launch template, which is now preferred.
- Forgetting to set
desired_capacity, leading to zero instances running. - Not tagging instances with
propagate_at_launch = true, making resource tracking harder.
terraform
resource "aws_autoscaling_group" "wrong" { name = "wrong-asg" launch_configuration = aws_launch_configuration.example.id min_size = 1 max_size = 2 # Missing vpc_zone_identifier causes errors } # Correct way uses launch_template and vpc_zone_identifier resource "aws_autoscaling_group" "right" { name = "right-asg" launch_template { id = aws_launch_template.example.id version = "$Latest" } min_size = 1 max_size = 2 vpc_zone_identifier = ["subnet-12345678"] }
Quick Reference
Key parameters for aws_autoscaling_group:
| Parameter | Description |
|---|---|
| name | Unique name for the auto scaling group |
| launch_template | Reference to launch template with instance config |
| min_size | Minimum number of instances to run |
| max_size | Maximum number of instances allowed |
| desired_capacity | Initial number of instances to launch |
| vpc_zone_identifier | List of subnet IDs for instance placement |
| tag | Tags to apply to instances, with propagate_at_launch option |
Key Takeaways
Use aws_autoscaling_group with a launch template to create scalable instance groups.
Always specify vpc_zone_identifier to place instances in correct subnets.
Set min_size, max_size, and desired_capacity to control scaling behavior.
Use tags with propagate_at_launch to track and manage instances easily.
Avoid using launch configurations; prefer launch templates for new setups.