All jobs

Tag Every Resource in a Pulumi Program

Yes. An agent adds automatic tags to every taggable AWS resource in your Pulumi program through a stack transformation, proves it with mocked tests and opens a PR.

Run this on RushOpens 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 it before it decides where tags should come from. In a program like aws-ts-static-website in pulumi/examples, Pulumi.yaml declares a nodejs runtime with targetDomain, pathToWebsiteContents, certificateArn and includeWWW as stack configuration, and package.json pins @pulumi/aws 7.23.0 and @pulumi/pulumi 3.228.0 as an ES module compiled by TypeScript. index.ts creates a content bucket named after the domain, one aws.s3.BucketObject per file under www/, a logs bucket whose ACL comes from acl.ts, an ACM certificate through a second aws.Provider pinned to us-east-1 with its Route 53 validation records, an origin access identity and a CloudFront Distribution named cdn with a PriceClass_100 price class and a custom 404.html. Not one of those resources has a tags argument, and a search of the repository's AWS TypeScript programs finds no defaultTags either.

There are two places tags can live, and the agent uses the one that covers the whole program. Provider default tags, set as aws:defaultTags in the stack configuration or on an aws.Provider, reach only resources created through that provider; here the certificate uses the explicit east provider while everything else uses the default one, so a configuration-only approach would miss the certificate unless both were changed. A stack transformation runs for every resource the program registers, whichever provider it uses. The agent adds tags.ts exporting registerAutoTags(tags), which calls pulumi.runtime.registerStackTransformation and, for resource types that take a tags input, merges the given tags under any the resource already sets. The list of taggable types is explicit and commented: aws:s3/bucket:Bucket, aws:s3/bucketObject:BucketObject, aws:cloudfront/distribution:Distribution and aws:acm/certificate:Certificate are in; BucketPolicy, BucketAcl, BucketPublicAccessBlock, OriginAccessIdentity, the Route 53 records and CertificateValidation are left out because the provider has no tags argument for them.

The tag values are configuration, not constants. index.ts calls registerAutoTags first thing, before the content bucket, with Project from pulumi.getProject(), Stack from pulumi.getStack(), ManagedBy: "pulumi", and an Owner and CostCenter read through the existing pulumi.Config() with stackConfig.require, so each stack sets its own in Pulumi.<stack>.yaml the way targetDomain already is. Pulumi.yaml gains the two new keys under template.config with descriptions, next to the existing ones, and tsconfig.json, which lists only index.ts under files, gains tags.ts so tsc type-checks it.

Proof runs without an AWS account. The agent adds a unit test that calls pulumi.runtime.setMocks in preview mode, imports the program and asserts that every bucket, object, distribution and certificate carries the five tags and that a resource-level tag wins over an automatic one; preview mode matters because index.ts throws on a placeholder targetDomain outside a dry run, and the test uses a placeholder. It runs npm install, npx tsc --noEmit and the test, and records that pulumi preview was not run because the sandbox has no AWS credentials; if the Project holds them and a Pulumi access token as secrets, the prompt can ask for a pulumi preview that shows each resource updating in place with new tags.

Start it with one API call

POST https://api.phoenix.so/o/{org}/sessions with this body:

