Skip to content

feat(cdk): deploy preflight for the version quota of the input guardrail #1088

Backlog

Description

@krokoko

Problem

A deploy that publishes a version of the input guardrail fails when the guardrail is at Bedrock's quota of versions, and the stack rolls back. The quota is 20 versions per guardrail and cannot be raised (Versions per guardrail in the Amazon Bedrock service quotas). #1079 hit it on a guardrail that had versions 16 to 34 and DRAFT: 19 numbered versions.

Since #1084 the logical ID of the version contains a hash of the guardrail's configuration, so a deploy publishes a version only when the guardrail changed. Two ways to the quota remain:

  1. The upgrade. The first deploy with fix: guardrail version quota, credentials race, late reconciler writes; CI cleanup #1084 publishes one more version, because the logical ID changes one last time: from the name that createVersion() of the alpha library gave (InputGuardrailGuardrailVersion…) to InputGuardrailVersion<hash>…. On a stack that earlier deploys brought to the quota, that deploy cannot create the version, and the stack rolls back as in Deploys stop once the input guardrail reaches its version quota #1079. The deployment guide says to count the versions before that deploy, but an operator who has not read the entry finds it only after the rollback.
  2. Over time. Every published version is retained, because a retained version of the orchestrator function can still name it (publishGuardrailVersion in cdk/src/stacks/agent.ts), and nothing deletes a replaced one. Each change of the guardrail adds a version, and so does a deploy that publishes one and then rolls back. A long-lived stack can reach the quota again.

Both points come from the review of #1084 (upgrade, over time). In that review a long-lived stack went from 11 to 12 published versions on its first deploy with the change, and stayed at 12 on the next.

What exists

  • The guide entry. docs/guides/DEPLOYMENT_GUIDE.md:524-572, "Deploys stop once the input guardrail reaches its version quota (Deploys stop once the input guardrail reaches its version quota #1079)", under "Known deployment issues". Line 539 says: "The first deploy with the fix publishes one more version, because the logical ID changes one last time. A guardrail at its quota needs the cleanup below before that deploy." Since fix: guardrail version quota, credentials race, late reconciler writes; CI cleanup #1084 its "Affects" paragraph (line 526) also says to count the guardrail's versions before the first deploy with the fix. The entry then lists the versions (aws bedrock list-guardrails --guardrail-identifier <guardrail-id>), the versions that functions name (aws lambda list-functions --function-version ALL, by GUARDRAIL_ID and GUARDRAIL_VERSION) and the unfinished durable executions, and deletes the other versions (aws bedrock delete-guardrail --guardrail-identifier <guardrail-id> --guardrail-version <n>).
  • Two preflights on the deploy path. mise //cdk:deploy (cdk/mise.toml:145-173) runs scripts/preflight-bootstrap.ts, then scripts/preflight-log-delivery.ts, then cdk deploy, each with the arguments after --. bgagent setup deploys through that task (cli/src/commands/setup.ts), so it runs them too. A bare cdk deploy runs neither.
    • preflight-bootstrap.ts is read-only (aws cloudformation describe-stacks). It reports PASS, WARN or FAIL (cdk/src/bootstrap/preflight.ts), prints the verdict as JSON with --json, exits 1 on FAIL and is skipped with ABCA_SKIP_BOOTSTRAP_PREFLIGHT=1. mise //cdk:preflight runs it alone.
    • preflight-log-delivery.ts reads the resources of the stack that is being deployed (aws cloudformation list-stack-resources). It takes the stack name from --stack-name, -c stackName=, STACK_NAME, cdk.context.json or cdk.json, else backgroundagent-dev (resolveStackName), forwards --profile and --region, and treats a stack that does not exist as a fresh install. Its tests (cdk/test/scripts/preflight-log-delivery.test.ts) run the script with a stand-in aws on PATH.
  • The resources. The guardrail is the AWS::Bedrock::Guardrail of the root stack whose logical ID starts with InputGuardrail. Its name is task-input-guardrail-<stack name>, cut off after 50 characters. The stack has one AWS::Bedrock::GuardrailVersion.
  • The API. ListGuardrails with guardrailIdentifier (an ID or an ARN) lists every version of one guardrail, in pages (API reference). The reference does not say whether that listing holds DRAFT; Deploys stop once the input guardrail reaches its version quota #1079 reports its guardrail with versions 16 to 34 and DRAFT. The IAM action is bedrock:ListGuardrails. The CloudFormation execution role already has it (BedrockGuardrailsAndLogging in cdk/src/bootstrap/policies/observability.ts), but a preflight runs with the operator's own credentials, as the two above do.

