繁体   English   中英

sam local start-api go lambda 在 linux ubuntu 上返回 502“内部服务器错误”

[英]sam local start-api go lambda returns 502 "internal server error" on linux ubuntu


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

我使用以下方法创建了 aws sam hello-world 示例:
curl 127.0.0.1:3000/hello

然后我使用以下方法在本地运行应用程序:

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>

然后:

 curl 127.0.0.1:3000/hello

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

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

我在用:
Sam Cli:1.15.0
Docker:20.10.0

Output:

 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>

错误消息清楚地指出:

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_codestatusCode的键

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

而且,它工作得很好。

我建议您查看 API 网关响应密钥并尝试 map 您对此的响应。

我也收到此错误,因为本地调用无权访问 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*"
},}

然后,您可以通过在sam local invoke command中添加-e env.json在本地开发中使用已部署的表。 像这样: sam local invoke getAllItemsFunction -e events/event-get-all-items.json -n env.json

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

我在https://stackoverflow.com/a/72067740/93074上写的相同答案

因此,从以下介绍如何使用 CDK 使 lambdas 和 api-gateway 工作的教程中,我设法隔离了如果没有以下行将导致遇到 502 BAD GATEWAY 错误,并使用所描述的建议返回类型。 它在new apigateway.RestApi道具中。

defaultCorsPreflightOptions: {
...
        allowOrigins: ['http://localhost:3000'],
      },

该操作没有指定他的基础设施提议方法。 如果不使用 CDK 并使用 Cloud Formation YAML,那么它可能与扩展的 YAML 中的等效项相关(尽管扩展的 .net 结果超出了我的能力范围)。

method.response.header.Access-Control-Allow-Origin

BrokerAPItest41BB435C:
    Type: AWS::ApiGateway::Resource
    Properties:
      ParentId: !GetAtt 'BrokerAPID825C3CC.RootResourceId'
      PathPart: test
      RestApiId: !Ref 'BrokerAPID825C3CC'
    Metadata:
      aws:cdk:path: BrokerAwsDeployStack/BrokerAPI/Default/test/Resource
  BrokerAPItestOPTIONS843EE5C3:
    Type: AWS::ApiGateway::Method
    Properties:
      HttpMethod: OPTIONS
      ResourceId: !Ref 'BrokerAPItest41BB435C'
      RestApiId: !Ref 'BrokerAPID825C3CC'
      AuthorizationType: NONE
      Integration:
        IntegrationResponses:
          - ResponseParameters:
              method.response.header.Access-Control-Allow-Headers: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'''
              method.response.header.Access-Control-Allow-Origin: '''http://localhost:3000'''
              method.response.header.Vary: '''Origin'''
              method.response.header.Access-Control-Allow-Methods: '''OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD'''
            StatusCode: '204'
        RequestTemplates:
          application/json: '{ statusCode: 200 }'
        Type: MOCK
      MethodResponses:
        - ResponseParameters:
            method.response.header.Access-Control-Allow-Headers: true
            method.response.header.Access-Control-Allow-Origin: true
            method.response.header.Vary: true
            method.response.header.Access-Control-Allow-Methods: true
          StatusCode: '204'
    Metadata:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM