Introduction
Scaling EC2 Auto Scaling Groups (ASGs) dynamically can enhance performance and reduce unnecessary costs. This blog provides an AWS Lambda function to adjust the capacity of ASGs based on time-based triggers and custom tags.
Overview
The Lambda function performs the following steps:
- Lists all EC2 Auto Scaling Groups in the account.
- Retrieves tags for each group.
- Checks for specific tags indicating when to adjust the capacity.
- Updates the ASG’s capacity using the
update_auto_scaling_groupAPI.
Prerequisites
- IAM Role: Ensure the Lambda function has sufficient permissions to describe ASGs, list tags, and update ASG capacity.
- Tagged ASGs: Add specific tags to your ASGs to indicate when to update capacity.
- Boto3: AWS SDK for Python to interact with AWS services.
Lambda Function Code
import boto3
import os
from datetime import datetime, timezone
# Initialize AWS clients
def list_all_auto_scaling_groups():
groups = []
response = autoscaling_client.describe_auto_scaling_groups()
for group in response['AutoScalingGroups']:
groups.append({
'AutoScalingGroupName': group['AutoScalingGroupName'],
'AutoScalingGroupARN': group['AutoScalingGroupARN']
})
return groups
def get_asg_tags(asg_name):
try:
response = autoscaling_client.describe_tags(
Filters=[{'Name': 'auto-scaling-group', 'Values': [asg_name]}] # Corrected filter
)
tags = {tag['Key']: tag['Value'] for tag in response.get('Tags', [])} # Extract tags properly
return tags
except Exception as e:
print(f"Error retrieving tags for {asg_name}: {str(e)}")
return {}
def update_asg_capacity(group_name, min_capacity, max_capacity, desired_capacity):
print(f"Updating capacity for {group_name} to min: {min_capacity}, max: {max_capacity}, desired: {desired_capacity}")
response = autoscaling_client.update_auto_scaling_group(
AutoScalingGroupName=group_name,
MinSize=int(min_capacity),
MaxSize=int(max_capacity),
DesiredCapacity=int(desired_capacity)
)
print(f"Update successful for {group_name}: {response}")
def lambda_handler(event, context):
try:
current_time = datetime.now(timezone.utc).strftime("%H:%M")
print(f"Current Time (UTC): {current_time}")
groups = list_all_auto_scaling_groups()
print(f"Found {len(groups)} EC2 Auto Scaling Groups.")
for group in groups:
group_name = group['AutoScalingGroupName']
print(f"Checking Auto Scaling Group: {group_name}")
tags = get_asg_tags(group_name)
print(f"Tags for {group_name}: {tags}")
if tags.get('update_instance_count', 'no').lower() == 'yes':
print(f"'update_instance_count' is set to 'yes' for {group_name}.")
morning_time = tags.get('morning_time')
night_time = tags.get('night_time')
min_capacity_morning = tags.get('min_instances_morning')
min_capacity_night = tags.get('min_instances_night')
max_count = tags.get('max_count', 1)
desired_capacity = tags.get('desired_capacity')
if not (morning_time and night_time and min_capacity_morning and min_capacity_night):
print(f"Missing required tags for Auto Scaling Group {group_name}. Skipping...")
continue
if current_time == morning_time:
update_asg_capacity(group_name, min_capacity_morning, max_count, min_capacity_morning)
elif current_time == night_time:
update_asg_capacity(group_name, min_capacity_night, max_count, min_capacity_night)
else:
print(f"No action needed for {group_name} at this time.")
else:
print(f"'update_instance_count' is not set to 'yes' for {group_name}. Skipping...")
print("EC2 Auto Scaling Group capacity update check completed successfully.")
return {
'statusCode': 200,
'body': 'EC2 Auto Scaling Group capacity updated successfully where applicable!'
}
except Exception as e:
print(f"Error occurred: {str(e)}")
return {
'statusCode': 500,
'body': f"Error: {str(e)}"
}Key Concepts
Tags for Time-Based Control:
update_instance_count: Set toyesto enable updates for the ASG.morning_timeandnight_time: Define when to update capacity (e.g.,06:00and18:00).min_instances_morningandmin_instances_night: Minimum capacity for morning and night periods.max_count: Maximum capacity.
How to Deploy
Create Lambda Function
- Open AWS Lambda and create a new function.
- Upload the code or use the in-line editor.
IAM Role
- Attach an IAM policy with permissions for
autoscaling:DescribeAutoScalingGroups,autoscaling:DescribeTags, andautoscaling:UpdateAutoScalingGroup.
Test the Function
- Trigger the Lambda function manually to verify it lists ASGs, reads tags, and updates capacity as expected.
Conclusion
By using this approach, you can automate capacity changes for EC2 ASGs, ensuring optimal performance and cost efficiency. This setup reduces manual effort, controls costs, and adapts to changing workloads.






