我们创建了一个“AWS::Serverless::Function”,路径为“/customers”,方法为“GET”。
我们现在正在尝试使用路径“/customers”和方法“OPTIONS”创建一个“AWS::ApiGateway::Method”
我们可以通过手动使用 /customers 的 API ID 和资源 ID 来做到这一点。但是,我们不想对这些 ID 进行硬编码。
我们可以使用“${ServerlessRestApi}”获取 RestApiId,使用“${ServerlessRestApi.RootResourceId}”获取根 (/) 资源 ID,但是我们需要 /customers 而不是 /。
"Customers" : {
"Type" : "AWS::Serverless::Function",
"Properties": {
"Handler": "Cloudhouse.AWSRegistration.Core::Cloudhouse.AWSRegistration.Core.LambdaEntryPoint::FunctionHandlerAsync",
"Runtime": "dotnetcore2.1",
"CodeUri": "",
"MemorySize": 256,
"Timeout": 30,
"Role": null,
"Policies": [ "AWSLambdaFullAccess" ],
"Environment" : {
"Variables" : {
"AuthDomain": { "Ref": "AuthDomain" },
"Audience": { "Ref": "Audience" },
"AllowedScopes": {"Ref": "AllowedScopes"}
}
},
"Events": {
"Registration": {
"Type": "Api",
"Properties": {
"Path": "/customers",
"Method": "GET" }
}
}
}
},
"CustomersOptionsMethod": {
"Type": "AWS::ApiGateway::Method",
"Properties": {
"AuthorizationType": "NONE",
"RestApiId": { "Fn::Sub" : "${ServerlessRestApi}" },
"ResourceId": { "Fn::Sub": "${ServerlessRestApi.RootResourceId}" },
"HttpMethod": "OPTIONS",
"Integration": {
"IntegrationResponses": [
{
"StatusCode": 200,
"ResponseParameters": {
"method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
"method.response.header.Access-Control-Allow-Methods": "'GET,OPTIONS'",
"method.response.header.Access-Control-Allow-Origin": "'*'"
},
"ResponseTemplates": {
"application/json": ""
}
}
],
"PassthroughBehavior": "WHEN_NO_MATCH",
"RequestTemplates": {
"application/json": "{\"statusCode\": 200}"
},
"Type": "MOCK"
},
"MethodResponses": [
{
"StatusCode": 200,
"ResponseModels": {},
"ResponseParameters": {
"method.response.header.Access-Control-Allow-Headers": true,
"method.response.header.Access-Control-Allow-Methods": true,
"method.response.header.Access-Control-Allow-Origin": true
}
}
]
}
}
我们需要将 ${ServerlessRestApi.RootResourceId} (/) 更改为 ${ServerlessRestApi.CustomersId} (/customers)。