{
  "agent": "claude",
  "input": "Add automatic resource tags to this Pulumi TypeScript program. Read Pulumi.yaml, package.json, tsconfig.json and every .ts file first. Add tags.ts with a registerAutoTags(tags) function that uses pulumi.runtime.registerStackTransformation to merge tags into every resource whose type takes a tags input, keeping an explicit, commented list of taggable types and leaving resource-level tags in control. Call it at the top of index.ts with Project from pulumi.getProject(), Stack from pulumi.getStack(), ManagedBy pulumi, and Owner and CostCenter read from stack configuration; add those two keys to Pulumi.yaml under template.config with descriptions, and add tags.ts to tsconfig.json. Add a unit test using pulumi.runtime.setMocks in preview mode that asserts every bucket, bucket object, CloudFront distribution and ACM certificate has the tags and that a resource-level tag overrides an automatic one. Run npm install, npx tsc --noEmit and the test. Do not run pulumi up. Open a pull request listing the files, the tag keys and their sources, the taggable and non-taggable resource types in this program, and the test 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 on its own branch that adds tags.ts and the mock-based test, edits index.ts to register the transformation and read the two new configuration values, adds Owner and CostCenter to Pulumi.yaml, lists tags.ts in tsconfig.json, and adds the test runner to package.json. acl.ts and the resource definitions are otherwise unchanged: the tags arrive through the transformation, not through edits to each new aws.s3.Bucket call.

The description has a table of every resource type in the program, whether it is taggable, and the tag keys it will carry, followed by the tsc and test output. The transcript at GET /o/{org}/sessions/{id}/messages keeps npm install, npx tsc --noEmit and the test run with their output, so a reviewer can see the assertions pass rather than read a sentence saying so.

Before merging, a reviewer sets Owner and CostCenter for each stack, checks that the taggable list matches the resources the program really creates, and runs pulumi preview once to see every tagged resource update in place. A follow-up on the same session can add aws:defaultTags for resources created outside this program, or move registerAutoTags into a shared package used by other stacks.

Run it on every new Pulumi program

A platform team with several Pulumi programs runs this once per repository. From a script, send the body above to POST /o/{org}/sessions with each repository in resources, one run per repository, and a request_id that is a UUID v5 of the repository name so a retry is safe. Give the key a webhook_url and every run the key starts posts run.completed with data.session_url when its turn ends; a handler reads the pull request link from the session.

To keep the list honest, add an automation to the Project with a github trigger on pull_request.opened and pull_request.synchronize and a prompt that reads the pull request's diff for a new aws. resource whose type is missing from tags.ts and comments with the line to add. The trigger clones that branch and places the event after the prompt, and the automation starts at most max_runs_per_hour runs in an hour.

The use case this job follows, with its automation and trigger: Hand off a long task.

Why run it on Rush?

Tagging after the fact is a reading job: which resource types accept tags, which provider each resource uses, and which configuration already exists to carry the values. The agent reads the actual program in its own sandbox, writes an explicit taggable list instead of a regular expression, and proves the result with mocks, so the pull request arrives with the decision and the evidence in one place.

Every tsc and test run and its output stay in the run's transcript, so the pull request's claim that each bucket, distribution and certificate is tagged is checkable, and the laptop that handed off the task is free while it happens. Several programs can get the same tags at once, each in its own sandbox and its own pull request, with the same keys in every one.

Frequently asked questions

Why a stack transformation rather than aws:defaultTags?

Default tags attach to a provider, and this program uses two: the default one and an explicit east provider for the ACM certificate. A transformation runs for every resource regardless of provider, and the explicit type list makes it clear what is and is not tagged. The pull request notes that aws:defaultTags remains useful for resources created outside the program.

Will adding tags replace any resource?

No. Tags on S3 buckets, bucket objects, CloudFront distributions and ACM certificates are in-place updates in the AWS provider, so a pulumi preview shows each one as an update rather than a replacement. The test asserts the tags on the resource inputs; the preview, when you run it, shows the same keys on the diff.

What about resources that cannot be tagged?

Bucket policies, ACLs, public access blocks, origin access identities, Route 53 records and certificate validations have no tags input in the provider, so the transformation skips them. They are listed in the pull request so nobody expects to find them in a cost report.

Does the test need AWS credentials?

No. pulumi.runtime.setMocks replaces the engine, so the program's resources are registered in memory and the test reads their inputs. Only pulumi preview and pulumi up need credentials; the agent runs them only when the Project provides them as secrets and the prompt asks.

Can a resource override an automatic tag?

Yes. The transformation merges the automatic tags first and the resource's own tags second, so a bucket that sets Owner keeps its value. One test covers exactly that case so the precedence does not change by accident.

More Pulumi jobs

The same job on other stacks

More jobs like this: Hand off a long task

Keep reading