As Infrastructure as Code (IaC) practices mature, teams often adopt tools that build upon Terraform's foundation to manage complexity and maintain open-source principles. Terragrunt helps keep configurations DRY (Don't Repeat Yourself) across multiple environments, while OpenTofu has emerged as a community-driven, open-source fork of Terraform. While these tools solve important workflow and licensing challenges, they also require a clear strategy for cost estimation. This article explores the tools and techniques available for implementing pre-deployment cost estimation for both Terragrunt and OpenTofu, ensuring that financial visibility keeps pace with infrastructure sophistication.
Cost Estimation for Terragrunt
Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping your configurations DRY, managing remote state, and handling dependencies between modules. Its ability to create reusable modules and DRY configurations helps teams standardize resource provisioning with cost-effective settings. However, a Terragrunt repository often consists of many small, interconnected modules, which can complicate cost analysis.
Infracost: Native Terragrunt Detection
Infracost is the leading tool for Terragrunt cost estimation, offering native support that simplifies the process significantly.
Automatic Detection: The
infracost breakdownandinfracost diffcommands automatically detect when they are run against a Terragrunt project. Infracost identifies theterragrunt.hclfile, downloads any required source modules into a cache, detects Terragrunt-defined inputs, and then parses the HCL code directly to calculate costs.Simple Execution: For a basic Terragrunt repository, running a cost estimation is as simple as pointing the CLI to the project path:
Shell
infracost breakdown --path path/to/terragrunt/repoInfracost will then output a cost breakdown for each Terragrunt module it finds.
Handling Complex Terragrunt Repositories
For more complex Terragrunt setups, such as monorepos with different usage patterns for each environment, using an Infracost configuration file (infracost.yml) is the recommended best practice.
Multiple Usage Files: If your Terragrunt project has multiple modules (e.g., for dev and prod environments) and you need to specify different usage files for each to get accurate estimates on resources like S3 or Lambda, you must define each Terragrunt subdirectory as a separate project in the
infracost.ymlfile.Excluding Directories: The config file or the
--exclude-pathCLI flag can be used to ignore certain directories (e.g., test environments) that you don't want to include in the cost estimate.
By integrating with cost estimation tools like Infracost, Terragrunt's structured approach to IaC becomes even more powerful, allowing teams to maintain cost-effective cloud architectures and improve collaboration between finance and engineering.
Cost Estimation for OpenTofu
OpenTofu is an open-source, community-driven fork of Terraform created in response to HashiCorp's license change. It is designed to be a drop-in replacement for Terraform, meaning it is compatible with existing Terraform providers, modules, and state files. This compatibility is key to its cost estimation strategy.
Because OpenTofu maintains compatibility with the Terraform ecosystem, most tools that work with Terraform can also work with OpenTofu with minimal or no changes.
Infracost and OpenTofu
Infracost, being a tool that primarily parses HCL code and interacts with the Terraform CLI, works seamlessly with OpenTofu. Teams can continue to use Infracost in their CI/CD pipelines for OpenTofu projects just as they would for Terraform projects. The workflow for generating cost estimates in a pull request remains the same: generate a baseline from the target branch, generate a diff from the feature branch, and post a comment.
Scalr: Native OpenTofu and Infracost Integration
Scalr, a Terraform Cloud alternative, is a strong supporter of the OpenTofu initiative and offers native support for both OpenTofu and Infracost.
How it Works: Scalr integrates directly with the Infracost cloud pricing server. When a user runs an OpenTofu plan within the Scalr platform, Scalr analyzes the plan and uses the Infracost integration to generate a cost estimate. This estimate includes the prior monthly cost, proposed monthly cost, and the delta.
Policy Enforcement: This cost data is then made available to Scalr's native Open Policy Agent (OPA) integration. This allows teams to write policies in Rego to enforce cost guardrails, such as automatically blocking an OpenTofu deployment if the
delta_monthly_costexceeds a predefined budget.
Other Tools
Other IaC management platforms that have embraced OpenTofu, such as Spacelift and env0, also offer cost estimation capabilities, often by integrating Infracost under the hood. Similarly, open-source tools like OpenInfraQuote that work with Terraform plan JSON files will also work with OpenTofu, as tofu plan and tofu show -json produce the same output format as their Terraform counterparts.
Strategies for Success
Whether using Terragrunt, OpenTofu, or both, a successful cost estimation strategy relies on a few key principles:
Integrate Early and Automate: Embed cost estimation directly into your CI/CD pipeline. For both tools, this means running cost checks on every merge or pull request to provide immediate feedback to developers.
Use Configuration for Complexity: For non-trivial repositories, especially with Terragrunt, leverage configuration files (
infracost.yml) to accurately map projects, environments, and usage data. This ensures your estimates are as realistic as possible.Combine Visibility with Governance: Don't just show the cost—act on it. Use policy as code frameworks like OPA or Sentinel to create automated guardrails that enforce budgets and prevent costly mistakes from reaching production. Platforms like Scalr make this straightforward for OpenTofu users.
Conclusion
The principles of proactive cost management are universal, regardless of the specific IaC tools you use. For Terragrunt users, Infracost provides a powerful and natively integrated solution that can handle the complexity of multi-module repositories. For the growing community of OpenTofu users, the compatibility with the existing Terraform ecosystem means that powerful cost estimation tools like Infracost, Scalr, and OpenInfraQuote are readily available. By leveraging these tools, teams can ensure that even as their infrastructure code becomes more sophisticated and their commitment to open-source deepens, their control over cloud costs remains robust and predictable.
All in One Place
Atler Pilot decodes your cloud spend story by bringing monitoring, automation, and intelligent insights together for faster and better cloud operations.

