Amazon EC2 Auto Scaling
Overview
The AWS Auto Scaling Module provides a reusable and scalable way to provision and manage Amazon EC2 Auto Scaling infrastructure using Terraform.
It abstracts the complexity of configuring Auto Scaling resources and enables developers to define infrastructure with minimal effort while following AWS best practices.
This module supports both resource creation and data sources for managing Auto Scaling Groups (ASG), Launch Configurations, and related policies.
Features
- 🚀 Reusable & Modular – Predefined structure for resources and data modules.
- 📈 Dynamic Scaling – Configure scaling policies based on CPU, memory, or custom CloudWatch metrics.
- 🔄 Lifecycle Hooks – Supports lifecycle events for custom workflows during instance launch/termination.
- 🧑🤝🧑 Multiple Attachments – Attach Auto Scaling groups to multiple target groups or load balancers.
- 📜 Tagging & Notifications – Supports group tags and SNS notifications for scaling activities.
- ⏱ Scheduled Scaling – Define scheduled actions for predictable workload changes.
- 🧩 Extensible – Easy to integrate with other Terraform modules (VPC, ELB, CloudWatch, etc.).
Core Concepts
-
Resources Module
- Creates and manages Auto Scaling components, including:
- Auto Scaling Groups (ASG)
- Launch Configurations
- Scaling Policies
- Lifecycle Hooks
- Scheduled Actions
- Notifications
- Creates and manages Auto Scaling components, including:
-
Data Module
- Fetches information about existing Auto Scaling resources.
- Useful for referencing attributes like group names, ARNs, and capacity.
-
Separation of Concerns
resources/handles ASG creation and configuration.data/is focused on reading and referencing existing ASG resources.- This separation ensures clarity, reusability, and modular design.
Sample Usage
- Terraform
- Terragrunt
- Opentofu
# DEPLOY ANANTA CLOUD'S AUTO SCALING MODULE
module "autoscaling" {
source = "github.com/anantacloud/terraform-aws-autoscaling-module/modules/resources"
# ==========================
# REQUIRED VARIABLES
# ==========================
name_prefix = "my-asg"
launch_config_name = "my-launch-config"
min_size = 1
max_size = 3
desired_capacity = 2
vpc_zone_identifier = ["subnet-123456", "subnet-654321"]
# ==========================
# OPTIONAL VARIABLES
# ==========================
health_check_type = "EC2"
health_check_grace_period = 300
tags = {
Environment = "dev"
Project = "my-app"
}
}
# terragrunt.hcl
terraform {
source = "github.com/anantacloud/terraform-aws-autoscaling-module/modules/resources"
}
inputs = {
# ==========================
# REQUIRED VARIABLES
# ==========================
name_prefix = "my-asg"
launch_config_name = "my-launch-config"
min_size = 1
max_size = 3
desired_capacity = 2
vpc_zone_identifier = ["subnet-123456", "subnet-654321"]
# ==========================
# OPTIONAL VARIABLES
# ==========================
health_check_type = "EC2"
health_check_grace_period = 300
tags = {
Environment = "dev"
Project = "my-app"
}
}
# Opentofu.hcl
terraform {
source = "github.com/anantacloud/terraform-aws-autoscaling-module/modules/resources"
}
inputs = {
# ==========================
# REQUIRED VARIABLES
# ==========================
name_prefix = "my-asg"
launch_config_name = "my-launch-config"
min_size = 1
max_size = 3
desired_capacity = 2
vpc_zone_identifier = ["subnet-123456", "subnet-654321"]
# ==========================
# OPTIONAL VARIABLES
# ==========================
health_check_type = "EC2"
health_check_grace_period = 300
tags = {
Environment = "dev"
Project = "my-app"
}
}
- Inputs
- Outputs
Required
| Name | Description | Type | Example |
|---|---|---|---|
| name_prefix | Prefix for the Auto Scaling Group name | string | "my-asg" |
| launch_config_name | Name of the Launch Configuration | string | "my-launch-config" |
| min_size | Minimum number of instances in the group | number | 1 |
| max_size | Maximum number of instances in the group | number | 3 |
| desired_capacity | Desired number of instances | number | 2 |
| vpc_zone_identifier | List of subnet IDs for the Auto Scaling Group | list | ["subnet-123456"] |
Optional
| Name | Description | Type | Default |
|---|---|---|---|
| health_check_type | Health check type for the group (EC2 or ELB) | string | "EC2" |
| health_check_grace_period | Time (in seconds) for grace period before checking | number | 300 |
| tags | Key-value tags to apply to Auto Scaling resources | map | {} |
| Name | Description |
|---|---|
| asg_id | The ID of the Auto Scaling Group |
| asg_name | The name of the Auto Scaling Group |
| asg_arn | The ARN of the Auto Scaling Group |
| asg_min_size | The minimum size of the Auto Scaling Group |
| asg_max_size | The maximum size of the Auto Scaling Group |
| asg_desired_size | The desired size of the Auto Scaling Group |