Proposal

A third preflight, read-only: cdk/scripts/preflight-guardrail-versions.ts, run by mise //cdk:deploy before cdk deploy, and on its own as mise //cdk:preflight:guardrail-versions.

  1. Resolve the stack, the profile and the Region as preflight-log-delivery.ts does. Share that code instead of copying it a third time.
  2. Read the stack's resources. No stack: nothing to check. Otherwise take the physical ID of the guardrail and the logical IDs of its version resources.
  3. List the guardrail's versions and count the numbered ones.
  4. Report:
    • FAIL, before cdk deploy builds or uploads an asset, when the guardrail is at its quota and the stack's version does not have the hash name yet (InputGuardrailVersion, 16 hexadecimal digits, and the 8 characters that CDK appends). That deploy is certain to publish a version. The message gives the count, says that the deploy would fail and roll back, and names the guide entry.
    • WARN when the guardrail is at its quota and the version already has the hash name (the next change of the guardrail cannot be published), and when the count is close to the quota, for example within five.
    • PASS otherwise, with the count in the line, so that every deploy log shows it.
  5. ABCA_SKIP_GUARDRAIL_PREFLIGHT=1 skips it. --json prints the verdict, as in the bootstrap preflight.

To decide when implementing:

  • What happens when the versions cannot be read (no AWS CLI, or no bedrock:ListGuardrails for the operator). The two existing preflights stop the deploy. This one guards against a rollback, not against a loss, so a warning that names the missing permission may be enough.
  • Whether the quota counts DRAFT. The one observation (Deploys stop once the input guardrail reaches its version quota #1079) is a refusal at 19 numbered versions.
  • Whether bgagent setup makes the check exact. It runs cdk diff before it deploys, and the diff shows whether a version would be created.

Not proposed: deleting unused versions automatically. Whether a version is unused depends on the unfinished durable executions of every retained version of the orchestrator function (see the guide entry).

Acceptance criteria

  • With a stand-in aws, as in preflight-log-delivery.test.ts:
    • no stack: exit 0, and Bedrock is not called;
    • a version resource without the hash name and a guardrail at its quota: exit 1, and the message has the count and names the guide entry;
    • the same version resource and a count close to the quota: exit 0 with a warning;
    • a version with the hash name and a guardrail at its quota: exit 0 with a warning;
    • a count below the warning threshold: exit 0, and the line has the count;
    • a listing that comes in two pages is counted whole;
    • --profile, --region and the stack name reach both calls;
    • ABCA_SKIP_GUARDRAIL_PREFLIGHT=1: exit 0 without a call.
  • The stand-in records every call, and the script makes none that changes anything.
  • mise //cdk:deploy runs the preflight before cdk deploy, and a FAIL stops the task. bgagent setup shows the FAIL line.
  • The guide entry, cdk/scripts/README.md and the command list of cdk/AGENTS.md describe the preflight and name the permission it needs.
  • One run against a deployed stack confirms what was only read so far: that the physical ID of the guardrail resource is accepted as --guardrail-identifier (the L1 in aws-cdk-lib reads Ref of this type as the ARN, and the CloudFormation reference leaves that Ref undescribed), whether the listing of one guardrail holds DRAFT, and the count at which Bedrock refuses the next version.

Activity

  1. added
    enhancement
    infra-cdk
    on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementinfra-cdk

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions