Friday, August 19, 2022

Terraform live cost estimate using infracost free plugin

This image has an empty alt attribute; its file name is image-1.png


Intro

The cloud offers unlimited scalability at lower costs with the available resources. Companies enable their engineering teams to launch infrastructure as and when needed. However, since costs aren't shown to engineers before Cloud resources are deployed, the real cost implications are unknown. That causes higher than expected costs and ballooning budgets over time, which is a nightmare for CFOs considering Gartner estimated Cloud wasted spend exceeding $26.6 billion in 2021. Orgs Blaming dev teams or business units after the damage is done is pointless. What then, should they nuke everything after $$$ overspend? which one to kill first?



The usual suspects? Good ol’ Overprovisioning (~8.7billions) and idle resources ($14.5 billion).

Thus, the greatest challenge for every organization remains:
 How to proactively Reduce Cloud Wasted Spend?”

 
Today, we’ll explore an awesome solution that helps DevOps, SREs,& Devs continuously reduce their cloud costs.

                                                           This tool is called Infracost!
Table of contents



WHAT IS INFRACOST?


Infracost is a super cool tool that live calculates the cost of your Terraform resources on AWS, GCP or Azure before you even hit deploy. All it needs is your terraform files and its cli installed.

Infracost CLI fetches the prices from its Cloud Pricing API, that stores/updates the prices from AWS/Azure/GCP pricing APIs. Their GraphQL-based API gleans up to 3 million prices to return the cost .

  • VS Code extension: by far their slickest feature, where cost is displayed inside your tf code (See below)

This image has an empty alt attribute; its file name is webview.gif

  • Modules: Infracost can even scan modules within your project and pull all the pricing details in seconds.


How does Infracost work?

  • CLI Extracts cost-related parameters from parsing terraform files such as the instance type or disk size.

  • Retrieves prices from the Cloud Pricing API which returns the prices.

  • Calculates the monthly costs and outputs them in table, JSON or other formats.

No need to scroll long pricing pages, just pick a resource value in your *.tf file,& infracost will get its cost for you.    

Syntax (basic): the format and out-file arguments are optional

 infracost breakdown --path myterraDir --format json --out-file infracost-base.json 

  • Terraform variables can be set using --terraform-var-file or --terraform-var
     

I.Installation 


II. Setup and authentication

  • Register for a free API key,used by the CLI to authenticate/retrieve prices from our Cloud Pricing API

  • C:\> infracost auth login
    We're redirecting you to our log in page, please complete that,and return here to continue using Infracost.
    Waiting...
  • In the login Page you have two options sign up via email or githubThis image has an empty alt attribute; its file name is image-4.png

  • After this step your API key will be stored in credentials.yml file and infracost will be all set

    # The API key was saved to C:\Users\brokedba\.config\infracost/credentials.yml
    Your account has been authenticated.
  • You are now ready to use infracost by navigating to any terraform project and running the cli command

    # check Authentication
    C:\> infracost configure get api_key
    ico-7XXXXXXXXXXXXXXXXXXXXX

    C:\>notepad C:\Users\brokedba\.config\infracost/credentials.yml
    version: "0.1"
    api_key: ico-7XXXXXXXXXXXXXXXXXXXXX
    pricing_api_endpoint: https://pricing.api.infracost.io



      III. Use infracost

      1. Azure stack 

              Run Infracost

      • I will use one more option to include services that don’t incur costs using  --show-skipped

         launch-instance/$ infracost breakdown –-path . --show-skipped

      cost estimate of terraform config live before deploy.

      Seems that our cheap static website will cost me USD$12/month 

      • Of the 9 resources, 2 have a fixed pricing (vm compute & public ip) and one(os_disk) based on usage

      • 7 are free which I wanted to display using  --show-skipped  option

      • Will talk later about usage based estimates
         

      2. AWS

      Image

      USD$10/month for a t2.micro compute. Notice there is no fee for the public IP because it’s FREE is AWS.


      3. GCP

      Image

      USD$6.51/m for the same simple web instance stack . Again, no fees for the public IP nor usage based storage. Although GCP usually charges 0.004/hour for Static IPs.


      Change Currency

      What if I want to show the cost in CAD$ or Euro? Well, all you need is to run the below configure command.

      # List your preferred ISO 4217 currency and run the configure command
      $ Infracost configure set currency EUR

      Check if your stack estimate currency has changed (you might need to restart Visual Code to see GUI change)

      This image has an empty alt attribute; its file name is image-5.png


      III. Features

      1. CI/CD integration:

        • Add Infracost to your CI/CD pipeline, to see cost estimates in pull requests before making changes. It supports GitHub actions, GitLab CI, Circle CI, bitbucket, Jenkins & more

      2. Infracost usage based estimate

        • Infracost distinguishes the price of a resource from its cost

          • Price is a per-unit value published by cloud vendors (fixed)

          • Cost is equal to (resource's price) x (its usage), see below AWS Lambda example

            Name                             Quantity  Unit                 Monthly Cost
            aws_lambda_function.hi
            ├─ Requests              Cost depends on usage: $0.20 per 1M requests └─ Duration              Cost depends on usage: $0.0000166667 per GB-seconds PROJECT TOTAL                                                          $0.00

        • There are two options for showing costs instead of prices:

          • Fetch usage from CloudWatch/cloud APIs: to narrow usage-based costs (AWS only).

          • Specify usage manually:

            1. Use --sync-usage-file option to generate a new usage file

            2. Edit the file with your usage estimates & run below command (more details here)

              $ infracost breakdown --path . --usage-file infracost-usage.yml

      3. Infracost Cloud

        • SaaS version which allows team lead visibility across all changes(i.e most expensive pull requests)

        • I had a free trial upon sign up & all my cost estimates were actually sent to my cloud accountInfracost Cloud dashboard showing pull request cost changes over the last 30 days



      Security and Privacy

      • Rest assured Infracost doesn't touch secrets and only parse what pricing API needs to return the cost.

      Conclusion

      • There you have it, I promised I’ll blog about this amazing solution & it’s been very easy and fun to explore

      • This was longer than expected lol, but I hope it was enough information for you to dive into infracost

      • Bare I mind that I couldn’t list all the features which would require web scraping their whole website ;)

      • infracost diff shows diff of monthly costs between current and planned state (using usage.yml file)

      • Among Future features we might have

        • Supporting Oracle Cloud : please upvote the issue-911 in GitHub 

        • Point to statefile and get the cost in a machine that doesn't even have terraform installed 

        • Hiding and Sorting resources by cost

        • Integrate infracost with remote backend based tfsates (S3)

         

        Thank you for reading


        No comments:

        Post a Comment