--- name: deploy description: >- Deploy, diff, or destroy the ABCA CDK stack. Handles pre-deployment validation, synthesis, and post-deployment verification. Use when the user says "deploy", "cdk deploy", "deploy the stack", "destroy", "cdk diff", "what changed", "redeploy", or "update the stack". --- # ABCA Deployment You are managing CDK deployment for the ABCA platform. Determine the user's intent and execute the appropriate workflow. ## Determine Action Ask the user (or infer from context) which action they want: - **deploy** — Build and deploy the CDK stack - **diff** — Show what would change without deploying - **destroy** — Tear down the stack (requires explicit confirmation) - **synth** — Synthesize CloudFormation without deploying ## Pre-Deployment Checks Before any deployment action, verify: 1. **Build is clean:** ```bash mise run build ``` This runs agent quality checks, CDK compilation + tests, CLI build, and docs build. Do NOT deploy if the build fails. Note: a passing build is noisy — it prints many `ERROR`/`WARN` and cdk-nag lines from test fixtures. Trust the **exit code (0 = pass)**, not the log volume. 2. **Docker is running** — Required for CDK asset bundling. 3. **Build host architecture** — The agent image targets `linux/arm64` (AgentCore is Graviton). On an **x86_64** host without QEMU/binfmt, the deploy fails partway with `exec /bin/sh: exec format error`. Register emulation with `docker run --privileged --rm tonistiigi/binfmt --install arm64`, or deploy from a native arm64 host (Graviton / Apple Silicon). The registration is kernel state and does not survive a reboot (stopping and starting a cloud dev box counts), so a host that deployed before can fail again: run the command again after a restart; repeating it is harmless. Skip on arm64 hosts. 4. **AWS credentials are configured** — `aws sts get-caller-identity` (confirm it's the intended account/region). ## Deploy Workflow Deploy and update with `bgagent setup`. It reads the stack's deployed features, checks Region support, reviews `cdk diff`, gates data loss, prepares the bootstrap and configures the CLI. A bare `mise //cdk:deploy` has none of those checks. 1. Ask the user which optional features the stack should have: `none`, or any of `slack`, `linear`, `jira`, `deployPreviewScreenshots`, `agentRegistry`, `toolGateway`. For an existing stack, read its `DeploymentFeatures` output first and offer that as the default, so nothing is removed by accident. 2. Run setup from `cli/`. Your shell has no terminal, so pass `--features` and `--yes`. Run it in the background (about 10 minutes); the log is under `~/.bgagent/logs//setup-deploy.log`. ```bash cd cli node lib/bin/bgagent.js setup --region --stack-name --features --yes ``` - A new installation also takes `--compute agentcore` (or `ecs`, `agentcore,ecs`). An existing stack keeps its backends. - Pass any context the stack was deployed with, for example `-c networkTopology=split`. - **Never pass `--accept-data-loss`.** If setup refuses because the change would delete state-bearing resources (tables, secrets, buckets), stop. Tell the user what would be removed, and ask them to run `node lib/bin/bgagent.js setup` themselves in a terminal, where they confirm by typing the stack name. 3. To change code without changing features, run the same command with the stack's current features. Use `mise //cdk:deploy -- --require-approval never` only when the user asks for it explicitly. It applies `abca.deployment.json` if that file exists, and otherwise deploys every optional feature. After a successful deployment, retrieve and display stack outputs: ```bash aws cloudformation describe-stacks --stack-name backgroundagent-dev \ --query 'Stacks[0].Outputs' --output table ``` Key outputs to highlight: `ApiUrl`, `RuntimeArn`, `UserPoolId`, `AppClientId`, `GitHubTokenSecretArn`, `DeploymentFeatures`. ## Diff Workflow ```bash mise //cdk:diff ``` Summarize the changes: new resources, modified resources, removed resources. Flag any potentially destructive changes (resource replacements, security group changes). ## Destroy Workflow **CRITICAL: Ask for explicit confirmation before destroying.** Use AskUserQuestion to confirm, explaining consequences. ```bash mise //cdk:destroy ``` **Teardown can stall in `DELETE_FAILED`** on a security group / private subnet: AgentCore injects service-managed (Hyperplane) ENIs into the VPC, and AWS reclaims them **asynchronously, with no fixed delay**, after the runtime is gone. It can take hours. - Do not promise the user a wait time, and do not try to force-detach the ENIs. They belong to `amazon-aws`. - While the ENIs exist, a retry of `mise //cdk:destroy` fails on the same security group and subnets. Before a retry, check that they are gone, with the inspection commands under "Teardown blocked by AgentCore network interfaces" in `docs/guides/DEPLOYMENT_GUIDE.md`. - Do **not** use `--deletion-mode FORCE_DELETE_STACK`, and do not retain the stuck VPC resources on your own initiative. What is left behind stays in the account, outside CloudFormation, and VPCs are quota-capped per Region. - If the user cannot wait, show them that section of the deployment guide. It has the command to finish the deletion with `--retain-resources`, and says what that leaves them to clean up. Also note: a first-create failure leaves the stack in `ROLLBACK_COMPLETE`, which can't be updated — destroy and redeploy fresh. ## Synth Workflow ```bash mise //cdk:synth ``` Output goes to `cdk/cdk.out/`. Useful for reviewing generated CloudFormation templates. ## Post-Deployment After a successful deploy, remind the user to: - Store/update the GitHub PAT in Secrets Manager if this is a fresh deployment. - Onboard a repository. `bgagent repo onboard ` is a runtime operation (no redeploy) that works when the repo can use the **platform/default-blueprint** setup — the default GitHub token secret, an already-granted model, and the default egress allowlist. A repo that needs its **own** config — a per-repo GitHub token, a model not yet granted to the runtime, custom egress domains, Cedar HITL policies, or system-prompt overrides — needs a dedicated CDK `Blueprint` construct and a redeploy (with the correct permissions). See the `onboard-repo` skill for both paths. - **Verify readiness before submitting a task:** `bgagent platform doctor` smoke-checks the API, Cognito, GitHub token, Bedrock model access, and onboarded repos — confirm everything is green first. - (Lower-level alternative) raw API smoke test: `curl -s -H "Authorization: $TOKEN" $API_URL/tasks`. ## Least-Privilege Bootstrap (the default) `mise //cdk:bootstrap` provisions a **custom least-privilege** CloudFormation execution role by default — NOT `AdministratorAccess` (ADR-002). It deploys `cdk/bootstrap/bootstrap-template.yaml`, which creates scoped `IaCRole-ABCA-*` managed policies (Infrastructure / Application / Observability) generated from `cdk/src/bootstrap/policies/`. The task passes `--force` to `cdk bootstrap`, so it replaces the bootstrap (the `CDKToolkit` stack) that the account and Region already have, for every CDK app that deploys through it. That includes a standard CDK bootstrap and one with a newer CDK bootstrap version than the task installs. `mise //cdk:bootstrap -- --no-force` cancels the flag for a run. The task then leaves such a bootstrap unchanged and still exits 0. Ask the user before you run the task, unless the user has said that no other CDK app deploys in that account and Region. What the replacement changes for the other CDK apps is described under "Using these policies" in `docs/design/DEPLOYMENT_ROLES.md`. A consequence worth knowing when you **add a new resource type or a new feature on an existing resource**: the scoped role must allow the IAM action CloudFormation will call, or the deploy rolls back with `AccessDenied` on that action (e.g. `s3:PutBucketVersioning`, `lambda:TagResource`). The fix is to add the action to the relevant policy in `cdk/src/bootstrap/policies/`, regenerate (`mise //cdk:bootstrap:generate`), re-bootstrap, and redeploy. The policy source and the `DEPLOYMENT_ROLES.md` golden doc are kept in sync by tests. See `docs/design/DEPLOYMENT_ROLES.md` for the complete IAM policies, trust policy, runtime role inventory, and tightening recommendations.