# Protect a DynamoDB Global Table and Its KMS Keys from Deletion in an AWS CDK App

Yes. An agent finds the stateful resources in your CDK app, adds retain policies and deletion protection, proves it with assertion tests and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/protect-stateful-resources/aws-cdk/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent clones the app and reads what it would destroy. In an app like `typescript/ddb/global-table-with-cmk` in aws-samples/aws-cdk-examples, `bin/app.ts` creates one `GlobalDDBTableCMK` stack in `us-east-1` with replicas in `us-west-2` and `us-east-2`, and `lib/global-ddb-cmk.ts` splits it into two nested stacks. `lib/stacks/cmk-stack.ts` creates a multi-region `kms.CfnKey` with rotation on, a `CfnAlias`, and an `AwsCustomResource` per replica region that calls `replicateKey` with a policy allowing `kms:*`. `lib/stacks/dynamo-db-stack.ts` creates a `dynamodb.CfnGlobalTable` on pay-per-request billing, keyed on `id`, with KMS encryption per replica and a `KEYS_ONLY` stream. Both are L1 constructs, so neither carries the `Retain` policies the L2 `Table` and `Key` constructs apply by default: a `cdk destroy`, a renamed logical id or a removed nested stack deletes the table in three regions and schedules the key for deletion. There is no `deletionProtectionEnabled` on any replica and no point-in-time recovery. `package.json` pins `aws-cdk-lib` 2.262.1 and `aws-cdk` 2.1133.0 with `jest` 30, `@swc/jest` and `tsx`, and `cdk.json` runs `npx tsc && npx tsx bin/app.ts`, but there is no `test` directory and no test script.

It then applies protection in layers, each in the file that owns the resource. On the `CfnGlobalTable` it calls `applyRemovalPolicy(RemovalPolicy.RETAIN)`, which writes both `DeletionPolicy` and `UpdateReplacePolicy` as `Retain` into the template, and sets `deletionProtectionEnabled: true` on every entry in `replicas`, so a delete attempted in the console or by the CLI is refused until someone turns it off on purpose. It adds `pointInTimeRecoverySpecification` with recovery enabled per replica, so a bad write can be rolled back. On the `CfnKey` it applies the same retain policy and sets `pendingWindowInDays` to the maximum, so a scheduled deletion gives the longest possible window to cancel. The custom resources that replicate the key are left with no policy on purpose, with a comment explaining that deleting the custom resource does not delete the replica keys, and `enableKeyRotation` is kept as it is.

Where a reader would expect a change and not find one, the agent says why. The `CfnAlias` is rebuildable from the key id, the nested stacks themselves are containers, and `addDependency` in `global-ddb-cmk.ts` already orders the key before the table. The README gains a section listing what is protected, what a retained resource costs when a stack is deleted, and the two-step process to delete the table deliberately: disable deletion protection in each region, then remove the retain policy and deploy before destroying.

It proves the change without AWS credentials, which the sandbox does not need. It runs `npm install` and `npx tsc` to compile, then adds `test/global-ddb-cmk.test.ts` using `Template.fromStack` on each nested stack to assert the global table resource has `DeletionPolicy: Retain`, `UpdateReplacePolicy: Retain`, `DeletionProtectionEnabled: true` on every replica and point-in-time recovery on, and that the key has the retain policies and the pending window, and a snapshot of the synthesized template so the next change to either stack shows its diff. It adds a `test` script, runs `jest`, and runs `npx cdk synth --no-lookups` to show the templates render with the policies in place.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Protect the stateful resources in this AWS CDK app from accidental deletion. Read bin/, lib/, cdk.json and package.json first, and list every resource as stateful or rebuildable with a reason, noting which are L1 constructs with no default retain policy. For each stateful resource apply a Retain removal policy, turn on provider-side deletion protection where the resource supports it, enable point-in-time recovery on tables, and set the longest pending deletion window on KMS keys. Leave rebuildable resources alone and comment where protection might be expected. Add a README section on what is protected and how to delete it on purpose. Compile the app, add assertion tests with Template.fromStack for every policy and setting plus a template snapshot, add a test script, run jest and cdk synth, and open a pull request with the stateful-versus-rebuildable table and the command output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request changing `lib/stacks/dynamo-db-stack.ts` and `lib/stacks/cmk-stack.ts`, adding a test file, a snapshot and a `test` script, and extending the README. The description has a table of every resource in both nested stacks, stateful or rebuildable, with the reason, and the exact template keys each protection produced.

