Sam Local start-API Go Lambda 在 Linux Ubuntu 上返回 502 "internal server error"



在本地运行aws-sam应用程序时返回"内部服务器错误">

我使用:
sam init --runtime go1.x --name robertsamlocallyhelloworld

创建了aws-sam-hello-world示例然后我在本地运行应用程序,使用:

sam local start-api

然后:

curl 127.0.0.1:3000/hello

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

/hello端点返回:502 "Internal server error"
Sam Cli:1.1.5.0
Docker: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>

错误消息清楚地说明:

Invalid API Gateway Response Keys

我遇到了一个类似的错误,我收到了响应状态的错误_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

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

而且,效果很好。

我建议您查看API网关响应密钥,并尝试将您的响应映射到该密钥。

我也收到了这个错误,因为本地调用无法访问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

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

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

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

行动没有具体说明他的基础设施提议方法。如果不使用CDK并使用Cloud Formation YAML,则可能与扩展的YAML中的等价物有关(尽管扩展的最终结果超出了我的能力范围(。

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:

相关内容

  • 没有找到相关文章

最新更新