Bitbucket Pipelines Terraform cost estimation on every PR
Add a step after terraform plan that prices the plan and comments the monthly cost on the pull request, with a budget gate that fails the pipeline. Here's the bitbucket-pipelines.yml and how it fits.
Quick answer
Add a step to your pull-request pipeline, after terraform plan, that converts the plan to JSON and prices it. Comment the monthly cost on the PR via the Bitbucket REST API, and exit non-zero past a budget so the pipeline fails and a merge check blocks the PR. It reuses your plan and needs no cloud credentials to estimate.
Bitbucket Pipelines runs your Terraform plan on every PR already. Adding cost is a few lines: price the plan, comment it, and gate the merge, so an expensive change is caught in review, the point of estimating before deploy.
Estimate from the plan
The resolved plan JSON is the most accurate input, see why.
terraform init -input=false
terraform plan -out=tf.plan -input=false
terraform show -json tf.plan > plan.json
c3x estimate --path plan.jsonA pull-request pipeline
pipelines:
pull-requests:
'**':
- step:
name: Terraform cost
image: hashicorp/terraform:latest
script:
- terraform init -input=false
- terraform plan -out=tf.plan -input=false
- terraform show -json tf.plan > plan.json
- c3x estimate --path plan.json --budget 500 --format markdown > cost.md
- ./post-pr-comment.sh cost.md # uses $BITBUCKET_TOKENGate the merge
--budget exits non-zero when the monthly total crosses the line, failing the step and the pipeline. Add a merge check that requires the pipeline to pass, and the over-budget PR is blocked, the budget-guardrail pattern in Bitbucket terms.
Comment on the PR
Post the estimate to the Bitbucket Cloud pull request comments endpoint using a repository access token stored as a secured variable. Comment the monthly total and the delta versus the destination branch, and update one comment in place per PR.
Same pattern, other CI
The plan → JSON → estimate → comment → gate flow is identical in GitHub Actions, GitLab CI, Jenkins, and Azure DevOps. Only the comment API and YAML change.
FAQ
Where does cost estimation go in Bitbucket Pipelines?
In a pull-request pipeline, in a step after terraform plan. Generate the plan, convert it to JSON, and price it. Because the plan resolved variables and modules, its JSON is the most accurate input.
How do I comment the cost on a Bitbucket PR?
Post to the Bitbucket Cloud REST API pull request comments endpoint. Use a repository access token or an app password stored as a secured pipeline variable, and comment the monthly total and the delta versus the destination branch.
How do I block a PR that's over budget?
Have the estimator exit non-zero past a threshold, which fails the step and the pipeline. Require that pipeline to pass with a merge check on the destination branch, so an over-budget PR can't merge until it's fixed.
Does it need cloud credentials to estimate?
The plan step needs your existing cloud credentials; the estimation step doesn't. It reads the plan JSON and prices it against a catalog, so it never calls your cloud account.
Can I cache the setup to keep pipelines fast?
Yes. Cache the Terraform provider plugins and the estimator binary between runs, reuse the plan you already produce, and the cost step adds only seconds.
Does it work with Bitbucket self-hosted runners?
Yes. On a self-hosted runner the estimator needs only the plan file and a pricing catalog, which can be self-hosted for restricted networks.
What to do next
Add the step to one repository and watch the cost comment appear on your next PR. C3X reads your Terraform plan, prices the resources against a live catalog, and comments the monthly cost with optional budget gating, free and self-hostable. The quickstart and CI/CD guide have the details.
Share this post
Try C3X on your own Terraform
Free and open source. No API key required. One command to install, one command to estimate.