Skip to main content
All posts
Updated

Terraform Cost Estimates in Every Pull Request

The LevelFour PR bot prices each Terraform and CloudFormation change on GitHub and GitLab, suggests cheaper settings, and commits them when you reply.

Gabriel Nielsen
Ler em portuguêsEstimativa de custo do Terraform em cada pull request

Much of a cloud bill gets decided in code review. Someone picks an instance size out of habit, copies a disk type from another module, or raises a replica count for a load test and never lowers it. By the time the bill shows it, the change has been running for weeks.

The LevelFour PR bot moves that moment into the pull request. It reads every pull request on GitHub and every merge request on GitLab that changes Terraform or CloudFormation, prices the change, and proposes cheaper settings where it finds them.

What the bot posts

  • A cost comment with the cost of the changed resources before and after, and the monthly spend. The bot edits that one comment on each push, so the thread stays readable.
  • A LevelFour status in the checks panel on GitHub and in the pipeline status on GitLab, with the analysis summary. The status fails only when the analysis itself could not run.
  • Inline suggestions on the lines of the diff, each one a concrete change to a resource, ready to commit.

In CloudFormation, the bot reads YAML and JSON templates, SAM templates, and nested stacks whose template lives in the repository. It resolves parameters, conditions, and mappings before it prices, and lists anything it had to assume under Assumptions in the cost comment.

Commit a fix with one comment

Reply to the bot and it acts on the request. apply writes the suggestions into one commit on the branch and replies with its short SHA. On GitLab it also resolves the threads whose suggestions it applied. On GitHub, if the branch moved since the bot's last analysis, it refuses rather than overwrite newer work.

Pull request comment
@levelfourai apply all
@levelfourai apply aws_instance.batch
@levelfourai ignore AWS::EC2::Volume.Logs
@levelfourai snooze waiting for the load test
@levelfourai dismiss sized for the holiday peak

Anyone who can comment can run help. The commands that change something need write access to the repository on GitHub, or the Developer role or above on GitLab. dismiss and snooze take a reason, and the cost comment shows who made the call and why, so the next reviewer sees the decision.

Tune it per repository

A .levelfour/config.yml file at the root of the repository turns the bot off, keeps the cost comment without inline suggestions, skips paths and resource types, or maps a CloudFormation template to the parameters file it deploys with:

.levelfour/config.yml
code_suggestions_enabled: false
excluded_paths:
  - tests/fixtures/**
  - examples/**
excluded_resource_types:
  - aws_iam_role_policy_attachment
  - AWS::Logs::*
cloudformation:
  parameter_files:
    stacks/app.yaml: stacks/params/prod.json

enabled: false turns the bot off, max_code_suggestions caps the inline suggestions at anywhere from 0 to 25, and region_override pins every estimate to one AWS region. The bot drops a field it does not recognize and warns about it at the bottom of its comment, so a typo such as enabld: false leaves the bot on.

The same estimate in CI

The l4 CLI prices the same Terraform and CloudFormation from a terminal or a pipeline. With --fail-above, l4 estimate exits with code 2 once the monthly total passes your threshold, and a pipeline can branch on that code before anything deploys. l4 diff gates on what a change adds instead of the total.

Terminal
l4 estimate --fail-above 500 -q ./infra/

By default, l4 diff measures against the git merge-base with main or master, so the job needs full git history: fetch-depth: 0 on GitHub Actions and GIT_DEPTH: 0 on GitLab CI. The CI/CD guide has both jobs written out.

Get started

Connect GitHub or GitLab from the LevelFour dashboard, and the bot starts on the next pull request that touches Terraform or CloudFormation. On GitHub, an organization Owner installs the LevelFour app. On GitLab, you need the Maintainer or Owner role on each project, on GitLab.com or a self-managed instance. The cost check on pull requests comes with every plan.

The PR bot docs list every command, and the configuration reference has every field.

Start with LevelFour and see the cost of your next pull request before it merges.

Frequently asked questions

Which infrastructure as code does the LevelFour PR bot read?

Terraform and CloudFormation, on GitHub pull requests and GitLab merge requests. For CloudFormation it reads YAML and JSON templates, SAM templates, and nested stacks whose template lives in the repository. What is shift-left FinOps?.

Who can run the bot's commands?

Anyone who can comment can run help. dismiss, snooze, ignore, and apply need write access to the repository on GitHub, or the Developer role or above on GitLab.

Can the bot commit to a pull request from a fork?

apply skips pull requests and merge requests from forks, because the fork's branch belongs to its author.

How do I keep the cost comment and drop the inline suggestions?

Set code_suggestions_enabled to false in .levelfour/config.yml. The bot keeps posting the cost comment and the status.

More posts

See what LevelFour can save you