我需要创建一个 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"
]
}
我错过了什么?请大家帮帮忙..
最佳答案
您没有从 lambda_handler
中返回任何内容。
假设您对 get_metric_statistics
的使用是正确的,您需要添加一个返回语句:
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
关于python-3.x - 尝试使用 get-metric-statistics 从 Lambda 从 Cloudwatch 获取 CPU 利用率指标时,响应为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65951223/