in src/ASGMonitoring/app.py [0:0]
def lambda_handler(event, context):
# show the response
logger.info(event)
# get our ASG client
client = boto3.client('autoscaling')
# get the list of autoscaling groups
resp = client.describe_auto_scaling_groups()
# if the ASG is created by Batch enable metrics collection
for g in resp['AutoScalingGroups']:
try:
x = g['MixedInstancesPolicy']['LaunchTemplate']['LaunchTemplateSpecification']['LaunchTemplateName']
except Exception:
logger.info(f"Cannot add metrics collection for {g['AutoScalingGroupName']}")
if 'Batch-lt' in x and 'EnabledMetrics' in g:
# in our case we add all the metrics but you could select the ones of interest
# don't forget to check and update your dashboards should you change the metrics
# https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-instance-monitoring.html
r = client.enable_metrics_collection(AutoScalingGroupName=g['AutoScalingGroupName'], Granularity='1Minute')
logger.info(f"Enabled metrics collection on {g['AutoScalingGroupName']}")
return