Managing, optimizing and estimate cloud costs has become critical to running efficient and cost-effective operations in today’s cloud-centric world. As organizations increasingly rely on Infrastructure as Code (IaC) tools like Terraform to manage their cloud infrastructure, the need for integrated cost estimation and financial operations (FinOps) practices is more important than ever. This article explores leveraging Infracost for accurate cloud cost estimates and adopting FinOps best practices to streamline your Terraform-managed infrastructure.
The Importance of Cloud Cost Estimates
Understanding the cost implications of your infrastructure decisions is fundamental to effective cloud management. Accurate cloud cost estimates enable teams to:
- Budget Accurately: Ensure that cloud expenses align with financial forecasts and budgets.
- Optimize Resources: Identify cost-saving opportunities by analyzing spending patterns and optimizing resource utilization.
- Promote Cost-Aware Culture: Embed a cost-conscious culture within development teams, ensuring cost considerations are part of the decision-making process.
How to use Infracost with Terraform?
Terraform is a widely used IaC tool that allows developers to define and provision cloud resources using declarative configuration files. By integrating Infracost, a tool designed for real-time cost estimation, into your Terraform workflows, you can gain valuable insights into the cost implications of your infrastructure changes before deployment.
Prerequisites
- A machine (for this guide, I am using Amazon Linux )
- Terraform CLI (How to Install Terraform on Linux)
- An AWS account
STEP 1 – Install Infracost
Get the latest Infracost release. The simplest method is to use the installation script.
# Downloads the CLI based on your OS/arch and puts it in /usr/local/bin
curl -fsSL https://raw.githubusercontent.com/infracost/infracost/master/scripts/install.sh | sh
infracost --version # Should show 0.10.37
STEP 2 – Get the API key
Please register for a free API key, which the CLI uses to fetch prices from our Cloud Pricing API, such as retrieving prices for different instance types.
infracost auth login
You will be redirected to the Infracost login page, where you must complete the login process. Once done, the key will be stored on your server. You can retrieve the key by running the following command:
infracost configure get api_key
# output should be ico-X8i4bg0xD6SBV9GT1QqtseF51S4V8Z0P
STEP 3 – Create a Terraform Configuration
Created a simple Terraform configuration file, which you can download from git https://github.com/cloudcuddlers/infracost-terraform
STEP 3 – Initialize Terraform and generate a plan
Run the command init and plan to generate a terraform plan for your infrastructure as code.
terraform init
terraform plan -out=tfplan.binary
STEP 4 – Generate Infracost Cost Estimates
Run Infracost to generate a cost estimate for your Terraform configuration:
infracost breakdown --path=tfplan.binary --project-name=cloudcuddler
Now if you want to compare the cost with the previous version and the current version then you need to save the output in JSON format with the mentioned command
infocost_terraform]# infracost breakdown --path=tfplan.binary --project-name=cloudcuddler --out-file previous_version.json --format json
Now you have added one more EC2 instance in your Terraform configuration file and again run the command
infracost diff --path=tfplan.binary --compare-to previous_version.json --project-name=cloudcuddler
As you can see, by adding another EC2 instance, Infracost captures this change from Terraform, reflecting the resulting increase in monthly costs.
Conclusion
By integrating Infracost into your Terraform workflows, you can gain valuable insights into the cost implications of your infrastructure changes before deployment. This ensures that cost efficiency is considered at every development lifecycle stage. Implementing FinOps best practices with Infracost helps promote a culture of cost awareness and drives financial and operational excellence in your cloud journey.