0

I have an a closure by Amazon webservices called lambda function with delegation defined as following:

def lambda_handler(event, context):
    logger.info('Ev° %s', event)

    if event['action'] == "getPosition":
        getPosition(event, context)

def getPosition(event, context):

    # read position from file
    positionLoaded = readPosition(pId=int(json.loads(str(event['body']['id']))))

    # build response
    response = {
        "statusCode": 200,
        "body": json.dumps(positionLoaded, indent=4),
        "message": "OK",
        "headers": {
            "Content-Type": "application/json",
        }
    }

return response


# Reads position in json file from s3 Bucket with pId
def readPosition(pId: int):
    positionFromBucketJSON = s3_client.get_object(Bucket="bucketName", Key=str(pId) + ".json")
    return json.loads(positionFromBucketJSON['Body'].read().decode('utf-8'))

When I send a request to the lambda function as following:

{
  "action": "getPosition",
  "body": {
    "id": 2021152530123456
  }
}

I get an error response from the lambda function as following:

{"errorMessage": "'Not Found'", "errorType": "KeyError", "stackTrace": ["  File \"/var/task/positionService.py\", line 58, in lambda_handler\n    getPosition(event, context, callback=None)\n", "  File \"/var/task/positionService.py\", line 76, in getPosition\n    message = "not existing at all!"}

I don't know why since JSON-File 2021152530123456.json do exist directly in s3 bucket.

Could you help to figure out what the error could be in this case?

0

2 Answers 2

2

The Python Lambda function handler takes 2 parameters (event, context), not 3 parameters (event, context, callback). You may have been reading JavaScript documentation, where a Lambda function handler has a 3rd parameter (callback).

Here is an example of a Python Lambda function that returns a JSON payload:

import json
        
def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "headers": {
            "Content-Type": "application/json"
        },
        "body": json.dumps({
            "id": 12,
            "description": "hoverboard"
        })
    }

In your original code, you have two additional problems:

  1. your Lambda handler doesn't return anything (it should return getPosition(event, context))
  2. your getPosition() function's return statement is mis-indented (correct the indentation)
Sign up to request clarification or add additional context in comments.

7 Comments

i,m going to check it out, and come back, thanks for the details, i have read that documentation also from many other ones. See you later then
i,m getting now null as return after update!
What does your current code look like? It's going to be difficult to diagnose something we can't see.
i just updated the question with newer code :)
Your Lambda handler, as written, doesn't return anything. And your getPosition() function's return statement is mis-indented.
|
-1

I solve my issue by adding a return to the main lambda function while delegating to the getPosition(...) function, it look then like following:

def lambda_handler(event, context):
    logger.info('Ev° %s', event)

    if event['action'] == "getPosition":
        return getPosition(event, context)

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.