Reputation: 65
i need to create a lambda function which has to fetch CPUUtilization of ec2 from cloudwatch and generate a csv report. When i tried fetching it using following code response is null,
import json
import boto3
import datetime
cw = boto3.client(service_name='cloudwatch',region_name = 'ap-south-1')
def lambda_handler(eeeee, context):
# TODO implement
response = cw.get_metric_statistics(
Namespace = 'AWS/EC2',
Period = 600,
StartTime = '2021-01-27T00:00:00Z',
EndTime = '2021-01-28T12:00:00Z',
MetricName = 'CPUUtilization',
Statistics=['Average'],
Dimensions = [
{
'Name': 'InstanceId',
'Value': 'i-0ae327'
}
]
)
But when i try in AWSCLI am recieving some datapoints,
{
"Namespace": "AWS/EC2",
"MetricName": "CPUUtilization",
"Dimensions": [
{
"Name": "InstanceId",
"Value": "i-0ae327"
}
],
"StartTime": "2021-01-27T00:00:00",
"EndTime": "2021-01-28T12:00:00",
"Period": 600,
"Statistics": [
"Average"
]
}
What am i missing? Please help out..
Upvotes: 2
Views: 249
Reputation: 238139
You are not returning anything from your lambda_handler
.
Assumming that your use of get_metric_statistics
is correct, you need to add a return statement:
def lambda_handler(eeeee, context):
# TODO implement
response = cw.get_metric_statistics(
Namespace = 'AWS/EC2',
Period = 600,
StartTime = '2021-01-27T00:00:00Z',
EndTime = '2021-01-28T12:00:00Z',
MetricName = 'CPUUtilization',
Statistics=['Average'],
Dimensions = [
{
'Name': 'InstanceId',
'Value': 'i-0ae327'
}
]
)
return response
Upvotes: 2