Lambda python 3.7:参数验证失败:\ n参数Dimensions [0]的类型无效

时间:2019-01-14 13:07:35

标签: python amazon-web-services aws-lambda

我已经连续几天收到此错误,无法解决此代码中的问题:

"errorMessage": "Parameter validation failed:\nInvalid type for parameter Dimensions[0].Value, value: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}, type: <class 'dict'>, valid types: <class 'str'>",
  "errorType": "ParamValidationError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 26, in bucket_size\n    Unit='Bytes'\n",
    "  File \"/var/runtime/botocore/client.py\", line 320, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 596, in _make_api_call\n    api_params, operation_model, context=request_context)\n",
    "  File \"/var/runtime/botocore/client.py\", line 632, in _convert_to_request_dict\n    api_params, operation_model)\n",
    "  File \"/var/runtime/botocore/validate.py\", line 291, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\n"
  ]

我的python 3.7代码:

import boto3
from datetime import datetime, timedelta
import json


def bucket_size(a, b):
    bucket_name = a
    cloudwatch = boto3.client('cloudwatch',region_name='ap-southeast-1')
    response = cloudwatch.get_metric_statistics(
        Namespace="AWS/S3",
        MetricName="BucketSizeBytes",
        Dimensions=[
            {
                'Name': 'BucketName',
                'Value': bucket_name
            },
            {
                'Name': 'StorageType',
                'Value': 'StandardStorage'
            }
        ],
        Statistics=['Average'],
        Period=86400,
        StartTime=datetime.now()-timedelta(days=10),
        EndTime=datetime.now()-timedelta(days=2),
        Unit='Bytes'
    )

我正在尝试从S3获取指标,并通过管道将其传递到特定S3存储桶上的.csv文件,但是我在lambda python 3.7上遇到此错误

任何帮助,不胜感激,打开很多标签可以在线找到答案,但不可用,谢谢!干杯

1 个答案:

答案 0 :(得分:0)

不确定,但是我认为是

Dimensions=[
            {
                'Name': 'BucketName',
                'Value': bucket_name
            },
            {
                'Name': 'StorageType',
                'Value': 'StandardStorage'
            }
        ]

您正在传递字典对象列表,而模块需要一个字符串对象列表。