【问题标题】:AWS Lambda POST to DynamoDB with APIGateway in Python使用 Python 中的 APIGateway 将 AWS Lambda POST 到 DynamoDB
【发布时间】:2020-08-05 00:14:14
【问题描述】:

我正在研究使用 AWS API 网关和 AWS Lambda 将数据发布到 DynamoDB 的 API。问题是当我使用测试场景时工作正常,但在 Postman 或任何其他外部 AWS API 服务中不一样。

import boto3
import uuid

def lambda_handler(event, context):
    # this will create dynamodb resource object and
    # here dynamodb is resource name
    client = boto3.resource('dynamodb')

    title = event['title']
    #generte UUID 
    recordId = str(uuid.uuid4())

    # this will search for dynamoDB table 
    # your table name may be different
    table = client.Table("smsapi")
    print(table.table_status)

    #Creating an Item with a unique id and with the passed title
    table.put_item(
        Item={
            'id' : recordId,
            'title' : title
        }
    )

    return recordId

在 Postman 中我收到 502 Bad gateway

【问题讨论】:

    标签: python-3.x amazon-web-services aws-lambda aws-api-gateway


    【解决方案1】:

    一个可能的原因是,当您在 lambda 控制台中测试您的函数时,API Gateway 代理集成使用了不同的event 格式。

    格式is:

    {
        "resource": "Resource path",
        "path": "Path parameter",
        "httpMethod": "Incoming request's method name"
        "headers": {String containing incoming request headers}
        "multiValueHeaders": {List of strings containing incoming request headers}
        "queryStringParameters": {query string parameters }
        "multiValueQueryStringParameters": {List of query string parameters}
        "pathParameters":  {path parameters}
        "stageVariables": {Applicable stage variables}
        "requestContext": {Request context, including authorizer-returned key-value pairs}
        "body": "A JSON string of the request payload."
        "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
    }
    

    您通过 API 网关提交的任何有效负载都将在:

    • “body”:“请求负载的 JSON 字符串。”

    所以在您的 lambda 函数中,您需要解析 body 有效负载。

    【讨论】:

      【解决方案2】:

      当您遇到一些集成问题时,此消息非常常见,例如权限运气、格式错误等。 所以,我不会给你原因,我会告诉你如何找到这个问题。 您可以使用 AWS 控制台测试此类集成。在 API Gateway 页面中转到您的 API,选择您要测试的方法,然后单击左侧的测试按钮: 在这里,您可以发送包含所有必需参数的测试请求,并查看集成流程的详细日志。通常,错误消息将位于日志底部的某个位置。您还可以在这里找到很多详细信息,例如转换前的请求和响应是什么等。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-05
        • 2020-12-11
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        相关资源
        最近更新 更多