# Protect Databases and Secrets from Accidental Deletion in a Pulumi Program

Yes. An agent finds the stateful resources in a Pulumi program, adds protect, deletion protection and final snapshots, tests the inputs with mocks and opens a PR.

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

## What the agent does

The agent clones the program into its own cloud sandbox and reads what it creates before deciding what deserves protection. In a program like the `aws-ts-wordpress-fargate-rds` example in pulumi/examples, `Pulumi.yaml` declares a Node.js runtime with template config for `serviceName`, `dbName`, `dbUser` and a secret `dbPassword`; `package.json` pins `@pulumi/pulumi` `3.228.0`, `@pulumi/aws` `7.23.0` and `@pulumi/random` `4.19.2`; and `index.ts` composes three component resources from `network.ts`, `backend.ts` and `frontend.ts`. The stateful part is small and specific: `backend.ts` creates an `rds.SubnetGroup` and an `rds.Instance` running MySQL on `db.t3.micro` with `allocatedStorage: 20`, `skipFinalSnapshot: true` and `publiclyAccessible: false`, parented to the `Db` component, and `index.ts` creates a `random.RandomPassword` for the database when no config password is set and exports it as `databasePassword`. Everything else, the VPC, subnets, security groups, ECS cluster, load balancer, task definition and service in `frontend.ts`, is rebuildable from code. No resource in the program carries `protect`, `retainOnDelete` or `deletionProtection`.

It makes the layered change that stops one `pulumi destroy`, one renamed resource or one removed line from taking the data with it. On the `rds.Instance` it adds the resource option `protect: true`, so Pulumi refuses to delete it until a person unprotects it on purpose, and the AWS-side `deletionProtection: true`, so a deletion attempted outside Pulumi is refused too. It changes `skipFinalSnapshot` to `false` and adds a `finalSnapshotIdentifier` built from the resource name, so a deletion that does go ahead leaves a snapshot behind. On the `RandomPassword` in `index.ts` it adds `protect: true` as well, because a regenerated password is a database nobody can log in to, and it leaves the `rds.SubnetGroup` alone with a comment saying why: it holds no data and recreating it is harmless. The `Db` component's `opts` are left unchanged so a caller can still pass its own options.

Each change is written where it can be understood. The `protect` option goes beside the existing `{ parent: this }` so a reader sees the two options together, each new input has a one-line comment, and the component's README section gains a paragraph on what is protected and how to remove protection when a database really should go: `pulumi state unprotect` on the URN, or setting `protect: false` and running `pulumi up` before the destroy. The agent does not touch the security group in `network.ts` that opens port 3306 to the world, but it notes it in the pull request as a separate finding, since a page about protecting the database should not pretend not to have seen it.

It proves the change without AWS credentials, which the sandbox does not have and does not need. It runs `npm install` and `tsc --noEmit` to show the program compiles, then adds a small unit test using the Pulumi runtime mocks that instantiates the `Db` component and asserts on the `rds.Instance` inputs: `deletionProtection` is true, `skipFinalSnapshot` is false and `finalSnapshotIdentifier` is set. The `protect` option is not visible to mocks, so the test file says so and the pull request shows instead the `pulumi preview --diff` output a reader will see, in which the instance and the password appear with their new options and no resource is replaced. The test runs with the repository's Node toolchain, and the output goes into the description.

## Start it with one API call

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

