简体   繁体   中英

Parameter validation failed:\nInvalid type for parameter Filters[0].Values[0], value: {}, type: <class 'dict'>, valid types: <class 'str'>

I have the following code in AWS lambda.

from operator import itemgetter

import boto3

client = boto3.client('ec2')

def lambda_handler(name='test*', owner='**********'):
    list_of_images = client.describe_images(Filters=[{'Name': 'name', 'Values':
    [name, ]}, {'Name': 'owner-id', 'Values': [owner, ]}])
    image_details = sorted(list_of_images['Images'], key=itemgetter(
    'CreationDate'), reverse=True)
    return image_details[0]  

I am not familiar with python. when I run this every time I get following error.

{
    "errorMessage": "Parameter validation failed:\nInvalid type for parameter Filters[0].Values[0], value: {}, type: <class 'dict'>, valid types: <class 'str'>\nInvalid type for parameter Filters[1].Values[0], value: <__main__.LambdaContext object at 0x7fb272a686a0>, type: <class '__main__.LambdaContext'>, valid types: <class 'str'>",
    "errorType": "ParamValidationError",
    "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 10, in lambda_handler\n    list_of_images = client.describe_images(Filters=[{'Name': 'name', 'Values':\n",
    "  File \"/var/runtime/botocore/client.py\", line 357, in _api_call\n    return self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 648, in _make_api_call\n    request_dict = self._convert_to_request_dict(\n",
    "  File \"/var/runtime/botocore/client.py\", line 696, in _convert_to_request_dict\n    request_dict = self._serializer.serialize_to_request(\n",
    "  File \"/var/runtime/botocore/validate.py\", line 293, in serialize_to_request\n    raise ParamValidationError(report=report.generate_report())\n"
    ]
}

Boto is picky about parameter types, but at least the error message describes what is expected.

I think you are mistakenly passing name as something other than a string. ec2 describe-instances does not have a name filter, perhaps you need to filter on tag:Name ?

eg

name='Webserver'
owner='123456789012'

list_of_images = client.describe_images(Filters=[ {'Name': 'tag:Name', 'Values': [name] } , {'Name': 'owner-id', 'Values': [owner] } ])

Alternatively pass in dict parameters

names=['Webserver']
owners=['123456789012']
list_of_images = client.describe_images(Filters= [ {'Name': 'tag:Name', 'Values': names }, {'Name': 'owner-id', 'Values': owners } ])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM