# Estimate AWS Costs in Terraform Pull Requests with Infracost

> How to see the monthly cost of Terraform and OpenTofu changes before applying them with Infracost, and how to keep AWS demo costs under control.

- Source: https://www.itwonderlab.com/terraform-cost-estimation-infracost/
- Published: 2026-10-02
- Updated: 2026-10-02
- Author: Javier Ruiz Jiménez (https://www.javierruizjimenez.com/)
- Site: IT Wonder Lab (https://www.itwonderlab.com/)

---

## 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](https://www.infracost.io/) 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

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

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

```shell
 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

```shell
$ 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](https://www.itwonderlab.com/aws-s3/), [Lambda](https://www.itwonderlab.com/aws-lambda/) or data transfer, create a usage file:

```yaml title="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
```

```shell
$ infracost breakdown --path . --usage-file infracost-usage.yml
```

### In pull requests

Add a step to the [GitHub Actions pipeline](https://www.itwonderlab.com/terraform-github-actions-aws-oidc/) 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](https://www.itwonderlab.com/tutorials/aws/):

- Use a separate [AWS account for demos](https://www.itwonderlab.com/create-an-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](https://aws.amazon.com/free/) conditions for your account.
- Tag all resources ([resource tagging](https://www.itwonderlab.com/aws-resource-tagging/)) and use Cost Explorer to filter by tag.
- Delete forgotten resources: an old [EBS](https://www.itwonderlab.com/aws-ebs/) volume or snapshot still costs money.

Cost estimation is part of the [Terraform best practices](https://www.itwonderlab.com/terraform-best-practices/).
