• Stars
    star
    679
  • Rank 66,532 (Top 2 %)
  • Language jq
  • License
    Apache License 2.0
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Anonymized, secure, and free Terraform cost estimation based on Terraform plan (0.12+) or Terraform state (any version)

Anonymized, secure and free Terraform Cost Estimation

cost.modules.tf is entirely free cost estimation service, which is part of modules.tf that is currently in active development.

Join the mailing list on modules.tf to stay updated!

This is not an official HashiCorp product.

tldr; Post your Terraform state or plan-file (as JSON) and get cost estimation:

$ terraform state pull | curl -s -X POST -H "Content-Type: application/json" -d @- https://cost.modules.tf/

{"hourly": 0.01, "monthly": 9.07}

NB: Cost estimation uses official AWS pricing data and does not include estimates for items not specified in Terraform configurations (e.g., usage patterns, amount of API calls, bandwidth, disk I/O, spot prices, AWS discounts, etc.).

It is sometimes impossible to extract all information required for cost estimations from the Terraform plan provided, and it is more accurate to get estimates from the Terraform state file after the infrastructure is created.

See the list of supported resources.

Secrets and sensitive information

As you probably know, Terraform state and plan files may contain secrets and sensitive information which you don't want to send anywhere to get cost estimates. There is a solution that is supported, secure, and easy to put in your continuous automation process.

All you need to do is to process the Terraform state or plan file with terraform.jq file which is available in this repository.

Make sure that JQ version 1.6 is installed. Many Linux distributions install older version by default, and you need to update it as described in the official documentation.

terraform.jq creates anonymized cost keys sufficient to perform cost estimation.

For example, cost keys for a single EC2 instance and an Application Load Balancer in eu-west-1 region look like this:

{
  "keys": [
    "ec2#eu-west-1#t3.nano#shared#linux",
    "ec2#eu-west-1#alb"
  ]
}

The whole process looks like this:

# Download terraform.jq file
$ curl -sLO https://raw.githubusercontent.com/antonbabenko/terraform-cost-estimation/master/terraform.jq

# Get terraform state (or plan), extract cost keys, send them to cost estimation service
$ terraform state pull | jq -cf terraform.jq | curl -s -X POST -H "Content-Type: application/json" -d @- https://cost.modules.tf/

{"hourly": 0.01, "monthly": 9.07}

Sweet, isn't it?

Things you should know about infrastructure costs:

  • How much does my infrastructure is going to cost before create?
  • How much does my infrastructure cost after it is created (based on Terraform state)?
  • What is the difference in the price comparing to the current infrastructure (based on Terraform plan)?
  • Can I have cost estimation based on Terraform 0.7 state files? Yes, any version of Terraform state files is supported!

Example - Get cost estimates during terraform plan

The flow is like this:

  1. Plan Terraform changes into a plan-file
  2. Convert the plan-file into JSON-file
  3. Extract anonymized cost keys from the JSON-file (optional, but recommended)
  4. Send cost keys to cost.modules.tf
  5. Process response

Step 3 recommended if you don't want to send the whole JSON-file, which may contain sensitive information.

The whole command looks like this:

# Install jq and download `terraform.jq` file as described in "secrets and sensitive information" section

$ terraform plan -out=plan.tfplan > /dev/null && terraform show -json plan.tfplan | jq -cf terraform.jq | curl -s -X POST -H "Content-Type: application/json" -d @- https://cost.modules.tf/

Alternatively, you can send the whole Terraform plan-file without modification as json, too:

$ terraform plan -out=plan.tfplan > /dev/null && terraform show -json plan.tfplan | curl -s -X POST -H "Content-Type: application/json" -d @- https://cost.modules.tf/

Helpers

# Get Terraform plan as json
$ terraform plan -out=plan.tfplan > /dev/null && terraform show -json plan.tfplan > plan.json

# Get Terraform state as json (option 1)
$ terraform state pull > plan.json

# Get Terraform state as json (option 2)
$ terraform show -json > plan.json

# Do something is monthly cost is too high
$ ... | curl -s -X POST -H "Content-Type: application/json" -d @- https://cost.modules.tf/ > costs.json
$ jq 'if .monthly|tonumber > 10 then "$" else "$$$" end' costs.json

Supported resources

  1. EC2 instances (on-demand) and Autoscaling Groups (Launch Configurations and Launch Templates):
  • aws_instance
  • aws_autoscaling_group
  • aws_launch_configuration
  • aws_launch_template
  1. EC2 Fleets (on-demand)
  • aws_ec2_fleet
  1. EBS Volumes, Snapshots, Snapshot Copies
  • aws_ebs_volume
  • aws_ebs_snapshot
  • aws_ebs_snapshot_copy
  1. Elastic Load Balancing (ELB, ALB, NLB)
  • aws_elb
  • aws_alb / aws_lb
  1. NAT Gateways
  • aws_nat_gateway

Please suggest other resources worth covering by upvoting existing issue or opening new issue.

As AWS Community Hero, I work a lot with AWS, but I am equally interested in covering other popular Terraform Providers with decent pricing API.

Like this? Please follow me and share it with your network!

@antonbabenko @antonbabenko

Consider support my work on GitHub Sponsors, Buy me a coffee, or PayPal.

Disclaimer

cost.modules.tf runs by Betajob. We don't save, publish, share with anyone data submitted to the service. No identifiable customer information used to query pricing systems (check source code of terraform.jq).

terraform-cost-estimation project managed by Anton Babenko.

This is not an official HashiCorp product. You may want to look into Terraform Cloud where similar feature exists.

License

This code is released under the Apache 2.0 License. Please see LICENSE for more details.

Copyright © 2020 Anton Babenko (Betajob AS)

More Repositories

1

pre-commit-terraform

pre-commit git hooks to take care of Terraform configurations 🇺🇦
Shell
3,145
star
2

terraform-best-practices

Terraform Best Practices free ebook translated into 🇬🇧🇦🇪🇧🇦🇧🇷🇫🇷🇬🇪🇩🇪🇬🇷🇮🇱🇮🇳🇮🇩🇮🇹🇰🇷🇵🇱🇷🇴🇨🇳🇪🇸🇹🇷🇺🇦🇵🇰
HCL
1,991
star
3

serverless.tf

serverless.tf is an opinionated open-source framework for developing, building, deploying, and securing serverless applications and infrastructures on AWS using Terraform.
726
star
4

terraform-aws-devops

Info about many of my Terraform, AWS, and DevOps projects.
422
star
5

terragrunt-reference-architecture

Terragrunt Reference Architecture (upd: May 2020)
HCL
371
star
6

modules.tf-lambda

Infrastructure as code generator - from visual diagrams created with Cloudcraft.co to Terraform
Python
349
star
7

terraform-best-practices-workshop

Terraform Best Practices - workshop materials
HCL
237
star
8

terraform-docs-as-pdf

Complete Terraform documentation (core + all official providers) as PDF files. Updating nightly.
Shell
201
star
9

awsp

AWS credential profile changer
Shell
91
star
10

you-have-passed-the-certification

I'm glad you have passed the certification! I just don't want to see posts about it in my LinkedIn newsfeed.
JavaScript
77
star
11

terrapin

[not-WIP] Terraform module generator (not ready for its prime time, yet)
Shell
71
star
12

modules.tf-demo

Real modules.tf demo (updated May 2021)
HCL
41
star
13

serverless.tf-playground

serverless.tf playground for examples and experiments
HCL
38
star
14

terraform-deployment-pipeline-talk

Code for my talks about Terraform in deployment pipeline
HCL
33
star
15

terraform-provider-openai

Terraform provider for OpenAI (experimental as hell)
Go
26
star
16

terraform-aws-anything

Content for "Manage any AWS resource with Terraform"
HCL
24
star
17

tfvars-annotations

[not-WIP] Update values in terraform.tfvars using annotations
Go
23
star
18

terrible

[not-WIP] Let's orchestrate Terraform configuration files with Ansible! Terrible!
Makefile
23
star
19

antonbabenko

12
star
20

addo-demo

All code used during my All Day DevOps talk
HCL
7
star
21

terramate-reference-architecture

WIP: Terramate Reference Architecture
HCL
7
star
22

renovatebot-tf-tg-demo

Renovatebot with Terraform and Terragrunt
HCL
5
star
23

.github

Meta repository for all repositories in my account
1
star