@@ -452,6 +452,32 @@ export class BackendStack extends cdk.Stack {
452452 reportStatusResource . addCorsPreflight ( corsOptions ) ;
453453 docsResource . addCorsPreflight ( corsOptions ) ;
454454
455+ // Configure Gateway Responses to add CORS headers to error responses
456+ const gatewayResponseTypes = [
457+ apigateway . ResponseType . UNAUTHORIZED ,
458+ apigateway . ResponseType . ACCESS_DENIED ,
459+ apigateway . ResponseType . DEFAULT_4XX ,
460+ apigateway . ResponseType . DEFAULT_5XX ,
461+ apigateway . ResponseType . RESOURCE_NOT_FOUND ,
462+ apigateway . ResponseType . MISSING_AUTHENTICATION_TOKEN ,
463+ apigateway . ResponseType . INVALID_API_KEY ,
464+ apigateway . ResponseType . THROTTLED ,
465+ apigateway . ResponseType . INTEGRATION_FAILURE ,
466+ apigateway . ResponseType . INTEGRATION_TIMEOUT ,
467+ ] ;
468+
469+ gatewayResponseTypes . forEach ( ( responseType ) => {
470+ new apigateway . CfnGatewayResponse ( this , `${ appName } GatewayResponse${ responseType } -${ props . environment } ` , {
471+ restApiId : api . restApiId ,
472+ responseType : responseType . toString ( ) ,
473+ responseParameters : {
474+ 'gatewayresponse.header.Access-Control-Allow-Origin' : "'*'" ,
475+ 'gatewayresponse.header.Access-Control-Allow-Headers' : "'Content-Type,Authorization,X-Amz-Date,X-Api-Key'" ,
476+ 'gatewayresponse.header.Access-Control-Allow-Methods' : "'GET,POST,PUT,PATCH,DELETE,OPTIONS'"
477+ } ,
478+ } ) ;
479+ } ) ;
480+
455481 // Create API Gateway execution role with required permissions
456482 new iam . Role ( this , `${ appName } APIGatewayRole-${ props . environment } ` , {
457483 assumedBy : new iam . ServicePrincipal ( 'apigateway.amazonaws.com' ) ,
0 commit comments