Home > Back-end >  Invoking python lambda with arguments
Invoking python lambda with arguments

Time:11-07

I have a simple lambda function which accepts an argument and prints it.

import json

def lambda_handler(event, context):
   temp = event["temp"]
   return {
      'statuscode': 200,
      'body': json.dumps(f'Hello from {temp}!')
   }

I've enabled function url in lambda and set authorization as None. Without any arguments, curl https://xxxx.lambda-url.us-east-1.on.aws works.

Using Test through the console, I get the correct output, but not through a curl call.

curl -X POST https://xxxx.lambda-url.us-east-1.on.aws \
-H 'content-type: application/json' \
--data '{"temp":"Hello World"}' \
> 
Internal Server Error%

CodePudding user response:

You forgot a step in your code. You have to extract the body first from the event, and then you can extract the temp attribute from the body, for example like this:

def lambda_handler(event, context):
   body = json.loads(event['body'])
   temp = body['temp']
   return {
      'statuscode': 200,
      'body': json.dumps(f'Hello from {temp}!')
   }
  • Related