Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

python 3.x - Response is null when trying to fetch CPU utilization metric from Cloudwatch from Lambda using get-metric-statistics

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..

question from:https://stackoverflow.com/questions/65951223/response-is-null-when-trying-to-fetch-cpu-utilization-metric-from-cloudwatch-fro

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...