What Is Shift-Left Cost Management?
Shift-left means moving cost visibility and controls earlier in the development lifecycle — into the infrastructure-as-code (IaC) review process, before resources are actually provisioned. An engineer writing Terraform should see the monthly cost impact of their changes before they merge, not after the next billing cycle.
Infracost — The Leading Shift-Left Tool
Infracost is an open-source tool that parses Terraform plans and generates a cost estimate breakdown. It supports AWS, Azure, and GCP with 1,000+ resources priced. The free tier covers unlimited CLI usage; the Team tier ($50/month) adds CI/CD integrations, team policies, and Slack notifications.
Example output when a developer adds an aws_db_instance:
CI/CD Integration
The real value of Infracost comes from integrating it into your pull request workflow. Every PR that changes Terraform gets an automatic cost estimate comment:
Cost Policies with OPA
Open Policy Agent (OPA) can evaluate Infracost diff output against a cost policy. The example below defines a policy; the GitHub Actions workflow shown above posts a cost comment but does not run this policy.
This example uses Rego v0 syntax. With OPA 1.x, use a v0-compatible execution mode or deliberately migrate the policy to Rego v1 syntax. Blocking a pull request requires a CI step that evaluates the policy, fails on a denial, and is required before merge. The workflow above does not configure that enforcement.
Terraform Cost Best Practices
- Use variables for instance types — makes right-sizing changes a one-line diff instead of hunting through modules
- Tag everything at the resource level — Terraform makes consistent tagging trivial with
default_tagson AWS providers - Separate dev/staging/prod workspaces — enforce smaller instance types in non-production via workspace-conditional variables
- Use data sources to reference existing resources — avoid provisioning duplicate infrastructure (VPCs, security groups) that runs parallel costs
- Review
terraform planoutput before apply — new resources added to plans should be questioned: is this intentional? Is the size appropriate?
// FAQ
infracost scan. Its supported-resources overview lists Pulumi as planned, rather than supported. Confirm OpenTofu compatibility and CLI options against the documentation for the version you use.