[英]sam local start-api go lambda returns 502 "internal server error" on linux ubuntu
curl 127.0.0.1:3000/hello
then I run app locally using:然后我使用以下方法在本地运行应用程序:
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>
then:然后:
curl 127.0.0.1:3000/hello
Image downloading has started and then stopped after a while.图像下载已经开始,然后在一段时间后停止。
/hello
endpoint returns: 502 "Internal server error"
/hello
端点返回: 502 "Internal server error"
I am using:我在用:
Sam Cli: 1.15.0 Sam Cli:1.15.0
Docker: 20.10.0 Docker:20.10.0
Output: 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>
The error message clearly states:错误消息清楚地指出:
Invalid API Gateway Response Keys
I faced a similar error in which I received error for the response status_code:我遇到了类似的错误,其中我收到了响应 status_code 的错误:
Invalid lambda response received: Invalid API Gateway Response Keys: {'status_code'} in {'status_code': 200, 'body': '"Success!!"'}
Apparently, SAM tries to mock the API Gateway and status_code was not one of the keys API Gateway expects in the response.显然,SAM 试图模拟 API 网关,而 status_code 不是 API 网关在响应中期望的键之一。 Therefor I changed it to key from
status_code
to statusCode
因此,我将其更改为从
status_code
到statusCode
的键
return {
'statusCode': 200,
'body': json.dumps("Success!!")
}
And, it worked fine.而且,它工作得很好。
I would suggest you take a look at API Gateway response keys and try to map your response to that.我建议您查看 API 网关响应密钥并尝试 map 您对此的响应。
I also got this error because the local invocation doesn't have access to environment variables referenced in the template.yml.我也收到此错误,因为本地调用无权访问 template.yml 中引用的环境变量。 eg.:
例如。:
...
#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
...
One solution is to deploy the app and fetch the env variables from the console, then input them into a "env.json" or similar:一种解决方案是部署应用程序并从控制台获取环境变量,然后将它们输入到“env.json”或类似文件中:
{
"getAllItemsFunction": {
"SAMPLE_TABLE": "dev-demo-SampleTable-*ID*"
},}
Then you can use the deployed table in your local development by adding -e env.json
to the sam local invoke command
.然后,您可以通过在
sam local invoke command
中添加-e env.json
在本地开发中使用已部署的表。 Like this: sam local invoke getAllItemsFunction -e events/event-get-all-items.json -n env.json
像这样:
sam local invoke getAllItemsFunction -e events/event-get-all-items.json -n env.json
Honestly dont know where the connection between SAMPLE_TABLE and ",Ref SampleTable" is happening.老实说,不知道 SAMPLE_TABLE 和“,Ref SampleTable”之间的连接发生在哪里。 but it works: Take a look at this: https://youtu.be/NzPqMrdgD1s?t=799
但它有效:看看这个: https://youtu.be/NzPqMrdgD1s?t=799
Same answer I wrote on https://stackoverflow.com/a/72067740/93074我在https://stackoverflow.com/a/72067740/93074上写的相同答案
So from the following tutorial covering how to get lambdas and api-gateway working using CDK , I managed to isolate that without the following line will result in the 502 BAD GATEWAY error experienced, with the suggested return type as described.因此,从以下介绍如何使用 CDK 使 lambdas 和 api-gateway 工作的教程中,我设法隔离了如果没有以下行将导致遇到 502 BAD GATEWAY 错误,并使用所描述的建议返回类型。 It's in the
new apigateway.RestApi
props.它在
new apigateway.RestApi
道具中。
defaultCorsPreflightOptions: {
...
allowOrigins: ['http://localhost:3000'],
},
The op doesn't specify his infrastructure propositioning method.该操作没有指定他的基础设施提议方法。 If not using the CDK and using Cloud Formation YAML then it's probably related to the equivalent in the expanded YAML (although the.net result of the expansion is beyond my competency).
如果不使用 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.