Optimizing Cloud Budgets: Automated Cloud FinOps Cost Control in CI/CD with Infracost
We will build an automated cost control system in CI/CD using Infracost to forecast and optimize cloud budgets. This guide demonstrates how to integrate Infracost into CI/CD...
We will build an automated cost control system in CI/CD using Infracost to forecast and optimize cloud budgets. This guide demonstrates how to integrate Infracost into CI/CD pipelines, using open-source tools to analyze costs of AWS, Azure, GCP resources, and report costs before deployment. The goal is to reduce unnecessary expenses, increase transparency, and support effective FinOps decision-making.
Overview of FinOps and Infracost
FinOps (Financial Operations) is a data-driven approach to managing cloud costs, helping organizations control spending, optimize budgets, and improve financial performance. Infracost is an open-source tool for forecasting cloud costs, supporting platforms like AWS, Azure, Google Cloud, and Kubernetes. It integrates easily with CI/CD systems to check costs before deploying changes.
The main features of Infracost include:
- Forecasting costs for changes in configuration code (Terraform, Helm, CloudFormation).
- Integration with GitHub Actions, GitLab CI, and other CI/CD systems.
- Displaying cost reports as tables, charts, or comments in pull requests.
- Setting cost budget alerts to notify when exceeding budgets.
Infracost works by analyzing configuration code and comparing it to current market prices to calculate estimated costs. The tool does not require a real cloud account to operate, only configuration code and information about the region and resource type.
Setting Up Infracost in CI/CD
To integrate Infracost into CI/CD, you need to install Infracost on the CI server and configure the pipeline to run cost analysis after each code change.
Step 1: Install Infracost
On the CI server (e.g., GitHub Actions), add the Infracost installation command to the pipeline:
curl -sSL https://infracost.io/install.sh | sh
Or use Docker:
docker run -it --rm -v $PWD:/infracost infracost/infracost:latest
Step 2: Configure CI/CD Pipeline
Here is an example with GitHub Actions:
name: Infracost Cost Analysis
on:
pull_request:
branches: [ main ]
jobs:
infracost:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Infracost
run: |
curl -sSL https://infracost.io/install.sh | sh
- name: Run Infracost
run: |
infracost breakdown --path=./terraform --format=json --out-file=infracost.json
- name: Upload Report
uses: actions/upload-artifact@v4
with:
name: infracost-report
path: infracost.json
Step 3: Display Report in Pull Request
To display the cost report in a pull request, you can use GitHub Actions to create a comment containing cost information:
name: Infracost PR Comment
on:
pull_request:
branches: [ main ]
jobs:
infracost:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Infracost
run: |
curl -sSL https://infracost.io/install.sh | sh
- name: Run Infracost
run: |
infracost breakdown --path=./terraform --format=github-pr-comment --out-file=infracost-pr-comment.md
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: fs.readFileSync('infracost-pr-comment.md', 'utf8')
})
Cost Analysis and Optimization
After running Infracost, you will receive a cost report in JSON or Markdown format. The report includes:
- Estimated cost for each resource.
- Cost by region and resource type (e.g., EC2 instance, S3 bucket).
- Comparison of costs between the current version and the changed version.
Example: Infracost Cost Report
{
"resources": [
{
"name": "web-server",
"type": "aws_instance",
"region": "us-east-1",
"cost": 120.5,
"currency": "USD",
"tags": {
"environment": "production"
}
},
{
"name": "database",
"type": "aws_rds",
"region": "us-east-1",
"cost": 80.2,
"currency": "USD",
"tags": {
"environment": "production"
}
}
],
"totalCost": 200.7,
"currency": "USD"
}
To optimize costs, you can:
- Reduce instance size (e.g., from t3.xlarge to t3.medium).
- Switch to cost-saving instances (Spot Instances or Reserved Instances).
- Remove unnecessary resources (e.g., unused S3 buckets).
- Change region to use lower-cost regions.
Testing and Verification
To verify that Infracost works correctly, you can:
- Run Infracost on a staging environment before deploying to production.
- Compare the cost report with actual costs from the cloud console (AWS Billing Console, Azure Cost Management).
- Set up cost budget alerts to receive notifications when exceeding budgets.
Verification Commands
# Check if Infracost is working
infracost --version
# Analyze costs for Terraform configuration
infracost breakdown --path=./terraform --format=json --out-file=infracost.json
# Display cost report in terminal
infracost breakdown --path=./terraform --format=table
Troubleshooting
If Infracost does not work as expected, check the following issues:
- Resource not found: Ensure that the Terraform configuration is correct and has no syntax errors.
- Inaccurate costs: Verify that the region and resource type are correct. Infracost uses current market prices, so costs may change over time.
- Connection errors: Ensure that the CI server has internet access and is not blocked by a firewall.
- No report in pull request: Check that GitHub Actions is configured correctly and has permission to create comments on pull requests.
References
- Infracost Official Website
- Infracost Documentation
- Infracost GitHub Repository
- Professional Web-Based VPN Server Setup with Firezone and WireGuard UI on Ubuntu
- New Generation Distributed Storage Architecture Optimizing Costs with JuiceFS and Ceph OSD on 3 Cheap VPS
Related infrastructure & engineering guides: NVMe Cloud VPS for Docker, Nginx & Databases
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
