【问题标题】:sam local start-api go lambda returns 502 "internal server error" on linux ubuntusam local start-api go lambda 在 linux ubuntu 上返回 502“内部服务器错误”
【发布时间】:2023-04-08 06:37:02
【问题描述】:

在本地运行 aws sam 应用程序时返回“内部服务器错误”。

我使用以下方法创建了 aws sam hello-world 示例:
sam init --runtime go1.x --name robertsamlocallyhelloworld

然后我在本地运行应用程序:

sam local start-api

然后:

curl 127.0.0.1:3000/hello

图像下载已开始,然后在一段时间后停止。

/hello 端点返回:502 "Internal server error"

我正在使用:
山姆·克里:1.15.0
码头工人:20.10.0

输出:

Invoking hello-world (go1.x)<br>
Image was not found.<br>
Building image....................................<br>
Skip pulling image and use local one: amazon/aws-sam-cli-emulation-image-go1.x:rapid-1.15.0.<br>

Mounting /home/robert/projects/try_dir/try_sam_go_daemons/robertsamlocallyhelloworld/hello-world as /var/task:ro,delegated inside runtime container<br>
START RequestId: 159c8e80-649d-4c71-8b54-3221387af308 Version: $LATEST<br>
fork/exec /var/task/hello-world: no such file or directory: PathError<br>
null<br>
END RequestId: 159c8e80-649d-4c71-8b54-3221387af308<br>
REPORT RequestId: 159c8e80-649d-4c71-8b54-3221387af308  Init Duration: 0.38 ms  Duration: 9.30 ms   Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 128 MB <br>
Lambda returned empty body!<br>
Invalid lambda response received: Invalid API Gateway Response Keys: {'errorMessage', 'errorType'} in {'errorMessage': 'fork/exec /var/task/hello-world: no such file or directory', 'errorType': 'PathError'}<br>
2021-01-11 23:55:41 127.0.0.1 - - [11/Jan/2021 23:55:41] "GET /hello HTTP/1.1" 502 <br>
2021-01-11 23:55:41 127.0.0.1 - - [11/Jan/2021 23:55:41] "GET /favicon.ico HTTP/1.1" 403 <br>

【问题讨论】:

  • 很明显错误是'fork/exec /var/task/hello-world: no such file or directory'表示sam local start-api有问题
  • 我解决了。在init 之后,我运行make 或sam build。

标签: ubuntu go aws-lambda aws-sam aws-sam-cli


【解决方案1】:

错误信息明确指出:

Invalid API Gateway Response Keys

我遇到了类似的错误,我收到了响应 status_code 的错误:

Invalid lambda response received: Invalid API Gateway Response Keys: {'status_code'} in {'status_code': 200, 'body': '"Success!!"'}

显然,SAM 试图模拟 API 网关,而 status_code 不是 API 网关在响应中期望的键之一。因此我把它从status_code改成了statusCode的key

    return {
    'statusCode': 200,
    'body': json.dumps("Success!!")
}

而且,效果很好。

我建议您查看 API Gateway 响应键并尝试将您的响应映​​射到该键。

【讨论】:

    【解决方案2】:

    我也收到此错误,因为本地调用无权访问 template.yml 中引用的环境变量。 例如:

    ...
     #template.yml
         Policies:
        # Give Create/Read/Update/Delete Permissions to the SampleTable
        - DynamoDBCrudPolicy:
            TableName: !Ref SampleTable
      Environment:
        Variables:
          # Make table name accessible as environment variable from function code during execution
          SAMPLE_TABLE: !Ref SampleTable
    
    ...
    

    一种解决方案是部署应用程序并从控制台获取环境变量,然后将它们输入到“env.json”或类似文件中:

    {
    "getAllItemsFunction": {
        "SAMPLE_TABLE": "dev-demo-SampleTable-*ID*"
    },}
    

    然后您可以通过将-e env.json 添加到sam local invoke command 来在本地开发中使用已部署的表。 像这样:

    sam local invoke getAllItemsFunction -e events/event-get-all-items.json -n env.json
    

    老实说,不知道 SAMPLE_TABLE 和“!Ref SampleTable”之间的连接在哪里发生,但它确实有效。 看看这个:https://youtu.be/NzPqMrdgD1s?t=799

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-02
      • 2019-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 2019-12-21
      相关资源
      最近更新 更多