sam local start-api go lambda 在 linux ubuntu 上返回

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


我创建了 aws sam hello-world 示例:

sam init --runtime go1.x --name robertsamlocallyhelloworld

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


sam local start-api

然后:


curl 127.0.0.1:3000/hello

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


端点返回:


我正在使用:

Sam Cli:1.15.0

Docker:20.10.0


输出:/hello502 "Internal server error"


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>


千巷猫影
浏览 126回答 3
3回答

函数式编程

错误消息明确指出: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 Gateway 在响应中期望的关键之一。因此,我将其更改为键从到status_codestatusCode&nbsp; &nbsp; return {&nbsp; &nbsp; 'statusCode': 200,&nbsp; &nbsp; 'body': json.dumps("Success!!")}而且,它工作正常。我建议您查看 API 网关响应密钥,并尝试将您的响应映射到该密钥。

慕无忌1623718

我也得到了这个错误,因为本地调用无法访问template.yml中引用的环境变量。例如:...&nbsp;#template.yml&nbsp; &nbsp; &nbsp;Policies:&nbsp; &nbsp; # Give Create/Read/Update/Delete Permissions to the SampleTable&nbsp; &nbsp; - DynamoDBCrudPolicy:&nbsp; &nbsp; &nbsp; &nbsp; TableName: !Ref SampleTable&nbsp; Environment:&nbsp; &nbsp; Variables:&nbsp; &nbsp; &nbsp; # Make table name accessible as environment variable from function code during execution&nbsp; &nbsp; &nbsp; SAMPLE_TABLE: !Ref SampleTable...一种解决方案是部署应用程序并从控制台获取env变量,然后将它们输入到“env.json”或类似文件中:{"getAllItemsFunction": {&nbsp; &nbsp; "SAMPLE_TABLE": "dev-demo-SampleTable-*ID*"},}然后,您可以通过向 .喜欢这个:-e env.jsonsam local invoke commandsam 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使lambda和api网关工作的教程中,我设法隔离了没有以下行将导致502 BAD GATEWAY错误,建议的返回类型如前所述。它在道具中。new apigateway.RestApidefaultCorsPreflightOptions: {...&nbsp; &nbsp; &nbsp; &nbsp; allowOrigins: ['http://localhost:3000'],&nbsp; &nbsp; &nbsp; },该op没有指定他的基础设施命题方法。如果不使用CDK并使用Cloud Formation YAML,那么它可能与扩展的YAML中的等效项有关(尽管扩展的最终结果超出了我的能力范围)。method.response.header.Access-Control-Allow-OriginBrokerAPItest41BB435C:&nbsp; &nbsp; Type: AWS::ApiGateway::Resource&nbsp; &nbsp; Properties:&nbsp; &nbsp; &nbsp; ParentId: !GetAtt 'BrokerAPID825C3CC.RootResourceId'&nbsp; &nbsp; &nbsp; PathPart: test&nbsp; &nbsp; &nbsp; RestApiId: !Ref 'BrokerAPID825C3CC'&nbsp; &nbsp; Metadata:&nbsp; &nbsp; &nbsp; aws:cdk:path: BrokerAwsDeployStack/BrokerAPI/Default/test/Resource&nbsp; BrokerAPItestOPTIONS843EE5C3:&nbsp; &nbsp; Type: AWS::ApiGateway::Method&nbsp; &nbsp; Properties:&nbsp; &nbsp; &nbsp; HttpMethod: OPTIONS&nbsp; &nbsp; &nbsp; ResourceId: !Ref 'BrokerAPItest41BB435C'&nbsp; &nbsp; &nbsp; RestApiId: !Ref 'BrokerAPID825C3CC'&nbsp; &nbsp; &nbsp; AuthorizationType: NONE&nbsp; &nbsp; &nbsp; Integration:&nbsp; &nbsp; &nbsp; &nbsp; IntegrationResponses:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; - ResponseParameters:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Access-Control-Allow-Headers: '''Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Access-Control-Allow-Origin: '''http://localhost:3000'''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Vary: '''Origin'''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Access-Control-Allow-Methods: '''OPTIONS,GET,PUT,POST,DELETE,PATCH,HEAD'''&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StatusCode: '204'&nbsp; &nbsp; &nbsp; &nbsp; RequestTemplates:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; application/json: '{ statusCode: 200 }'&nbsp; &nbsp; &nbsp; &nbsp; Type: MOCK&nbsp; &nbsp; &nbsp; MethodResponses:&nbsp; &nbsp; &nbsp; &nbsp; - ResponseParameters:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Access-Control-Allow-Headers: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Access-Control-Allow-Origin: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Vary: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method.response.header.Access-Control-Allow-Methods: true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StatusCode: '204'&nbsp; &nbsp; Metadata:
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go