All jobs

Add Infracost to CI for a Terraform Module

Yes. An agent reads your Terraform module and its examples, adds an Infracost workflow that comments a cost diff on each pull request, and checks it runs.

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 starts from what the repository already runs on a pull request instead of adding a parallel toolchain. In a module like terraform-aws-modules/terraform-aws-vpc it reads .github/workflows/pre-commit.yml, which pins TFLINT_VERSION and TERRAFORM_DOCS_VERSION, collects every root directory with clowdhaus/terraform-composite-actions/directories, and runs terraform_validate at the module's minimum and maximum Terraform versions. It reads .pre-commit-config.yaml too, where pre-commit-terraform runs terraform_fmt, terraform_docs, terraform_tflint with a fixed list of rules and terraform_validate. Nothing in either file estimates cost, so the agent adds a separate workflow rather than squeezing a new step into the pre-commit matrix.

Next it works out what Infracost can price. A module has no state and no provider configuration of its own, so the agent prices the examples: examples/complete, examples/simple, examples/ipv6-dualstack and the others under examples/, each a root with its own main.tf that calls source = "../../". It reads main.tf at the root and finds the resources that carry an hourly or per-GB price, aws_nat_gateway with its aws_eip, aws_vpn_gateway, and aws_flow_log in vpc-flow-logs.tf, next to free ones like aws_subnet, aws_route_table and the fourteen aws_network_acl_rule resources. It also reads versions.tf, which requires Terraform >= 1.0 and hashicorp/aws >= 6.28, so the workflow installs a Terraform in that range.

Then it writes .github/workflows/infracost.yml on pull_request, with infracost/actions/setup, a baseline run against the base branch (infracost breakdown --path examples/complete and the other example paths, written to a JSON file) and a diff run against the pull request's checkout (infracost diff --compare-to), ending with infracost comment github --behavior update so one comment is edited per pull request instead of a new one per push. The API key comes from a GitHub Actions secret the pull request description names, never from the workflow file. It adds an infracost.yml usage file only when a resource needs one for a meaningful estimate, such as the data processed by a NAT gateway, and explains the assumption in the pull request.

Before pushing, the agent runs terraform init -backend=false and terraform validate in each example it points Infracost at, and infracost breakdown locally when a key is available to the run, so the paths and flags in the workflow are ones it has seen work. It commits the workflow on its own branch, opens the pull request, and reads the workflow run that pull request starts. When a step fails, for example because an example directory needs a variable the baseline run does not have, it fixes the workflow and pushes again until the cost comment appears on its own pull request.

Start it with one API call

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

{
  "agent": "claude",
  "input": "Add Infracost to this Terraform repository's CI. Read the existing workflows and .pre-commit-config.yaml first and do not change them. Add .github/workflows/infracost.yml that runs on pull requests, installs Infracost, prices every root under examples/ as a baseline from the base branch and as a diff from the pull request, and posts one updating cost comment on the pull request. Read the API key from a GitHub Actions secret named INFRACOST_API_KEY and say in the pull request that it must be created. Run terraform validate in each example you price before you push. Open a pull request and make sure the new workflow passes on it; if a step fails, fix it and push again.",
  "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 a new branch that adds .github/workflows/infracost.yml and, when a resource needs it, an infracost-usage.yml with the usage assumptions written out. In a module shaped like terraform-aws-vpc the workflow prices each examples/* root, so the comment shows which example's NAT gateways, VPN gateway or flow logs carry a monthly figure and which resources are free. The pull request description lists the example paths it prices, the secret the workflow reads, the Terraform version it installs and why it chose that range from versions.tf.

The proof is on the pull request itself: the Infracost workflow runs there and leaves its cost comment, and the existing pre-commit workflow still passes because nothing in it changed. The session keeps the full record of every command the agent ran in its sandbox, including the terraform validate output for each example and any workflow log it read after a failed push.

Before merging, a reviewer checks that the INFRACOST_API_KEY secret exists in the repository or organization, that the example directories priced are the ones the team keeps up to date, that the usage assumptions match how the module is typically deployed, and that --behavior update is the comment behaviour they want. Once merged, every later pull request gets a cost diff without anyone running Infracost on a laptop.

Run it on every new Terraform repository

Add a GitHub organization webhook for the repository event and, when one is created with Terraform files, have your server send this same request with that repository's URL in resources. Each repository gets its own sandbox, its own run and its own pull request, so a module or environment repository created from a template gets a cost workflow the day it exists. The Rush GitHub App must be installed on that repository for resources to reach it; GET /o/{org}/github lists what it can clone.

Set a webhook URL on the API key and Rush POSTs run.completed or run.failed to it when each run's turn finishes, with session_url so your server can post the pull request link to the platform team's channel. Send a request_id with each request so a retry after a network error returns the run already started instead of opening a second pull request. A backfill across many existing repositories can be sent in full: the key runs up to max_concurrent, 4 by default and up to 100, and the Terraform repositories beyond that answer 202 with status: "queued" and start, oldest first, when a slot frees.

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

Why run it on Rush?

Adding a cost workflow by hand means pushing, waiting for the run, reading why the baseline step could not initialise an example, and pushing again, with your working copy parked on the branch the whole time. On Rush the agent does that loop in its own sandbox: it runs terraform validate in each example first, pushes, reads the failed step's log on GitHub and fixes it itself. Your laptop is not involved, and no Infracost or Terraform install touches your machine.

The result is checkable rather than claimed. The cost comment on the agent's own pull request is the evidence the workflow works, and the trajectory keeps every command with its output, so a reviewer can see which example roots were validated and why a usage file was or was not added. Because each run is independent, the same request can add the workflow to every Terraform repository in the organization at once, each behind a pull request a person reviews.

Frequently asked questions

Will it change my pre-commit workflow or my tflint rules?

No. The prompt tells it to leave existing workflows and .pre-commit-config.yaml alone, and it adds a new .github/workflows/infracost.yml beside them. If a file with that name already exists, it says so in the pull request instead of overwriting it. Everything it changes is in the pull request diff.

My repository is a module, not a deployment. What does Infracost price?

The example roots under examples/, because a module on its own has no provider and no concrete resource counts. In terraform-aws-vpc each example calls the module with source = "../../", so pricing examples/complete prices the module's NAT gateways, VPN gateway and flow logs as that example configures them. For a repository that is itself a deployment, the agent prices its root directories instead.

Where does the Infracost API key go?

In a GitHub Actions secret, referenced from the workflow as ${{ secrets.INFRACOST_API_KEY }}. The pull request description says the secret must be created before the workflow can post a comment. The key is never written into the workflow file or committed anywhere in the branch.

How does it handle resources whose cost depends on usage?

NAT gateway data processing and similar usage-based prices need a usage file for a meaningful estimate. When one of the module's resources is like that, the agent adds an infracost-usage.yml with the assumption spelled out and names it in the pull request, so the team can replace the figure with what they actually see. Without a usage file Infracost prices only the fixed hourly part.

Does it wait for the workflow to pass before it finishes?

Yes. It reads the workflow run on its own pull request, and when a step fails it reads that step's log, fixes the cause and pushes again. If the failure is outside the repository, for example the secret not existing yet, it stops and says exactly that in its answer and in the pull request instead of reporting success.

More Terraform jobs

More jobs like this: Hand off a long task

Keep reading