'Accessing data POST'd to a python AWS Lambda Function URL
I'm attempting to invoke an AWS Lambda Function URL in Python. Using their example lambda code as follows the value for action
is always returning null
. How do I access the value in the json data I POST to the Function URL?
Lambda Code (taken from here):
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
result = None
action = event.get('action')
if action == 'increment':
result = event.get('number', 0) + 1
logger.info('Calculated result of %s', result)
else:
logger.error("%s is not a valid action.", action)
response = {'result': result}
return response
Invoking using a Function URL
curl -X POST 'https://[redacted].lambda-url.eu-west-1.on.aws/' -d '{"action":"increment","number": 3}'
Result:
{"result":null}
Problem:
How do I reference the value of 'action'
correctly to produce the result
?
Solution 1:[1]
Out of interest, this is how I adapted the AWS sample code to parse the required fields
import logging
import json
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
message = json.loads(event['body'])
result = None
action = message['action']
if action == 'increment':
result = message['number'] + 1
logger.info('Calculated result of %s', result)
else:
logger.error("%s is not a valid action.", action)
response = {'result': result}
return response
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | c0nt0s0 |