AWS Lambda:无法通过URL访问事件['param'],但可用于API GW测试

时间:2019-11-05 22:50:10

标签: aws-lambda

import json

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "headers": {"Content-Type": "application/json"},
        "body": json.dumps(event['param'])
    }

我不确定自己在做什么错。我成功设置了API网关,Python Lambda函数,通过Route53的正确DNS和有效的TLS证书...

我可以通过调用mydomain.com/lambda访问我的Lambda,但是我没有传递任何参数,只有这个错误。现在已经尝试了5个令人沮丧的小时。

通过mydomain.com/lambda调用

 errorMessage   "'param'"
    errorType   "KeyError"
    stackTrace  
    0   "  File \"/var/task/lambda_function.py\", line 7, in lambda_handler\n    \"body\": json.dumps(event['param'])\n"

通过API网关的“测试”调用(此处一切正常)

    Execution log for request 
    Tue Nov 05 22:42:47 UTC 2019 : Starting execution for request:
    Tue Nov 05 22:42:47 UTC 2019 : HTTP Method: GET, Resource Path: /
    Tue Nov 05 22:42:47 UTC 2019 : Method request path: {}
    Tue Nov 05 22:42:47 UTC 2019 : Method request query string: {}
    Tue Nov 05 22:42:47 UTC 2019 : Method request headers: {}
    Tue Nov 05 22:42:47 UTC 2019 : Method request body before transformations: 
    Tue Nov 05 22:42:47 UTC 2019 : Endpoint request URI: 
    Tue Nov 05 22:42:47 UTC 2019 : Endpoint request headers: {x-amzn-lambda-integration-tag=Authorization=...[TRUNCATED]
    Tue Nov 05 22:42:47 UTC 2019 : Endpoint request body after transformations: 
    {
      "param" : "foo"
    }
    Tue Nov 05 22:42:47 UTC 2019 : Sending request to 
    Tue Nov 05 22:42:47 UTC 2019 : Received response. Status: 200, Integration latency: 36 ms
    Tue Nov 05 22:42:47 UTC 2019 : Endpoint response headers: [...]
    Tue Nov 05 22:42:47 UTC 2019 : Successfully completed execution
    Tue Nov 05 22:42:47 UTC 2019 : Method completed with status: 200

最后成功(通过API网关的“测试”)

{
  "statusCode": 200,
  "headers": {
    "Content-Type": "application/json"
  },
  "body": "\"foo\""
}

如何通过URL使其正常工作?我尝试从客户端(浏览器)实际获取一些标头参数,但需要逐步学习。似乎通过URL调用时,我什至无法从API网关传递到Lambda,但是为什么呢?从概念上讲,我该怎么办? :(

1 个答案:

答案 0 :(得分:0)

我正确设置了所有内容,但不知道我必须重新部署API才能公开进行更改。

这就是答案:再次部署API。