Cost estimation

View as Markdown

Buddy estimates infrastructure costs from Terraform or OpenTofu plans using Infracost before Terraform Apply. The run displays estimated monthly costs for individual resources and exports cost totals as variables for use in approval conditions, notifications, and trigger conditions of subsequent actions.

Image loading...Run with the monthly cost before and after the change next to each resource

Infracost estimates costs for supported AWS, Azure, and Google Cloud resources. Resources that cannot be priced are excluded from the cost estimate.

Supported actions

Action How to enable Variables
Terraform Plan cost_estimation in the action BUDDY_ACTION_TF_PLAN_COST_*
Terraform Drift Detection cost_estimation in the action BUDDY_ACTION_TF_DRIFT_COST_*
Terraform Cost Estimation separate action after Plan BUDDY_ACTION_TF_COST_*

Use the dedicated Terraform Cost Estimation action when cost estimation requires a separate configuration, such as a different usage file or trigger conditions.

Before you begin

Add an Infracost integration with an Infracost Cloud key. In YAML, you reference it by its ID in the integration field.

During the run, Buddy downloads Infracost from GitHub (github.com/infracost/infracost/releases). The action must be able to reach this address, and a custom image (tool: CUSTOM) must contain curl or wget, and tar.

Enable cost estimation in Terraform Plan

In the Terraform Plan action, on the Run tab, select Cost estimation and choose the Infracost integration.

Image loading...Terraform Plan action with cost estimation enabled and an Infracost integration selected

yaml
- action: Plan
  type: TERRAFORM_PLAN
  tool: OPENTOFU
  version: 1.12.6
  working_directory: infra
  backend:
    type: ARTIFACT
    artifact: infra-state
    artifact_version: prod
  cost_estimation:
    enabled: true
    integration: infracost
    usage_file: infracost-usage.yml
    fail_on_error: true
Field Description
enabled Enables cost estimation. Default: false
integration ID of the Infracost integration. Required when enabled: true
usage_file Path to the Infracost usage file relative to working_directory. See Usage-based costs
fail_on_error true: the action fails if Infracost encounters an error. false (default): the error is logged and the action completes without a cost estimate

Cost estimates in run details

For each resource in the plan that changes, the run shows the monthly cost. Deleted and replaced resources also show the cost before the change. Above the list are the total after the change, the total before the change, and the difference.

Unchanged resources don't appear on the list, but they count toward the monthly total. The total covers the whole configuration in the working directory.

Example for a plan that changes an m5.large instance to t3.small and deletes a NAT Gateway (us-east-1, no usage file):

Resource Action Before After
aws_instance.app replace 70.88 15.98
aws_nat_gateway.egress delete 32.85 -
Total (with an unchanged EBS volume, 1.60) 105.33 17.58

The difference is -87.75 USD per month, or -83.3%.

Buddy also sends the same result to the Git provider as a commit status, so it shows up on the pull request.

Variables

Plan exports BUDDY_ACTION_TF_PLAN_COST_*:

Variable Example Description
BUDDY_ACTION_TF_PLAN_COST_MONTHLY 17.58 Monthly cost after the change
BUDDY_ACTION_TF_PLAN_COST_PAST_MONTHLY 105.33 Monthly cost before the change
BUDDY_ACTION_TF_PLAN_COST_DIFF_MONTHLY -87.75 Difference. Negative when the cost goes down
BUDDY_ACTION_TF_PLAN_COST_DIFF_PERCENT -83.3 Difference in percent. Not set when the cost before the change is 0
BUDDY_ACTION_TF_PLAN_COST_CURRENCY USD Currency
BUDDY_ACTION_TF_PLAN_COST_UNPRICED_COUNT 0 Number of resources Infracost didn't price

Amounts have two decimal places, percentages have one. The full list of prefixes is in Output variables.

Info
Set thresholds on DIFF_MONTHLY, not on DIFF_PERCENT. The first plan of a new stack has a cost of 0 before the change, so the percentage is not set and a condition on it won't work.

Approval based on cost increases

In the following example, the Approve Run action pauses the run when the estimated monthly cost increases by more than 50 USD. Changes that do not exceed this threshold proceed to Terraform Apply without approval. The threshold is configured using a variable-based trigger condition:

yaml
- action: Plan
  type: TERRAFORM_PLAN
  tool: OPENTOFU
  version: 1.12.6
  working_directory: infra
  cost_estimation:
    enabled: true
    integration: infracost
    fail_on_error: true
