Nerd For Tech
Published in

Nerd For Tech

Analyze Terraform costs with Infracost ( The GitOps Way )

Analyzing the terraforming cost with Infracost

Terraform is one of the major IAC tools used across various Organizations. Well in my previous article I have explained how Atlantis and GitHub actions can be used to provision Infrastructure the GitOps way. But what about the cost? The GitOps method obviously made the deployments easy. Developers / Ops Engineers will start raising the PR’s easily and keep merging them. But who keeps track of the cost? We cannot assign an engineer to keep a track of the cost all the time. Assume there are 100 pull requests, an Engineer cannot really calculate the cost for all the PR’s from the AWS Calculator. What if there's a tool that would comment on the increment/decrement with the change in the infrastructure. Ahh, that's cool Isn't it? Here comes Infracost into the picture. Infracost shows cloud cost estimates for Terraform. It lets DevOps, SRE, and engineers see a cost breakdown and understand costs before making changes, either in the terminal or pull requests. This provides your team with a safety net as people can discuss costs as part of the workflow. Using one of the many integrations DevOps, SRE, and engineers see a cost estimate in pull requests before making changes. This provides your team with a safety net as people can understand cloud costs upfront, and discuss them as part of your workflow.

InfraCost

What is the entire story all about? (TLDR)

  1. Identifying and analyzing the cost using the GitOps way.
  2. Integrating Infracost with GitHub actions.

Prerequisites

  1. A GitHub account.
  2. An AWS Account.

Story Resources

  1. GitHub Link: https://github.com/pavan-kumar-99/medium-manifests
  2. GitHub Branch: infracost

Setting up InfraCost

Register for a free API key, which is used by the CLI to retrieve prices from our Cloud Pricing API, e.g. get prices, for instance types.No cloud credentials or secrets are sent to the API and you can also self-host it.

$ brew install infracost$ infracost --version$ infracost register

Setting up the GitHub Action

To learn more about GitHub Actions and how you can Integrate terraform with GitHub actions is well explained in one of my previous articles here. So let us look at the actual Github workflow file used as a part of this article.

So, as a part of this example, I would be creating an EC2 Instance in AWS. So if you see the workflow file, you would notice that we are adding a couple of secrets for AWS and also the InfraCost API Key that we generated earlier. This workflow is designed in such a way that, once there is a pull request raised to the branch infracost ( line 4),

  1. Check out the repository.
  2. Install the terraform binary.
  3. Initilize the terraform directory.
  4. Terraform Plan.
  5. Terraform show, to save the plan output.
  6. Initilize the Infracost API.
  7. Generate Infracost JSON.
  8. Analyze the cost and comment on the Pull Request.

Well done. We are all set to analyze the cost the GitOps way. But before that let us create the necessary GitHub secrets.

GitHub secrets

As a part of this article, we would be needing the following secrets.

  1. AWS_ACCESS_KEY_ID
  2. AWS_SECRET_ACCESS_KEY
  3. INFRACOST_API_KEY

All Done. Let us now see the Infracost in Action. I have created a Terraform script to create an EC2 Instance.

EC2 Instance

Let us now create a pull request by changing the instance type from t2.medium to t3.medium. Alright !! Excited? Me too !!!!

Let us now check the comments on the Pull request that I have raised.

Comment about the cost

So if you observe the comment precisely, you should see a table showing the Previous Cost, New Cost, and the Difference ( Increment or Decrement ). And a precise output on the resource configuration and its spec and its pricing.

Me watching where all of my money is going

Cleanup

$ Delete the worflow files from your Github

Ahh, these are all the steps needed to analyze the cost the GitOps way. I hope you have liked this article. Feel free to share your thoughts and your experiences working with Cost Analysing and reduction activities. In case you face any issues during the deployment, please raise an issue here or feel free to reach me on my e-mail ( pavan1999.kumar@gmail.com ). Also, feel free to get in touch for any queries/consultation on Kubernetes here.

Here are some of my other articles that may interest you

Until next time…..

Recommended

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavan Kumar

Pavan Kumar

586 Followers

Cloud DevOps Engineer at Informatica || CKA | CKS | CSA | CRO | AWS | ISTIO | AZURE | GCP | DEVOPS Linkedin:https://www.linkedin.com/in/pavankumar1999/