GithubHelp home page GithubHelp logo

standardgalactic / terraform-cost-estimation Goto Github PK

View Code? Open in Web Editor NEW

This project forked from antonbabenko/terraform-cost-estimation

0.0 0.0 0.0 432 KB

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

Home Page: https://terraform-cost-estimation.com/

License: Apache License 2.0

JSONiq 100.00%

terraform-cost-estimation's Introduction

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)

terraform-cost-estimation's People

Contributors

ahlinc avatar antonbabenko avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.