- action: Approve cost increase
  type: WAIT_FOR_APPLY
  description: "Monthly cost: $BUDDY_ACTION_TF_PLAN_COST_PAST_MONTHLY -> $BUDDY_ACTION_TF_PLAN_COST_MONTHLY $BUDDY_ACTION_TF_PLAN_COST_CURRENCY (+$BUDDY_ACTION_TF_PLAN_COST_DIFF_MONTHLY)"
  trigger_conditions:
    - trigger_condition: VAR_GREATER_THAN
      trigger_variable_key: BUDDY_ACTION_TF_PLAN_COST_DIFF_MONTHLY
      trigger_variable_value: "50"
- action: Apply
  type: TERRAFORM_APPLY
  tool: OPENTOFU
  version: 1.12.6
  working_directory: infra

VAR_GREATER_THAN compares numbers, including decimals: 105.33 triggers the approval, while 9.99 and -87.75 skip it.

Image loading...Run stopped at the approval with the cost before and after the change

Warning
In a pipeline with a threshold, set fail_on_error: true. With false, an Infracost failure ends Plan without an estimate. The condition on DIFF_MONTHLY is then not met and Apply runs without approval.

You can combine the cost threshold with the rule for destroyed resources from Conditional apply. Add the cost condition as another operand in OR.

Terraform Cost Estimation action

The Terraform Cost Estimation action estimates infrastructure costs from a plan file generated by an earlier Terraform Plan action in the same run. By default, it reads the plan file specified by $BUDDY_ACTION_TF_PLAN_OUTPUT_FILE.

Image loading...Terraform Cost Estimation action form with an Infracost integration and the list of exported variables

yaml
- action: Estimate cost
  type: TERRAFORM_COST_ESTIMATION
  tool: OPENTOFU
  version: 1.12.6
  working_directory: infra
  cost_estimation:
    integration: infracost
    usage_file: infracost-usage.yml
  • Set the same working_directory, tool, and version as in the Plan action. The plan is read in the working directory of this action.
  • The action fails if Infracost encounters an error. The fail_on_error option is not available.
  • The action exports cost estimates as BUDDY_ACTION_TF_COST_* variables using the same suffixes as Terraform Plan.

Without a Plan action earlier in the run, the plan file variable is empty and the action fails with Failed to read the given file as a state or plan file.

Drift cost

With cost_estimation enabled, the Terraform Drift Detection action also prices the difference between the state and what actually runs in the cloud. The result goes to BUDDY_ACTION_TF_DRIFT_COST_*, so a drift notification can include the cost of the changed resources:

yaml
- action: Drift check
  type: TERRAFORM_DRIFT_DETECTION
  tool: OPENTOFU
  version: 1.12.6
  working_directory: infra
  fail_on_drift: false
  cost_estimation:
    enabled: true
    integration: infracost
- action: Notify
  type: SLACK
  integration: my-slack
  channel: "#infra"
  content: "Drift: $BUDDY_ACTION_TF_DRIFT_SUMMARY. Monthly cost: $BUDDY_ACTION_TF_DRIFT_COST_PAST_MONTHLY -> $BUDDY_ACTION_TF_DRIFT_COST_MONTHLY $BUDDY_ACTION_TF_DRIFT_COST_CURRENCY ($BUDDY_ACTION_TF_DRIFT_COST_DIFF_MONTHLY)"
  trigger_conditions:
    - trigger_condition: VAR_IS
      trigger_variable_key: BUDDY_ACTION_TF_DRIFT_DETECTED
      trigger_variable_value: "true"

To run the check on a schedule, see Drift detection.

Usage-based costs

Some infrastructure costs depend on resource usage, such as data processed by a NAT Gateway, Lambda and S3 requests, or database queries. Without estimated usage data from a usage file or Infracost Cloud, these usage-based charges are not included in the cost estimate.

The usage file provides estimated volumes. The path is relative to working_directory:

yaml
version: 0.1
resource_usage:
  aws_nat_gateway.egress:
    monthly_data_processed_gb: 500

With this file, the NAT Gateway cost goes up to 55.35 USD per month. The format and the list of fields for each resource are in the Infracost documentation.

When there are no volumes, Infracost prices resources without usage-based costs and reports no error.

Warning
An invalid usage file disables the cost threshold In tests of the integration in Buddy, a usage file with invalid YAML syntax caused no error, and Infracost returned 0.00 for all resources. fail_on_error: true won't stop the action in that case, the condition on DIFF_MONTHLY won't be met, the approval will be skipped, and Apply will run. Check every change to the usage file in a pull request run before it reaches the branch that runs Apply.

Limitations

  • Only AWS, Azure, and Google Cloud resources are priced. UNPRICED_COUNT gives the number of skipped resources.
  • Cost estimates are based on list prices and do not include discounts, Savings Plans, or credits. You can specify EC2 Reserved Instances in the usage file.
  • Usage-based costs require estimated volumes from a usage file or from Infracost Cloud.
  • Each action with cost estimation uses one CI run from the Infracost account limit, including plans on pull requests.

See also

Last modified on Nov 25, 2026