Lambda event returns empty object

倖福魔咒の 提交于 2021-02-10 18:40:43

问题


I need to access event["pathParameters"] but the event returns an empty object. I created the function with AWS Cloud9 IDE.

Here is my simple function:

def handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps(event),
        'headers': {
            'Content-Type': 'application/json'
        }
    }

回答1:


event is set by the payload you're invoking the lambda with.

When you use API gateway, that payload includes the key pathParameters, but when you're testing using the lambda console you'll need to form the JSON yourself. The console does include an example of an API gateway proxy event in its templates section.

For a more complete reference see: https://docs.aws.amazon.com/lambda/latest/dg/eventsources.html#eventsources-api-gateway-request




回答2:


If invoking your Lambda function from the command-line, another reason for event being empty could be a change between the v1 and v2 of AWS CLI. Compare:

Where in AWS CLI v1 you could do:

invoke
aws lambda invoke \
--function-name LambdaPhono \
--invocation-type Event \
--payload file://inputFile.txt \
outputfile.txt

In AWS CLI v2 you need to do:

invoke
aws lambda invoke \
--function-name LambdaPhono \
--cli-binary-format raw-in-base64-out \
--invocation-type Event \
--payload file://inputFile.txt \
outputfile.txt

Mind the new --cli-binary-format raw-in-base64-out option in v2.

This is documented here: https://docs.aws.amazon.com/cli/latest/userguide/cliv2-migration.html#cliv2-migration-binaryparam



来源:https://stackoverflow.com/questions/53084288/lambda-event-returns-empty-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!