July 4, 2022

Blog @ Munaf Sheikh

Latest news from tech-feeds around the world.

How to Integrate Infracost with Terraform Cloud

Great post from our friends at Source link

Running infrastructure at any scale almost always guarantees a dizzying array of components and configurations. To further complicate things, different teams within an organization may need similar infrastructures with slight variations. Additionally, that infrastructure may be spread over multiple topographies, from on-premise to one or more cloud vendors.

Terraform is Hashicorp’s service offering that can provision infrastructure across multiple clouds and on-premises data centers, in addition to safely and efficiently re-provisioning infrastructure in response to configuration changes. 

This article explores why you need to know about Terraform Cloud if your organization plans to deploy a hybrid cloud or multi-cloud environment. We also outline how you can integrate Terraform Cloud with Infracost. 

Table of Contents

  • What is Terraform Cloud?
  • Why Terraform Cloud?
  • Infracost and Terraform
  • How to Integrate Infracost with Terraform
  • Conclusion

What Is Terraform Cloud?

Terraform is an open-source “Infrastructure as Code”(IaC) tool created by HashiCorp.

Terraform enables developers to use a high-level configuration language called HCL (HashiCorp Configuration Language) to describe the desired “end-state” cloud or on-premises infrastructure for running an application.

Essentially, Terraform Cloud is a great and easy way to apply your Terraform Code. In terms of pricing, different tier plans exist, but the free tier can still satisfy most of your needs. One of the reasons why Terraform cloud is popular right now is because there’s no need to find a remote host to store your state file.  Instead, Terraform Cloud will directly manage, host, and encrypt it at rest with versioning enabled by default.

At any point, you can access older states, view and restore them. The entire security applied around this is done by HashiCorp, reducing any administration overhead you might encounter if you were managing the state file yourself. A plan or application can be triggered simply with a Git commit to your repository, where your IaC resides. It supports most of the Version Control repository management, including the self-hosted ones.

Learn more about how this works here

Why Terraform Cloud? 

Terraform Cloud eliminates the need for unnecessary tooling and documentation to use Terraform in production. To better understand Terraform’s benefits, it is vital to first learn about the advantage of Infrastructure as Code (IaC). Primarily, IaC enables developers to codify infrastructure in a way that allows provisioning to be not only automated but also faster and repeatable. Note that IaC is a key part of Agile and DevOps practices such as; 

  • Version control
  • Continuous integration
  • Continuous deployment

With this in mind, the reasons why developers are drifting towards Terraform include its: 

  • Immutable infrastructure
  • Support for multiple platforms
  • Open-source nature
  • Scalability

Infracost and Terraform Cloud

Infracost is an open-source project released in June 2020. Infracost aims to help companies estimate the cost of every IaC change made to their cloud infrastructure. 

With that in mind, previously, to integrate Infracost into your CI/CD pipeline, you needed to rely on your Version Control repository management e.g 

This was made possible based on the fact that Infracost already has integrations with these tools. All you need to do is make a pull request, after which Infracost will compare the previous state with the planned state and then comment on the difference in your pull request.

You may be wondering, what if you are using Terraform Cloud? Is there an easier way to integrate Infracost into it? Well, Run Tasks is a new feature (still in beta at the time of writing) for Terraform Cloud and is likely to become available to the public by the end of the year.

This feature allows you to integrate third-party tools such as BridgeCrew, Snyk, Refactr, CloudTamer, and Infracost between the plan and apply stage. If you’re hoping to develop your own integration, HashiCorp, Terraform’s parent company,  built some documentation on how to achieve this. You can also find more information about Run Task here.

How To Integrate Infracost to Terraform Cloud

Using the run task feature explained above, integrating Infracost with Terraform Cloud is pretty straightforward.

All you have to do is:

  1. Grab your unique Infracost hook endpoint URL and HMAC Key by registering at the following form
  2. Navigate to “Task event hooks” in your organization setting and fill up the blank fields with the information generated in step 1; if you aren’t sure where to find this setting, see the screenshot below or this link.
  3. The final step is to enable the task on your workspace, as shown in the screenshot below. 
  4. Voilà! Now, whenever Terraform Cloud runs a plan, you will see a new stage called for this run task with Infracost similar to the one we enabled for our test:

If you click on details, this is where we see something new; a unique URL generated for you by Infracost. This is a fantastic feature as it can be shared with the rest of your team and offers a rundown on the cost difference.



Conclusion

Terraform’s “Run Tasks,” which is in the beta phase, enables your organization to integrate third-party tools such as Infracost within a Terraform run. Specifically, its utility comes in handy between the plan and apply stages of the Terraform Cloud workflow. Essentially, anything from security checks to cost estimation to code scanning can be integrated into your Terraform workflow with the Run Tasks feature. We hope this feature will become quickly available for everyone as it can unlock lots of exciting possibilities.

#Integrate #Infracost #Terraform #Cloud