简体   繁体   中英

AWS Lambda - Python : How to pass JSON input to event object in python handler

I have a lambda function with a lambda handler function. I want to pass a key via the 'event' object. That key can then be processed via this handler function.

For example I want to pass a JSON input to the lambda handler. The JSON input contain a field 'who'.

This is the code in the lambda function:

import json

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from ' + event.who ) # event.who does not exist even though i pass it via JSON 
    }

I created a test event and replaced the Event JSON with the following:

{
    "who": "It is me!"
}

I am expecting 'who' to be accessible from within the event object inside the lambda_handler.

In python we can access the attributes inside a dict this way: dict['attribute']. The 'event' object is a dictionary of key-value pairs. Hence we can use event['who'] to fetch the value of the 'who' attribute.

def lambda_handler(event, context):
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from ' + event['who'] ) 
    }

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