Python:Boto3:get_metric_statistics() 只接受关键字参数 [英] Python: Boto3: get_metric_statistics() only accepts keyword arguments
问题描述
刚开始在 Python 中使用 Boto3,所以绝对是新手.
Just started using Boto3 with Python so definitely new at this.
我正在尝试使用一个简单的 get_metric_statistics 脚本来返回有关实例的 CPUUtilization 的信息.这是我要使用的脚本:
I'm trying to use a simple get_metric_statistics script to return information about CPUUtilization for an instance. Here is the script I'm looking to use:
import boto3
import datetime
cw = boto3.client('cloudwatch')
cw.get_metric_statistics(
300,
datetime.datetime.utcnow() - datetime.timedelta(seconds=600),
datetime.datetime.utcnow(),
'CPUUtilization',
'AWS/EC2',
'Average',
{'InstanceId':'i-11111111111'},
)
但我不断收到以下消息:
but I keep getting the following message:
Traceback (most recent call last):
File "C:..../CloudWatch_GetMetricStatistics.py", line 13, in <module>
{'InstanceId':'i-0c996c11414476c7c'},
File "C:Program FilesPython27libsite-packagesotocoreclient.py", line 251, in _api_call
"%s() only accepts keyword arguments." % py_operation_name)
TypeError: get_metric_statistics() only accepts keyword arguments.
我有:
- 查看了 Boto3 上的文档,我相信我已经正确编写/包含了所有内容
- 在 .aws 文件夹中设置正确的区域/输出格式/安全凭证
- 在 Google 上搜索了 put_metric_statistics 等类似的问题以尝试解决
我仍然不知道我错过了什么?
I'm still stuck as to what I'm missing?
任何指导将不胜感激.
非常感谢本
推荐答案
这有效:
import boto3
import datetime
cw = boto3.client('cloudwatch')
cw.get_metric_statistics(
Period=300,
StartTime=datetime.datetime.utcnow() - datetime.timedelta(seconds=600),
EndTime=datetime.datetime.utcnow(),
MetricName='CPUUtilization',
Namespace='AWS/EC2',
Statistics=['Average'],
Dimensions=[{'Name':'InstanceId', 'Value':'i-abcd1234'}]
)
为了找到正确的值,我使用了 AWS 命令行界面 (CLI):
To find the right values, I use the AWS Command-Line Interface (CLI):
aws cloudwatch list-metrics --namespace AWS/EC2 --metric-name CPUUtilization --max-items 1
它返回如下信息:
{
"Metrics": [
{
"Namespace": "AWS/EC2",
"Dimensions": [
{
"Name": "InstanceId",
"Value": "i-abcd1234"
}
],
"MetricName": "CPUUtilization"
}
],
"NextToken": "xxx"
}
然后您可以使用这些值来填充您的 get_metric_statistics()
请求(例如 Dimensions
参数).
You can then use these values to populate your get_metric_statistics()
requet (such as the Dimensions
parameter).
这篇关于Python:Boto3:get_metric_statistics() 只接受关键字参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!