gI9PbPfVbKy0jWV6Kt3QList All AWS EC2 Auto Scaling Groups (ASGs)
List All AWS EC2 Auto Scaling Groups (ASGs)
There was a problem that the LLM was not able to address. Please rephrase your prompt and try again.
This task involves retrieving information about all the ASGs within a specified AWS region or across multiple regions. It helps in monitoring and managing the scaling behaviors of instances automatically, ensuring that the number of instances adjusts according to the set criteria, such as changes in demand or defined schedules.
inputs
outputs
import boto3
from botocore.exceptions import NoCredentialsError, PartialCredentialsError, BotoCoreError, ClientError
creds = _get_creds(cred_label)['creds']
access_key = creds['username']
secret_key = creds['password']
def list_all_regions():
ec2 = boto3.client('ec2', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name='us-east-1')
return [region['RegionName'] for region in ec2.describe_regions()['Regions']]
def list_auto_scaling_groups(region=None):
regions = [region] if region else list_all_regions()
asg_details = []
for region in regions:
try:
asg_client = boto3.client('autoscaling', aws_access_key_id=access_key, aws_secret_access_key=secret_key, region_name=region)
except (NoCredentialsError, PartialCredentialsError, BotoCoreError, ClientError) as e:
print(f"Failed for {region}: {e}")
continue
paginator = asg_client.get_paginator('describe_auto_scaling_groups')
try:
for page in paginator.paginate():
for asg in page['AutoScalingGroups']:
asg['CreatedTime'] = asg['CreatedTime'].strftime('%Y-%m-%d %H:%M:%S') if 'CreatedTime' in asg else 'N/A'
asg['Region'] = region # Ensuring the region is part of the details
asg_details.append(asg)
except ClientError as e:
print(f"Failed for {region}: {e}")
return asg_details
def display_asg_details(data):
if not data:
print("No Auto Scaling Groups found.")
return
table = context.newtable()
table.title = "Auto Scaling Group Details"
table.num_cols = 6 # Add an additional column for 'CreatedTime'
table.num_rows = len(data) + 1 # Rows for headers plus each ASG
table.has_header_row = True
# Define header names with the addition of 'CreatedTime'
headers = ["AutoScalingGroupName", "Region", "MinSize", "MaxSize", "DesiredCapacity", "CreatedTime"]
# Set headers in the first row
for col_num, header in enumerate(headers):
table.setval(0, col_num, header) # Assume setval sets value at (row, col)
# Populate the table with ASG data
for row_num, asg in enumerate(data, start=1): # Starting from the second row
values = [
asg["AutoScalingGroupName"],
asg["Region"],
str(asg["MinSize"]),
str(asg["MaxSize"]),
str(asg["DesiredCapacity"]),
asg["CreatedTime"] # Ensure CreatedTime is included and properly formatted
]
for col_num, value in enumerate(values):
table.setval(row_num, col_num, value)
#region_name = None # Set to None to list ASGs for all available regions or specify a region
asg_list = list_auto_scaling_groups(region_name)
if asg_list:
display_asg_details(asg_list)
#print("\nComplete ASG List:")
#print(asg_list) # Display the complete list of ASG objects for downstream tasks
else:
print("No Auto Scaling Groups found.")
copied