我们用路径“ / customers”和方法“ GET”创建了一个“ AWS :: Serverless :: Function”。
我们现在正在尝试使用路径“ / 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)。