The run's record keeps the `tsc` compile, the `jest` run with the assertions and snapshot, and the `cdk synth` output, so a reviewer can read the rendered `DeletionPolicy`, `UpdateReplacePolicy` and replica settings in the template rather than reasoning about the constructs.

Before merging, a reviewer runs `cdk diff` against the deployed stack to confirm the change is an update in place with no replacement, agrees the pending window for the key, and decides whether the team wants deletion protection on in every region or only the primary.

## Run it on every CDK app with a database

A platform engineer hands this off from the terminal once per app: one `POST /o/{org}/sessions` with this body and the repository in `resources`. A team with many CDK apps sends one request per repository, each with a `request_id` that is a UUID v5 of the repository path so a retry returns the run it already started, and the key's `webhook_url` reports each finished run with its pull request.

Each run classifies that app's own resources: an S3 bucket with objects, an RDS instance, a table, a key, an EFS file system. The policy names are the same across CDK languages, so a Python or Go app gets the same change with its assertions written in that language's test framework.

## Why run it on Rush?

Protection is a judgement about what holds state, and with L1 constructs there is no default doing the job for you. The agent reads every construct in its own sandbox, separates the table and the key from the alias and the custom resources, and asserts each policy in the synthesized template before a reviewer opens the diff.

On Rush the compile, the tests and the synth are in the run's record next to the pull request, and the same request runs across every CDK app an organisation owns, so one question, what would a careless destroy take with it, gets a consistent answer in every repository.

## Frequently asked questions

### Will this replace the table on the next deploy?

No. Deletion and update-replace policies are template metadata, and deletion protection and point-in-time recovery are updatable settings on a global table's replicas, so `cdk diff` shows an update in place. Nothing in the key schema or the logical ids changes, which is what would force a replacement.

### Why do the L2 constructs not need this?

The L2 `Table` and `Key` constructs apply a retain policy by default. This app uses `CfnGlobalTable` and `CfnKey` because the L2 global table did not cover a customer-managed key per replica, and L1 constructs carry no default policy, which is exactly why the gap exists.

### How do we delete the table when we mean to?

In two deliberate steps: set `deletionProtectionEnabled` to false on each replica and deploy, then remove the retain policy and deploy again before destroying. The README section the pull request adds spells this out, including that a retained table keeps costing after the stack is gone.

### Does the sandbox need AWS credentials?

No. It compiles the app, synthesizes the templates and asserts on them with the CDK assertions library. The `cdk diff` against the real account is for the reviewer, and the pull request says what it should show.

## More AWS CDK jobs

- [Review AWS CDK Pull Requests](https://getrush.ai/run/review-cdk-changes/aws-cdk/)
- [Upgrade aws-cdk-lib in a CDK App](https://getrush.ai/run/upgrade-aws-cdk/aws-cdk/)
- [Upgrade Retired Lambda Runtimes in an AWS CDK App](https://getrush.ai/run/upgrade-lambda-runtime/aws-cdk/)

## The same job on other stacks

- [Protect Databases and Secrets from Accidental Deletion in a Pulumi Program](https://getrush.ai/run/protect-stateful-resources/pulumi/)

## More jobs like this: Hand off a long task

- [Publish a Helm Chart to an OCI Registry](https://getrush.ai/run/publish-chart-to-oci/helm/)
- [Publish dbt Docs to GitHub Pages](https://getrush.ai/run/publish-dbt-docs/dbt/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
