Created
December 21, 2019 16:51
-
-
Save nickfox-taterli/7f5257c1c48eed4d1fdfca3d49cec9fc to your computer and use it in GitHub Desktop.
Get EC2 Credit @ Lambda
This file contains 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
import json | |
import boto3 | |
import datetime | |
def lambda_handler(event, context): | |
ec2 = dict() | |
cloudwatch = boto3.client('cloudwatch') | |
for metric in ['CPUUtilization', 'CPUCreditUsage', 'CPUCreditBalance']: | |
response = cloudwatch.get_metric_statistics( | |
Namespace = 'AWS/EC2', | |
MetricName = metric, | |
Dimensions = [ | |
{ | |
'Name': 'InstanceId', | |
'Value': 'i-0634341d1cb7c7c81' | |
} | |
], | |
Statistics = [ | |
'Sum', | |
], | |
StartTime = datetime.datetime.now() - datetime.timedelta(minutes=10), | |
EndTime = datetime.datetime.now(), | |
Period = 300 | |
) | |
ec2[metric] = response['Datapoints'][0]['Sum'] | |
return json.dumps(ec2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment