我正在学习AWS Lambda。我已经创建了一个lambda,它将充当REST API(在CloudFormation术语中为APIEvent
),并希望使用事件在本地调试该lambda。
如果我理解正确的话,运行sam local generate-event apigateway aws-proxy
会生成一个适合本地运行/调试Lambda的事件。这会产生以下事件(一些嵌套的值被缩写):
{
"body": "eyJ0ZXN0IjoiYm9keSJ9",
"resource": "/{proxy+}",
"path": "/path/to/resource",
"httpMethod": "POST",
"isBase64Encoded": true,
"queryStringParameters": {
"foo": "bar"
},
"multiValueQueryStringParameters": {
"foo": [
"bar"
]
},
"pathParameters": {
"proxy": "/path/to/resource"
},
"stageVariables": {
"baz": "qux"
},
"headers": {
...
},
"multiValueHeaders": {
...
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "123456",
"stage": "prod",
"requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef",
"requestTime": "09/Apr/2015:12:34:56 +0000",
"requestTimeEpoch": 1428582896000,
"identity": {
...
},
"path": "/prod/path/to/resource",
"resourcePath": "/{proxy+}",
"httpMethod": "POST",
"apiId": "1234567890",
"protocol": "HTTP/1.1"
}
}
然而,我不明白resource
(在requestContext.resourcePath
中复制)和path
(在requestContext.path
中复制)之间的关系。我应该怎么填这些值呢?
resource
是您在API网关中定义的API资源。例如在您的案例中/{proxy+}
.
path
是来自请求的实际路径。
所以,当你发出请求时GET https://yourdomain.com/v1/pets
:
- 路径为
/v1/pets
- 资源为
/{proxy+}