Last active
June 26, 2021 14:34
-
-
Save DaisukeMiyamoto/dcab0712197f7d774dedb13f3e59da47 to your computer and use it in GitHub Desktop.
Lambda function for counting EC2 instances with Tag filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -- coding: utf-8 -- | |
import boto3 | |
import logging | |
import json | |
TAG_KEY = 'ClusterName' | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
ec2_client = boto3.client('ec2') | |
cloudwatch_client = boto3.client('cloudwatch') | |
def lambda_handler(event, context): | |
responce = ec2_client.describe_instances( | |
Filters=[ | |
{'Name': 'tag-key', 'Values': [TAG_KEY]}, | |
# {'Name': 'tag:'+tag_name, 'Values': [tag_value]}, | |
# {'Name': 'tag:Name', 'Values': ['Compute']}, | |
{'Name': 'instance-state-name', 'Values': ['running']} | |
] | |
) | |
# instance_list = [] | |
num_instances = dict() | |
num_vcpus = dict() | |
vcpus = 0 | |
for reservation in responce['Reservations']: | |
for instance in reservation['Instances']: | |
tag_value = 'NONE' | |
for val in instance['Tags']: | |
if val['Key'] == TAG_KEY: | |
tag_value = val['Value'] | |
if tag_value not in num_instances: | |
num_instances[tag_value] = 0 | |
num_vcpus[tag_value] = 0 | |
num_instances[tag_value] += 1 | |
num_vcpus[tag_value] += instance['CpuOptions']['CoreCount'] * instance['CpuOptions']['ThreadsPerCore'] | |
print(num_instances) | |
print(num_vcpus) | |
metricdata = [] | |
for tag_value in num_instances.keys(): | |
metricdata.append( | |
{ | |
'MetricName': 'Number of Instances', | |
'Dimensions': [{'Name': TAG_KEY, 'Value': tag_value}], | |
'Unit': 'Count', | |
'Value': num_instances[tag_value] | |
} | |
) | |
metricdata.append( | |
{ | |
'MetricName': 'Number of vCPUs', | |
'Dimensions': [{'Name': TAG_KEY, 'Value': tag_value}], | |
'Unit': 'Count', | |
'Value': num_vcpus[tag_value] | |
} | |
) | |
logger.debug(metricdata) | |
cloudwatch_client.put_metric_data( | |
Namespace = 'EC2-Counter', | |
MetricData = metricdata | |
) | |
return { | |
'statusCode': 200, | |
'body': 'Succeeded' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment