Skip to content

Component Infra Stacks

Reese Hodge edited this page Nov 19, 2026 · 11 revisions

Component Infra Stacks

Branch review qualification (2026-11-15)

Source-Agent-Infra-Deployment-Review checks 98fadf7 on the unmerged architecture-review branch. The standalone JWT Runtime V2 construct and Data stack now exist there, but app composition still uses guest Cognito and the old budget path and does not instantiate the new runtime. Planned asset publication must be separated from bootstrap synthesis. The historical resource descriptions below do not establish that this branch is deployable.

The infra package defines four CDK stacks: WebSocket/Lambda plumbing, monthly budget enforcement, Cognito guest identity, and static frontend hosting. It also contains the WebSocket Lambda handler described in Component-WebSocket-Transport. The CDK code read on 2026-09-26 does not define an AgentCore runtime construct and does not define an AgentCore Memory construct.

The guest identity pool remains current code, not the deployment target: constitution v1.5.0 requires invite-only, allowlisted sign-in and forbids unauthenticated spend-bearing access. The architecture review must replace this path before first deployment; the constitution amendment added no infrastructure.

Files

  • packages/infra/lib/agent-stack.ts
  • packages/infra/lib/budget-stack.ts
  • packages/infra/lib/cognito-stack.ts
  • packages/infra/lib/frontend-stack.ts
  • packages/infra/lib/handlers/websocket-handler.ts

Exported classes and props

  • class AgentStack extends cdk.Stack
  • interface BudgetStackProps extends cdk.StackProps { unauthRole: iam.Role; unauthPolicyName: string; alertEmail: string; }
  • class BudgetStack extends cdk.Stack
  • interface CognitoStackProps extends cdk.StackProps { agentRuntimeArn: string; }
  • class CognitoStack extends cdk.Stack
    • identityPoolId: string
    • unauthRole: iam.Role
    • unauthPolicyName: string
  • class FrontendStack extends cdk.Stack
  • handler: APIGatewayProxyWebsocketHandlerV2

AgentStack

Current resources:

  • Imports two SSM SecureString parameters by name for weather and news API keys.
  • Creates WebSocketHandler Lambda from lib/handlers, Node.js 22.x, 30 second timeout, 256 MB memory.
  • Does not set the WebSocket Lambda's CloudWatch log retention or log level explicitly; see Concept-Observability.
  • Publishes a Lambda version and live alias for rollback.
  • Grants the Lambda read access to those two secure parameters.
  • Creates an API Gateway v2 WebSocket API with route selection expression $request.body.type.
  • Creates Lambda proxy integration.
  • Creates $connect route with AWS_IAM authorization.
  • Creates $disconnect and $default routes.
  • Creates prod stage with auto deploy and route throttling.
  • Grants API Gateway permission to invoke the Lambda alias.
  • Grants the Lambda execute-api:ManageConnections for the WebSocket API.
  • Outputs the WebSocket endpoint, live alias ARN, and expected SSM parameter names.

Important verification: no AgentCore runtime resource and no AgentCore Memory resource are constructed in agent-stack.ts. There is no CfnMemory, memory ID output, memory policy, or AgentCore memory wiring in the stack code read.

BudgetStack

Current resources:

  • SNS topic BudgetHardStopTopic. Anything published to it triggers the hard stop, so only the 100% notification publishes there.
  • Topic policy letting budgets.amazonaws.com call sns:Publish, with aws:SourceAccount and aws:SourceArn conditions that restrict it to this account's budgets. Both are built from CDK tokens, never a literal account ID.
  • Inline Node.js 22 Lambda that deletes a named inline policy from the unauth role.
  • Does not set the hard-stop Lambda's CloudWatch log retention or log level explicitly; see Concept-Observability.
  • IAM permission allowing that Lambda to delete the specific role policy.
  • Lambda subscription to the hard-stop topic.
  • AWS Budgets monthly cost budget named for Max Height, with actual-cost notifications at 50%, 80%, and 100% of a 10 USD monthly amount. All three email the owner. Only 100% also publishes to the hard-stop topic.
  • Output HardStopTopicArn. Nothing consumes it.

The owner's address comes from the alertEmail prop, which bin/app.ts reads from the CDK context key budgetAlertEmail. The constructor throws if it is missing or blank, and the message names the key. Every cdk synth, diff and deploy of the app therefore needs -c budgetAlertEmail=<owner email>. The address is never committed, because the repo is public.

Warning routing (#56, fixed 2026-10-03)

Until 2026-10-03 the 50%, 80% and 100% notifications all published to one topic, and the hard-stop Lambda was that topic's only subscriber. It never checked which threshold fired, so a $5 warning would have revoked guest access. No person was subscribed either, so P2's $5 and $8 email alarms emailed nobody.

A second bug hid the first. The topic had no access policy for budgets.amazonaws.com. AWS requires one, so as written Budgets could not have delivered any notification at all, the $10 hard stop included. Fixing only the grant would have armed the routing bug, so both changed together.

Warnings now go by email only, with no SNS subscriber. That makes the bug structurally impossible rather than filtered at runtime. The two runtime alternatives were rejected:

  • Parse the threshold in the Lambda. AWS does not document the format of a Budgets SNS message, so a parser fails either open (warnings delete the policy) or closed (the hard stop never fires).
  • An SNS subscription filter policy. It needs message attributes or a JSON body to match on, and Budgets is not documented to provide either.

Each budget notification allows one SNS subscriber and up to ten email subscribers, so the warnings' SNS slot stays free. A future machine-readable $8 signal can use it for the soft-degrade (FR-016), which today has a frontend consumer and no producer. That would add a topic, so it is a Spec Kit trigger.

All of this is verified against synthesized templates only. Nothing has been deployed, so no real notification has been delivered.

Hard-stop policy name (#61)

The 100% budget notification is meant to drive the hard-stop Lambda, which removes the unauthenticated role policy supplied by CognitoStack. As written it would silently do nothing (issue #61). The Lambda deletes the literal name CognitoUnauthRoleDefaultPolicy, but CDK's Role.addToPolicy generates the inline policy's name with a hash suffix. A synth on 2026-10-02 produced CognitoUnauthRoleDefaultPolicy4E16848D. The delete would therefore hit NoSuchEntityException, which the handler logs as "Policy already detached" and treats as success. budget-stack.test.ts passes the same literal to a mock role, so the test agrees with the bug. Nothing has been deployed, so it is latent.

Even with the correct name, the hard stop only removes the guest role's InvokeAgentRuntime. The frontend reaches the agent through the WebSocket API's Lambda, whose own role the hard stop never touches. The mechanism is an open question for the architecture review.

CognitoStack

Current resources:

  • Cognito Identity Pool with unauthenticated identities enabled.
  • Federated unauthenticated IAM role for Cognito Identity.
  • Policy allowing bedrock-agentcore:InvokeAgentRuntime on props.agentRuntimeArn.
  • Policy allowing Polly neural synthesis with VoiceId constrained to Matthew; resource is *.
  • Identity pool role attachment.
  • Output for the identity pool ID.

The stack exposes unauthRole and unauthPolicyName so Concept-Budget-Ceiling enforcement can detach the policy.

FrontendStack

Current resources:

  • Private S3 bucket with block-public-access, S3-managed encryption, SSL enforcement, destroy removal policy, and auto-delete objects.
  • CloudFront response headers policy with CSP, content-type options, frame deny, strict-origin referrer policy, and HSTS.
  • The current CSP's connect-src allows 'self', wildcard API Gateway WebSockets, and Cognito Identity hosts. It does not allow a Polly host.
  • CloudFront distribution with S3 origin access control, HTTPS redirect, index.html root, and SPA-style 403/404 fallback to /index.html.
  • Outputs for distribution URL and bucket name.

useAudio.ts calls synthesizeTurn in pollyTts.ts, which constructs a browser-side PollyClient and sends SynthesizeSpeech requests. Once that path has working credentials and an endpoint, the deployed CSP would block those cross-origin requests, preventing the intended Polly voice output. The same policy also has no sign-in user-pool endpoint yet. This is a code-level prediction, not a browser-verified failure: nothing has been deployed. Spec 003 FR-032, FR-036 and FR-050 leave endpoint, credentials and exact CSP hosts to the deployment plan. See Component-Audio-Chain.

SPA fallback swallows API errors (spec 003 finding N6)

frontend-stack.ts:51-62 maps 403 and 404 to /index.html with status 200. CloudFront custom error responses are a DistributionConfig member with no per-behavior form (CloudFront API: CustomErrorResponse), so they apply to every origin on the distribution. Once the runtime sits behind the same distribution, its authorizer's 403 would arrive as an HTML 200, and the client could never tell that its token had expired.

Spec 003's plan replaces errorResponses with a CloudFront Function on the default behavior only. The function rewrites any URI whose last segment has no . to /index.html. Two constraints shaped it:

  • Lambda@Edge was not an option. The AWS Free plan does not support it (supported services, re-read 2026-10-11). The Free plan is temporary (Decision-AWS-Account-Plan), so this constraint lapses. A CloudFront Function remains the cheaper fit for a URI rewrite.
  • cloudfront.Function defaults to the JavaScript 1.0 runtime (aws-cloudfront/lib/function.d.ts:88, aws-cdk-lib 2.272.0), so the plan sets FunctionRuntime.JS_2_0 explicitly.

This is a plan; the stack still has errorResponses. See Source-Spec-003.

Gotchas

  • Known deviation C4 is real in current code: agent-stack.ts does not contain an AgentCore Memory construct.
  • Never hardcode a CDK-generated policy name. Role.addToPolicy names the inline policy <RoleId>DefaultPolicy<hash>, not <RoleId>DefaultPolicy. The hard stop does exactly that, and its idempotent NoSuchEntityException path turns the miss into a false success (see BudgetStack above, issue #61).
  • AWS Budgets cannot publish to an SNS topic without a topic policy granting budgets.amazonaws.com, and CDK's sns.Topic adds none. Without it no notification arrives, the hard stop included (see Warning routing above).
  • One topic cannot route by threshold. Budgets' SNS message format is undocumented, and filter policies have nothing reliable to match on. Give each destination its own subscriber instead.
  • Never publish anything else to BudgetHardStopTopic. Every message revokes guest access. Ops alarms (T111) need their own topic (deviation C8).
  • CloudFront errorResponses are distribution-wide. A SPA's 403/404 fallback rewrites every origin's errors, APIs included, into an HTML 200 (see N6 above).
  • cloudfront.Function defaults to JavaScript 1.0. Set runtime: FunctionRuntime.JS_2_0 explicitly.
  • Lambda@Edge is unavailable on the AWS Free plan, until the account upgrades in December 2026 (Decision-AWS-Account-Plan). A CloudFront Function is cheaper for edge rewrites anyway.
  • Tags.of does not reach CfnBudget. In aws-cdk-lib 2.272.0 it is not ITaggable (aws-budgets/lib/budgets.generated.d.ts:14); a budget's tags must go in its own resourceTags.
  • The budget's defaults count credits. BudgetStack sets no CostTypes, so IncludeCredit is true and Free-plan credits can hide all spend. See Concept-Budget-Ceiling.
  • The deployment CSP omits Polly. The browser calls Polly directly, but connect-src permits only the site, API Gateway WebSockets and Cognito Identity hosts. This is a latent voice failure until the deployment's endpoints and CSP agree; see FrontendStack above.
  • CognitoStackProps requires an agentRuntimeArn, but AgentStack does not create or expose an AgentCore runtime ARN. This closes once the runtime moves into CDK — see below.
  • Planned change (2026-09-27, deviation C7). The AgentCore Runtime is moving into this stack. The AgentCore constructs graduated from @aws-cdk/aws-bedrock-agentcore-alpha into stable aws-cdk-lib/aws-bedrockagentcore, and CloudFormation gained a PlatformVersion property, so the CLI no longer has to own the runtime. Import Runtime, Memory and MemoryStrategy from the stable module. The alpha pin was removed on 2026-09-28 (issue #34); all six constructs T023b and T152 need were confirmed present in stable then, and the import is what proves it from here on. See Decision-AgentCore-Runtime-V2.
  • AgentStack deploys a WebSocket Lambda handler from infra code; it is not the same runtime path as packages/agent/src/index.ts.
  • AgentStack outputs a WebSocket endpoint. Treat deployed endpoint values as public routing data, and avoid copying environment-specific endpoints into public wiki pages.

Clone this wiki locally