Skip to content

Instantly share code, notes, and snippets.

@jveldboom
Created March 11, 2018 18:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jveldboom/8675b68fd0fda156b9d0f926792dce7a to your computer and use it in GitHub Desktop.
Save jveldboom/8675b68fd0fda156b9d0f926792dce7a to your computer and use it in GitHub Desktop.
import boto3
ec2 = boto3.client('ec2');
cloudwatch = boto3.client('cloudwatch');
def lambda_handler(event, context):
count = 0
response = ec2.describe_instances()
for r in response['Reservations']:
count += len(r['Instances'])
cloudwatch.put_metric_data(
Namespace='EC2',
MetricData=[
{
'MetricName': 'InstanceCount',
'StatisticValues': {
'SampleCount': count,
'Sum': count,
'Minimum': count,
'Maximum': count
},
'Unit': 'Count'
},
]
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment