Skip to content

Commit 30f93bc

Browse files
committed
Add CORS configuration to API Gateway and backend
1 parent 421d355 commit 30f93bc

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

backend/src/iac/backend-stack.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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'),

backend/src/main.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ async function bootstrap() {
1313
// Enable CORS
1414
app.enableCors({
1515
origin: [
16-
'http://localhost:5173', // Vite default dev server
17-
'http://localhost:3000',
18-
'http://localhost:4173', // Vite preview
19-
...(process.env.FRONTEND_URL ? [process.env.FRONTEND_URL] : []),
16+
'*', // Vite default dev server
2017
],
2118
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE,OPTIONS',
2219
credentials: true,

0 commit comments

Comments
 (0)