AWS Lambda无缘无故返回内部服务器错误

时间:2019-12-28 23:30:22

标签: aws-lambda timeout

我有一个AWS Lambda函数,当我运行它时会生成{"message":"Internal Server Error"}。使用CloudWatch Logs和print语句,我可以确认代码可以正确执行,直到返回为止。这是我的代码:

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")

也没有超时,因为此代码在〜10s内运行,并且我的超时设置为2分钟。我不知道为什么我没有正确的http响应。如果有人可以告诉我原因或想法,那么我将非常感激。

1 个答案:

答案 0 :(得分:1)

我没有您的http_response方法。但是下面的代码可以在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'})
    }

我还注意到您正在将AWS凭证直接添加到lambda代码中。您可以将IAM角色用于lambda,然后为该角色分配所需的权限。 https://aws.amazon.com/blogs/security/how-to-create-an-aws-iam-policy-to-grant-aws-lambda-access-to-an-amazon-dynamodb-table/

相关问题