【问题标题】:Response is null when trying to fetch CPU utilization metric from Cloudwatch from Lambda using get-metric-statistics尝试使用 get-metric-statistics 从 Lambda 从 Cloudwatch 获取 CPU 利用率指标时响应为空
【发布时间】:2021-05-03 04:20:45
【问题描述】:

我需要创建一个 lambda 函数,该函数必须从 cloudwatch 获取 ec2 的 CPUUtilization 并生成一个 csv 报告。当我尝试使用以下代码获取它时,响应为空,

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'
            }   
        ] 
    
    )

但是当我尝试在 AWSCLI 中接收一些数据点时,

{
  "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"
]
}

我错过了什么?请帮忙..

【问题讨论】:

    标签: python-3.x amazon-web-services aws-lambda boto3 amazon-cloudwatch


    【解决方案1】:

    不会返回您的lambda_handler 的任何内容。

    假设你对get_metric_statistics的使用是正确的,你需要添加一个return语句

    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
    

    【讨论】:

    • 谢谢。那很有用。 :)(lambda lunction 的新手)
    • @Bala 没问题。如果答案有用,我们将不胜感激。
    猜你喜欢
    • 2019-01-22
    • 2019-11-01
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多