Search code examples
aws-lambdatimeout

AWS Lambda returning internal server error for no reason


I have an AWS Lambda function which results in {"message":"Internal Server Error"} when I run it. Using the CloudWatch Logs and print statements I can confirm that the code executes without errors until it returns. This is my code:

def lambda_handler(event, context):
    table_name = "<redacted>"
    bucket_name = "<redacted>"

    session = boto3.Session(
        aws_access_key_id="<redacted>",
        aws_secret_access_key="<redacted>",
    )

    if "body" in event and event["body"]:
        req_json = json.loads(event["body"])

        uuids = process(req_json, table_name, bucket_name,
                        session, ("127.0.0.1", 8000), "site/")

        print("success", uuids)  # this code still executes sucessfully
        return http_response(200, "Success", uuids)
    else:
        return http_response(400, "No request body")

There is no timeout either, as this code runs within ~10s and my timeout is set to 2 minutes. I have no clue why I'm getting no proper http response. If anyone can tell me why or has an Idea what the issue could be, I'd be very grateful.


Solution

  • I dont have your http_response method. but the code below works in lambda.

    import json
    
    def lambda_handler(event, context):
    
      if "body" in event and event["body"]:
        req_json = json.loads(event["body"])
    
        print("success")  # this code still executes sucessfully
        return {
            'statusCode': 200,
            'body': json.dumps(req_json)
        }
      else:
        return {
            'statusCode': 400,
            'body': json.dumps({'message': 'No request body'})
        }
    

    Also I have noticed that you are adding AWS credentials to the lambda code directly. You could instead use IAM role for lambda and assign the required permission to the role. https://aws.amazon.com/blogs/security/how-to-create-an-aws-iam-policy-to-grant-aws-lambda-access-to-an-amazon-dynamodb-table/