```json
{
  "agent": "claude",
  "input": "Protect the stateful resources in this Pulumi program from accidental deletion. Read Pulumi.yaml, package.json, every TypeScript file and the README first, and list each resource as stateful or rebuildable with a reason. For the database instance add the protect resource option and provider-side deletion protection, turn off skipping the final snapshot and set a final snapshot identifier; protect the generated database password too; leave rebuildable resources alone and say why in a comment where a reader might expect protection. Do not change networking. Add a README section on what is protected and how to unprotect on purpose. Run npm install and tsc, add a unit test with the Pulumi mocks asserting the database inputs, run it, and open a pull request with the stateful-versus-rebuildable table, any unrelated risk you noticed, 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 `backend.ts` and `index.ts`, adding a mock-based test file and a `package.json` test script, and extending the README. The description has a two-column table of every resource in the program, stateful or rebuildable, with the reason, and a separate paragraph on the open database security group as a finding outside the change.

The run's record keeps `tsc --noEmit` and the test run from the sandbox, so a reviewer sees the program compile and the `rds.Instance` inputs asserted, and can read the diff knowing no resource name changed and therefore no replacement will be proposed on the next `pulumi up`.

Before merging, a reviewer runs `pulumi preview --diff` against the real stack to confirm only the instance and the password show updated options, agrees the snapshot identifier naming, and decides whether the exported `databasePassword` output should stay now that the password is protected.

## Run it on every Pulumi program with a database

A platform engineer hands this off from the terminal once per program: one `POST /o/{org}/sessions` with this body and the repository in `resources`. A team with many stacks sends one request per program, 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 reads that program's resources and decides afresh what is stateful: an S3 bucket with objects, a DynamoDB table, a managed database, a KMS key, a generated secret. The option names are the same in every Pulumi language, so the same body works for a Python or Go program with the test adapted to that language's mocks.

## Why run it on Rush?

Protection is a judgement about which resources hold state, and the judgement needs the whole program in front of it. The agent reads every file in its sandbox, classifies each resource, and applies three layers to the ones that matter, with the compile and the mock test run before the pull request exists, so a reviewer argues about the classification table rather than about whether the code builds.

The same body runs across every Pulumi program an organisation owns, and each pull request keeps its own table and its own test output. That is how a platform team gets a consistent answer to one question, what will a careless destroy take with it, across stacks written by different people at different times.

## Frequently asked questions

### Will this cause a replacement on the next pulumi up?

No. `protect` is a resource option Pulumi stores in state, and `deletionProtection`, `skipFinalSnapshot` and `finalSnapshotIdentifier` are updatable inputs on an RDS instance, so the preview shows an update in place. No resource name or parent changes, which is what would force a replacement.

### How do we delete the database when we really mean to?

In two steps, both deliberate. Run `pulumi state unprotect` on the instance's URN, or set `protect: false` and run `pulumi up`, then turn off `deletionProtection` the same way, then destroy. The final snapshot is still taken because `skipFinalSnapshot` is false. The README section the pull request adds spells this out.

### Why protect the random password?

Because the password is state too. If the `RandomPassword` resource is removed or renamed, Pulumi deletes and recreates it, the new value does not match what the database has, and the site goes down with no error in the program. Protecting it makes that mistake fail loudly at preview time.

### Why not protect everything?

Because `protect` on a rebuildable resource turns a routine change into a two-step chore and teaches people to unprotect without thinking. Subnets, security groups, the ECS service and the load balancer are recreated from code in minutes; the pull request leaves them alone and says why in a comment.

### Does the sandbox need AWS credentials?

No. It compiles the program and runs a unit test with Pulumi's mocks, which asserts on the inputs without calling AWS. The `pulumi preview --diff` against the real stack is for the reviewer, and the pull request says what it should show.

## More Pulumi jobs

- [Review Pulumi Pull Requests](https://getrush.ai/run/review-pulumi-changes/pulumi/)
- [Upgrade the npm Dependencies of a Pulumi Program](https://getrush.ai/run/upgrade-npm-dependencies/pulumi/)
- [Write Unit Tests for a Pulumi Program](https://getrush.ai/run/write-pulumi-unit-tests/pulumi/)

## The same job on other stacks

- [Protect a DynamoDB Global Table and Its KMS Keys from Deletion in an AWS CDK App](https://getrush.ai/run/protect-stateful-resources/aws-cdk/)

## 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/)
