From 13379824aa6c025d02bba5708e4cce9ad306c744 Mon Sep 17 00:00:00 2001 From: ezekielelvis Date: Sun, 2 Aug 2026 11:58:55 +0000 Subject: [PATCH] fix: make ApiStack default stage depend on its throttled routes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Second deploy failure after #4: EventTicketing-dev-Api CREATE_FAILED on the DefaultStage resource — "Unable to find Route by key POST /auth/confirm-signup within the provided RouteSettings". The stage's RouteSettings override referenced routes by literal string key, which CloudFormation validates against routes that already exist — but nothing told CFN the Stage had to wait for those Route resources, so it could be created/updated before them. Fix: build the RouteSettings map from the actual HttpRoute objects returned by addRoutes() as routes are created, and add an explicit defaultStage.node.addDependency() for each throttled route at the same time. Confirmed in the synthesized template: DefaultStage now has an explicit DependsOn covering all 7 throttled routes' Route/Integration/ Permission resources, and RouteSettings keys match 1:1. Co-Authored-By: Claude Sonnet 5 --- infrastructure/lib/stacks/api-stack.ts | 49 ++++++++++++++++---------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/infrastructure/lib/stacks/api-stack.ts b/infrastructure/lib/stacks/api-stack.ts index 9b07c7a..01c1952 100644 --- a/infrastructure/lib/stacks/api-stack.ts +++ b/infrastructure/lib/stacks/api-stack.ts @@ -64,9 +64,9 @@ export class ApiStack extends Stack { }); // Baseline throttle protects every public/unauthenticated route from - // simple volumetric abuse; the six /auth/* routes get a much tighter - // limit since they're the highest-value target for credential - // stuffing, signup spam, and password-reset enumeration. + // simple volumetric abuse; these routes get a much tighter limit + // since they're the highest-value target for credential stuffing, + // signup spam, password-reset enumeration, and contact-form spam. const defaultStage = new HttpStage(this, 'DefaultStage', { httpApi: this.httpApi, stageName: '$default', @@ -74,22 +74,17 @@ export class ApiStack extends Stack { throttle: { rateLimit: 50, burstLimit: 100 }, }); - // Per-route throttle overrides aren't exposed on the HttpStage L2 - // (only the stage-wide `throttle` default is), so this drops to the - // L1 `RouteSettings` CFN property directly. Keep these keys in sync - // with the `/auth/*` entries in the `routes` array below. + const THROTTLED_ROUTE_KEYS = new Set([ + 'POST /auth/signup', + 'POST /auth/confirm-signup', + 'POST /auth/login', + 'POST /auth/refresh', + 'POST /auth/forgot-password', + 'POST /auth/reset-password', + 'POST /contact', + ]); const AUTH_ROUTE_THROTTLE = { ThrottlingRateLimit: 5, ThrottlingBurstLimit: 10 }; - (defaultStage.node.defaultChild as CfnStage).addPropertyOverride('RouteSettings', { - 'POST /auth/signup': AUTH_ROUTE_THROTTLE, - 'POST /auth/confirm-signup': AUTH_ROUTE_THROTTLE, - 'POST /auth/login': AUTH_ROUTE_THROTTLE, - 'POST /auth/refresh': AUTH_ROUTE_THROTTLE, - 'POST /auth/forgot-password': AUTH_ROUTE_THROTTLE, - 'POST /auth/reset-password': AUTH_ROUTE_THROTTLE, - // Same tight limit as /auth/* — the other highest-value target for - // spam since it's unauthenticated and triggers an outbound email. - 'POST /contact': AUTH_ROUTE_THROTTLE, - }); + const routeSettings: Record = {}; const authorizer = new HttpJwtAuthorizer('CognitoAuthorizer', props.userPool.userPoolProviderUrl, { jwtAudience: [props.userPoolClient.userPoolClientId], @@ -149,14 +144,30 @@ export class ApiStack extends Stack { this.applyGrants(fn, route, props); this.functions[route.id] = fn; - this.httpApi.addRoutes({ + const httpRoutes = this.httpApi.addRoutes({ path: route.path, methods: [route.method], integration: new HttpLambdaIntegration(`${route.id}Integration`, fn), authorizer: route.authenticated ? authorizer : undefined, }); + + const routeKey = `${route.method} ${route.path}`; + if (THROTTLED_ROUTE_KEYS.has(routeKey)) { + routeSettings[routeKey] = AUTH_ROUTE_THROTTLE; + // RouteSettings (set below) references these routes by key, which + // CloudFormation validates against routes that already exist — + // without this, the Stage can be created/updated before its + // Route resources, and deployment fails with "Unable to find + // Route by key ... within the provided RouteSettings". + httpRoutes.forEach((r) => defaultStage.node.addDependency(r)); + } } + // Per-route throttle overrides aren't exposed on the HttpStage L2 + // (only the stage-wide `throttle` default is), so this drops to the + // L1 `RouteSettings` CFN property directly. + (defaultStage.node.defaultChild as CfnStage).addPropertyOverride('RouteSettings', routeSettings); + new CfnOutput(this, 'ApiUrl', { value: this.httpApi.apiEndpoint }); new CfnOutput(this, 'UserPoolId', { value: props.userPool.userPoolId }); new CfnOutput(this, 'UserPoolClientId', { value: props.userPoolClient.userPoolClientId });