Estimate AWS Costs in Terraform Pull Requests with Infracost

· 2 min read · Terraform & OpenTofu Tutorials

Know the price before you apply #

A single line of Terraform can add hundreds of dollars a month: a NAT gateway, a larger RDS instance, a load balancer. Infracost reads the code or the plan and estimates the monthly cost using public cloud prices. It works with Terraform and OpenTofu, and supports AWS, Azure and Google Cloud.

Install and use #

$ infracost auth login          # free API key
$ infracost breakdown --path .

Example of the output for a configuration with a NAT gateway and an EC2 instance:

 Name                                  Monthly Qty  Unit   Monthly Cost

 aws_nat_gateway.public_a
 ├─ NAT gateway                                730  hours        $36.00
 └─ Data processed                  Monthly cost depends on usage: $0.045 per GB

 aws_instance.web
 ├─ Instance usage (Linux, t3.micro)             730  hours         $7.59
 └─ root_block_device
    └─ Storage (general purpose SSD, gp3)         8  GB            $0.64

 OVERALL TOTAL                                                    $44.23

The prices and format depend on the region and on the version of the tool, so treat the example as an illustration. The estimate does not include usage-based charges (data transfer, requests) unless you provide a usage file.

Compare with the current state: diff #

$ infracost breakdown --path . --format json --out-file base.json
# change the code
$ infracost diff --path . --compare-to base.json

The diff command shows the difference in monthly cost caused by your change.

Usage estimates #

For resources that bill by usage, such as S3, Lambda or data transfer, create a usage file:

infracost-usage.yml
version: 0.1
resource_usage:
  aws_lambda_function.api:
    monthly_requests: 5000000
    request_duration_ms: 200
  aws_s3_bucket.assets:
    standard:
      storage_gb: 500
      monthly_tier_1_requests: 1000000
$ infracost breakdown --path . --usage-file infracost-usage.yml

In pull requests #

Add a step to the GitHub Actions pipeline that posts the cost difference as a comment on each pull request, using the infracost/actions/setup action and infracost comment github. Reviewers then see "+$38/month" next to the code change. You can also set a policy that fails the check when the increase passes a threshold.

Keep demo and tutorial costs low #

When you follow the AWS with Terraform tutorials:

  • Use a separate AWS account for demos and set a budget alert in AWS Budgets.
  • Run tofu destroy when you finish. The most expensive items are NAT gateways, load balancers, RDS instances and unattached Elastic IPs, which are charged by the hour.
  • Prefer small instance types (t3.micro, db.t4g.micro) and check the free tier conditions for your account.
  • Tag all resources (resource tagging) and use Cost Explorer to filter by tag.
  • Delete forgotten resources: an old EBS volume or snapshot still costs money.

Cost estimation is part of the Terraform best practices.

#Terraform #OpenTofu #AWS #Finops