Tuesday, 11 May 2021

AWS Lambda function for AWS Metric using Autoscaling groups

I am in the midst of coding a lambda function which will create an alarm based upon some disk metrics. The code so far looks like this:

import collections
from datetime import datetime
import calendar
def lambda_handler(event, context):
    client = boto3.client('cloudwatch')
    alarm = client.put_metric_alarm(
    AlarmName='Disk Monitor',
    MetricName='disk_used_percent',
    Namespace='CWAgent',
    Statistic='Maximum',
    ComparisonOperator='GreaterThanOrEqualToThreshold',
    Threshold=60.0,
    Period=10,
    EvaluationPeriods=3,
    Dimensions=[
        {
            'Name': 'InstanceId',
            'Value': '{instance_id}'
        },
        {
            'Name': 'AutoScalingGroupName',
            'Value': '{instance_id}'
        },
        {
            'Name': 'fstype',
            'Value': 'xfs'
        },
        {
            'Name': 'path',
            'Value': '/'
        }
    ],
    Unit='Percent',
    ActionsEnabled=True)

As seen, {instance_id} is a variable because the idea is that this will be used for every instance. However, I am wondering how I would code the same for AutoScalingGroupName because I require this to be a variable also. I know that that the below pulls out the AutoScalingGroupName for me, but how would I add that to the above block in terms of syntax, is my problem:

aws autoscaling describe-auto-scaling-instances --output text --query "AutoScalingInstances[?InstanceId == '<instance_dets>'].{AutoScalingGroupName:AutoScalingGroupName}"

For example, would I add a block beginning as below:

def lambda_handler(event, context):
    client = boto3.client('autoscaling')

And if so, how would I then code what is needed in terms of syntax to get the 'Value': '{AutoScalingGroupName}' by which I mean a variable to hold the ASG?



from AWS Lambda function for AWS Metric using Autoscaling groups

No comments:

Post a Comment