python - 使用代理集成通过 API 网关触发 AWS Lambda

标签 python amazon-web-services aws-lambda aws-api-gateway

我刚刚开始使用 AWS,并在 AWS Lambda 中制作了一个基本计算器,如下所示:

import json

def lambda_handler(event, context):
op1 = event.get('op1',0)
op2 = event.get('op2',0)
operator = event.get('op', None)
if op1 == 0 or op2 == 0 or operator == None:
    result = 'Please enter valid input data'
    ret_code = 219
else:
    if operator in {'add', 'plus', '+'}:
        result = op1 + op2
        ret_code = 278
    elif operator in {'subtract', 'minus', '-'}:
        result = op1 - op2
        ret_code = 278
    elif operator in {'multiply', '*'}:
        result = op1 * op2
        ret_code = 278
    elif operator in {'divide', '/'}:
        result = op1 / op2
        ret_code = 278
    else:
        result = 'Please enter valid operator'
        ret_code = 219
return {
    'isBase64Encoded': False,
    'statusCode': ret_code,
    'headers': {'Content-Type': 'application/json'},
    'body': json.dumps(result)
}

当我使用以下方法测试 Lambda 时,此函数工作正常

{
"op1": 7,
"op2": 9,
"op": "divide"
}

然后我在 Amazon API Gateway 中创建了一个 API,并将其配置为使用 Lambda 代理集成,如 API Setup 所示。 .

但是,现在当我尝试通过与以前相同的输入数据通过 PUT 或 POST 触发 AWS Lambda 时,我收到 219 错误,这意味着数据没有被传递到 Lambda 事件中,而是 Lambda使用默认零,给出错误。我从 API 网关得到的响应如下:

Execution log for request test-request
Tue Nov 21 12:19:34 UTC 2017 : Starting execution for request: test-invoke-request
Tue Nov 21 12:19:34 UTC 2017 : HTTP Method: PUT, Resource Path: /
Tue Nov 21 12:19:34 UTC 2017 : Method request path: {}
Tue Nov 21 12:19:34 UTC 2017 : Method request query string: {}
Tue Nov 21 12:19:34 UTC 2017 : Method request headers: {}
Tue Nov 21 12:19:34 UTC 2017 : Method request body before transformations: {
"op1": 7,
"op2": 9,
"op": "divide"
}
Tue Nov 21 12:19:34 UTC 2017 : Endpoint request URI: https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:*:function:newCalc/invocations
Tue Nov 21 12:19:34 UTC 2017 : Endpoint request headers: {x-amzn-lambda-integration-tag=test-request, Authorization=*, X-Amz-Date=20171121T121934Z, x-amzn-apigateway-api-id=*, X-Amz-Source-Arn=arn:aws:execute-api:eu-west-2:*:wrsio8dvp3/null/PUT/{proxy+}, Accept=application/json, User-Agent=AmazonAPIGateway_*, X-Amz-Security-Token=*
Tue Nov 21 12:19:34 UTC 2017 : Endpoint request body after transformations: {"resource":"/{proxy+}","path":"/","httpMethod":"PUT","headers":null,"queryStringParameters":null,"pathParameters":null,"stageVariables":null,"requestContext":{"path":"/{proxy+}","accountId":"*","resourceId":"fsamcn","stage":"test-invoke-stage","requestId":"test-invoke-request","identity":{"cognitoIdentityPoolId":null,"accountId":"*","cognitoIdentityId":null,"caller":"*","apiKey":"test-invoke-api-key","sourceIp":"test-invoke-source-ip","accessKey":"*","cognitoAuthenticationType":null,"cognitoAuthenticationProvider":null,"userArn":"arn:aws:iam::*:user/*","userAgent":"Apache-HttpClient/4.5.x (Java/1.8.0_144)","user":"*"},"resourcePath":"/{proxy+}","httpMethod":"PUT","apiId":"wrsio8dvp3"},"body":"{\n  \"op1\": 7,\n  \"op2\": 9,\n  \"op\": \"divide\"\n}","isBase64Encoded":false}
Tue Nov 21 12:19:34 UTC 2017 : Sending request to https://lambda.eu-west-2.amazonaws.com/2015-03-31/functions/arn:aws:lambda:eu-west-2:*:function:newCalc/invocations
Tue Nov 21 12:19:34 UTC 2017 : Received response. Integration latency: 287 ms
Tue Nov 21 12:19:34 UTC 2017 : Endpoint response body before transformations: {"isBase64Encoded": false, "statusCode": 219, "headers": {"Content-Type": "application/json"}, "body": "\"Please enter valid input data\""}
Tue Nov 21 12:19:34 UTC 2017 : Endpoint response headers: {x-amzn-Remapped-Content-Length=0, Connection=keep-alive, x-amzn-RequestId=3befcbd6-ceb6-11e7-a4dd-c3db02aba4a2, Content-Length=139, Date=Tue, 21 Nov 2017 12:19:34 GMT, X-Amzn-Trace-Id=root=1-5a1419d6-106413cfbf4bf985d840894f;sampled=0, Content-Type=application/json}
Tue Nov 21 12:19:34 UTC 2017 : Method response body after transformations: "Please enter valid input data"
Tue Nov 21 12:19:34 UTC 2017 : Method response headers: {X-Amzn-Trace-Id=sampled=0;root=1-5a1419d6-106413cfbf4bf985d840894f, Content-Type=application/json}
Tue Nov 21 12:19:34 UTC 2017 : Successfully completed execution
Tue Nov 21 12:19:34 UTC 2017 : Method completed with status: 219

那么,您能否帮我弄清楚我做错了什么 - 以及如何正确格式化我的请求正文以便能够触发 Lambda 函数。

最佳答案

您遇到的问题来自api网关的代理集成。如果您选中此项,则提供给您的 lambda 的输入结构不再是提供给您 PUT 操作的普通负载。

您的事件参数实际上由以下结构组成(在您的日志中,行 Endpoint request body after transformations)

{
    "resource": "/{proxy+}",
    "path": "/",
    "httpMethod": "PUT",
    "headers": null,
    "queryStringParameters": null,
    "pathParameters": null,
    ....
    "body": "{\n  \"op1\": 7,\n  \"op2\": 9,\n  \"op\": \"divide\"\n}",
    "isBase64Encoded": false
}

因此,要访问您的原始事件,您必须解码 event.body 元素。

关于python - 使用代理集成通过 API 网关触发 AWS Lambda,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47413250/

相关文章:

python - 无法在模板/网页中显示特殊字符 Django/Python

python - 在odoo的日期字段中添加年份

json - 使用 EventBridge 输入转换器时如何从状态机输出中提取属性

python - Google App Engine 模型父级

amazon-web-services - 在可见性超时后放置哪个优先级顺序 SQS 消息?

amazon-web-services - 如何使用codebuild通过AWS CDK连接到github?

php - 使用 Amazon Web Services PHP SDK 的 SenderID

amazon-web-services - 如何对使用层方法的 lambda 逻辑进行单元测试?

amazon-web-services - 是否可以向使用 AWS Amplify 预置的 lambda PostAuthenticate 函数授予额外权限?

php - 卡在 os.rename() 处