[apigateway] defaultMethodOptions not available when using RestApi.fromRestApiAttributes()
See original GitHub issueThe new asset system and fromRestApiAttributes()
updates have been great, they have solved some of my long held problems.
I came across this in the last step of splitting up my API stacks, everything looked ok until I realised the endpoints were missing their options
methods.
Maybe this is intended, but I couldn’t find an explanation in the docs.
When adding methods to a restApi that was imported using RestApi.fromRestApiAttributes()
, defaultCorsPreflightOptions
and defaultMethodOptions
don’t seem to be imported.
If this is intended, is there a way to set defaultCorsPreflightOptions
and defaultMethodOptions
on an imported rest api?
Thanks, Justin
Reproduction Steps
Stack 1: create a rest api and pass it to a nested stack
this.restApi = new apigateway.RestApi(this, `${platform}-api`, {
defaultCorsPreflightOptions: {
allowOrigins: apigateway.Cors.ALL_ORIGINS,
allowMethods: apigateway.Cors.ALL_METHODS,
allowHeaders: apigateway.Cors.DEFAULT_HEADERS,
allowCredentials: true,
},
defaultMethodOptions: {
authorizationType: apigateway.AuthorizationType.NONE,
methodResponses: [
{
statusCode: "200",
responseModels: {
"application/json": new EmptyModel(),
},
responseParameters: {
"method.response.header.Access-Control-Allow-Headers": true,
"method.response.header.Access-Control-Allow-Methods": true,
"method.response.header.Access-Control-Allow-Credentials": true,
"method.response.header.Access-Control-Allow-Origin": true,
},
},
],
},
});
...
new OrderApiStack(this, "orderApiStack",{
...props,
restApiId:this.restApi.restApiId,
rootResourceId:this.restApi.restApiRootResourceId
}
);
stack 2: import and add a method to the rest api
const restApi = RestApi.fromRestApiAttributes(this, "RestApi", {
restApiId: props.restApiId!,
rootResourceId: props.rootResourceId!,
});
...
restApi.root
.resourceForPath("orders")
.addMethod("GET", integration, {
authorizer: { authorizerId: apiStack.authorizer.ref },
authorizationType: AuthorizationType.COGNITO,
});
Environment
- CLI Version : 1.49.1
- Framework Version: 1.49.1
- Node.js Version: v12.13.1
- OS : OSX
- Language (Version): TypeScript
This is 🐛 Bug Report
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:8 (5 by maintainers)
Top GitHub Comments
Copied from https://github.com/aws/aws-cdk/issues/9574 by @sblackstone
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-apigateway-readme.html#breaking-up-methods-and-resources-across-stacks
Not sure to report this as a bug or documentation issue, but the solution presented here breaks a number of things. For example, if you have defaultCorsFlightOptions, they won’t be carried over since the api’s are looked up in each stack by ID.
The solution I finely settled on was to manually call addCorsPreflight(…opts…) on each resource and push the method into the array so the stage waits for all of the OPTIONS methods to be created as well before building the deployment. You can’t even pass the cors options into the resource’s constructor as a prop since that method will be added silently and you won’t have access to it to make the deployment dependent on the method.
FYI - I wrote a blog post about how to get ApiGatewayV2 working with CORS after a long struggle.
https://medium.com/@steveb3210/apigatewayv2-cloudfront-distribution-and-cors-via-the-cdk-9bae7f22ecf8
Maybe this will help someone.