0

I am trying to create a simple Lambda function ChildFunction with the following Code:

    import json
    import uuid
 
    def lambda_handler(event, context):
        
        productName = event['ProductName']
        quantity    = event['Quantity']
        unitPrice   = event['UnitPrice']
 
        transactionId   = str(uuid.uuid1())
 
        amount      = quantity * unitPrice
 
        return {
            'TransactionID' :   transactionId,
            'ProductName'   :   productName,
            'Amount'        :   amount
        }

I am creating a Test Event with the following Test parameters:

{

  "ProductName": "iPhone SE",
  "Quantity": "2",
  "UnitPrice": "499"
}

When I execute the Test Event, I am getting the following output:

Response:
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}

Request ID:
"9c68e0d8-3781-4046-ac26-127c45321d71"

Function logs:
START RequestId: 9c68e0d8-3781-4046-ac26-127c45321d71 Version: $LATEST
END RequestId: 9c68e0d8-3781-4046-ac26-127c45321d71
REPORT RequestId: 9c68e0d8-3781-4046-ac26-127c45321d71  Duration: 1.19 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 52 MB

I don't know why the Lambda Function is not executing. What am I doing wrong here?

5
  • Are you really executing the correct Lambda? How is the response with JSON object and the key statusCode even possible given your code? Commented Sep 28, 2020 at 17:12
  • @raupach Yes I am executing the correct Lambda. I mean I am creating a Test event inside the Lambda function and it's not working Commented Sep 28, 2020 at 17:22
  • Are you sure it points to your latest lambda ? How do you deploy your latest code ? Commented Sep 28, 2020 at 18:55
  • @TraychoIvanov I am using the Test button to execute. There is no error log in Cloudwatch. It's saying Execution result: succeeded when I hit Test. but it does not gives any output. Commented Sep 28, 2020 at 20:52
  • Show your configuration. Add a screenshot, etc. Commented Sep 29, 2020 at 1:47

2 Answers 2

1

Lambda has a specific response format:

Try this, where out is your output dict:

import json

return { 'statusCode': 200,  
    'body' :json.dumps({'response': out})
    }

Related issue: Serverless Framework Python lambda return JSON directly

Sign up to request clarification or add additional context in comments.

2 Comments

This needs to be set for API gateway integration. If its standalone function, then it can have custom return format.
your response still needs a body as output. json.dumps(out) is also crucial! Just try it.
0

Even if you execute your function correctly, it will not work. There reason is that quantity and unitPrice are strings, not integers. The correct version is:

import json
import uuid

def lambda_handler(event, context):
    
    productName = event['ProductName']
    quantity    = int(event['Quantity'])
    unitPrice   = int(event['UnitPrice'])

    transactionId   = str(uuid.uuid1())

    amount      = quantity * unitPrice

    return {
        'TransactionID' :   transactionId,
        'ProductName'   :   productName,
        'Amount'        :   amount
    }

From what you described it can't be determined why you execute different function. Maybe you are not deploying it before testing, or testing some old version of it. In the first case, you have to explicitly deploy your function before testing, and in the second case you must ensure that you choose correct version of function to test.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.