[apigateway] defaultMethodOptions not available when using RestApi.fromRestApiAttributes()
The 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: Bug Report
Unfortunately, this is not a bug but perhaps a feature request.
It was never intended for defaultMethodOptions or defaultCorsPreflightOptions to be available when imported via fromRestApiAttributes().
For the time being, you have to restate both of these properties for every Resource/Method specified after import.
Fair enough. I decided to turn CORs off and finally put my dev api on a proper subdomain.
Thinking about it a bit more, is CORS not a pretty common use case for api gateway?
And if this approach is the recommended way to split up api stacks, lots of people will encounter this issue when they reach a certain number of api methods.
So they will all have to do some pretty heavy duplication of code across their projects i.e. preflight and method options on every method.
It seems like a bit of a regression.
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.
Hey @nija-at , looks like this is still valid. Is it fine sitting in the backlog as a p2?
This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.
It's a shame that this issue was closed :-/ Looks like this has still not be adressed. I just stumbled on this after following the exakt same guide mentioned in this issue for breaking up a stack. Would be great if this was at least mentioned in the docs.
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.
Hi @justinThompson,
Thanks for opening this issue! After reviewing the details, we found some other discussions that may be duplicates or closely related:
Potential Duplicate Issues
- #29079: Both issues describe the same problem where
fromRestApiAttributes()doesn't preserve configuration likedefaultCorsPreflightOptionsanddefaultMethodOptions. Both specifically mention that the imported API doesn't carry over these settings, leading to missing OPTIONS methods on API resources. They share the same root cause and would require the same fix in the API Gateway module.
Related Issues
-
#33053: This issue shares the same root cause of configuration not being preserved when using
fromRestApiAttributes()but focuses on authorization settings rather than CORS options. Both issues occur in the same API Gateway module and involve the same import method, but affect different aspects of API configuration. -
#9574: This issue reports the same problem where
fromRestApiAttributes()doesn't preserve CORS configuration when breaking up API stacks. It specifically mentions that defaultCorsPreflightOptions isn't carried over when importing an API, and proposes the same workaround of manually calling addCorsPreflight on each resource. The issue also references the same documentation section about breaking up methods and resources across stacks.
This message was generated automatically to help connect related conversations and improve discoverability.
If you feel this issue brings new or distinct information, feel free to add a comment to keep it open. Otherwise, we'll close this issue in 7 days if we don't recieve a response to help keep discussions consolidated.
Please react with 👍 or 👎 to let us know if this response was helpful!
Thank you for helping improve CDK! 🙌
This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.