aws-apigateway: introduce convenience method for adding CORS headers to authorizer responses #33437
Labels
@aws-cdk/aws-apigateway
Related to Amazon API Gateway
effort/medium
Medium work item – several days of effort
feature-request
A feature should be added or improved.
p2
Describe the feature
When using custom authorizers with API Gateway, CORS headers are not automatically added to 401 responses since these responses come directly from the authorizer before reaching any Lambda integration. This requires developers to manually add Gateway Responses with CORS headers for each API Gateway instance, leading to repetitive boilerplate code.
This proposal suggests adding a convenience method
addAuthorizerCORSHeaders()
to theRestApi
class to streamline this common use case.Use Case
When building web applications that use API Gateway with custom authorizers, browsers require proper CORS headers even for unauthorized (401) responses. Currently, developers need to manually add Gateway Responses like this for each API:
This is repetitive and easy to forget, leading to CORS issues that are hard to debug.
Proposed Solution
Add a simple convenience method to
RestApi
:Usage would be as simple as:
For cases requiring custom CORS settings, developers can still use the
GatewayResponse
construct directly.Other Information
This is a common issue that many developers face when working with API Gateway custom authorizers, as evidenced by:
Acknowledgements
CDK version used
2.178.2
Environment details (OS name and version, etc.)
Ubuntu 24.04.1
The text was updated successfully, but these errors were encountered: