DevOps.com estimates billions of dollars wasted in cloud computing workloads. This is primarily due to resources sitting idle for most of the time and lack of proper cost estimation during and automated provisioning of infrastructure.
With billions of dollars at stake, every simple optimisation/improvement we make to our delivery pipelines can make a huge difference.
So today we’re going to take a quick look at estimating cost of applying a terraform plan using Infracost; an open source cost estimation tool.
As with any other guidelines, we’re assuming you have some tools preinstalled. Follow these amazing third party guides to install them:
Get your AWS credentials ready
Our focus will be for AWS resources, however you can reconfigure this guide for any other mainstream cloud providers.
# bash shell: example AWS credentials bash (dummy values below)
export AWS_ACCESS_KEY_ID="ASIATIYSATIYSRMJ69"
export AWS_SECRET_ACCESS_KEY="ldIcH0wkaRuRkFubXycH0wkaRuRkFuab"
export AWS_SESSION_TOKEN="cH0wkcH0wkaRuRkFuabUtMaRuRkFuabUcH0wkaRuRkFuabUtMtMcH0wkaRuRkFuabUtM"
Configure Infracost
Infrascost has two components, a client and an Infracost api that hosts pricing data. It is possible to self-host the Infracost API yourself but in this guide we’ll use their hosted API for simplicity.
Registering for API key
We will use their cli docker image to register and use their API.
# bash shell: infracost cli docker image with ~/.config mounted so it can automatically store api creds for you
docker run -it -v ~/.config:/root/.config --entrypoint=bash infracost/infracost:ci-0.9
# bash shell for infracost cli docker image
infracost register
You’ll then go through the following steps inside the cli image:
Please enter your name and email address to get an API key.
See our FAQ (https://www.infracost.io/docs/faq) for more details.
Name: "Your Name"
? Would you like to see our CI/CD integration docs? [y/N] N
Thanks "Your Name"! Your API key is: XXXXXXYYYYYYXXXXYYYXYXYXXYXYXYDDYYDD
This was saved to /root/.config/infracost/credentials.yml
Follow https://infracost.io/docs to use Infracost.
Simple Terraform Code for EC2 in AWS
Here’s a quick example for a simple EC2 instance in AWS. We’ll be using this code for our guide.
If you’ve already got your Terraform code, you can still use that. Just cd to your project folder. This example is copied from terraform-aws-ec2-module.
# main.tf
module "ec2_instance" {
source = "terraform-aws-modules/ec2-instance/aws"
version = "~> 3.0"
name = "single-instance"
ami = "ami-ebd02392"
instance_type = "t2.micro"
key_name = "user1"
monitoring = true
vpc_security_group_ids = ["sg-12345678"]
subnet_id = "subnet-eddcdzz4"
tags = {
Terraform = "true"
Environment = "dev"
}
}
provider "aws" {
region = "ap-southeast-2"
}
Running terraform plan and exporting the outputs to a json file
# bash shell
# cd to your project directory beforehand
terraform init
terraform plan -out tfplan.binary
terraform show -json tfplan.binary > plan.json
Estimating terraform infrastructure cost with Infracost
Finally, run the Infracost docker image from the same directory.
# bash shell
docker run --rm \
-e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY \
-e AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN \
-e AWS_REGION=$AWS_REGION \
-v $PWD/:/code/ \
-v ~/.config:/root/.config \
infracost/infracost:ci-0.9 breakdown --path /code/plan.json
Running the above command will yield the following outputs to your terminal
Project: /code/plan.json
Name Monthly Qty Unit Monthly Cost
module.ec2_instance.aws_instance.this[0]
├─ Instance usage (Linux/UNIX, on-demand, t2.micro) 730 hours $10.66
├─ EC2 detailed monitoring 7 metrics $2.10
└─ root_block_device
└─ Storage (general purpose SSD, gp2) 8 GB $0.96
OVERALL TOTAL $13.72
──────────────────────────────────
1 cloud resource was detected:
∙ 1 was estimated, it includes usage-based costs, see https://infracost.io/usage-file
This was a quick power-walk through the infrastructure cost estimation using Infracost. However, the story doesn’t end here. Infracost can do lot more. Checkout its full integrations and features at https://www.infracost.io/docs/integrations/github_actions/
Adopting Cloud? Let's Chat!
We are passionate cloud consultants and bring years of experience doing it the right way.
Book a quick 30 min chat with us and let us help you get the most of your cloud infrastructure requirements.