How does Terraform work?

Terraform is an Infrastructure as Code (IaC) tool which allows the building, changing and versioning of infrastructure in an efficient way. Terraform allows management of an infrastructure whether that’s cloud service or in-house. The purpose of Terraform is to answer three questions, “How do we get the infrastructure up and running on day 1?”, “How do we manage the Infrastructure from day 2 onwards?” and finally “How do we destroy the Infrastructure at the end of its lifecycle?”.

  1. How it works

Terraform template allows us to define how the infrastructure is going to be spun up. This involves the details of the resource you are going to spin up, any necessary and optional details required to complete the deployment of the resource etc. More details on the various steps within a Terraform deployment is explained below:

Terraform Refresh

The first step is Terraform Refresh, which looks at the “real world” setup, as is currently and comparing it against the templates that are in place. The refresh stage looks at what is different compared to the real world and what needs to be updated/deployed.

Terraform Plan

The second step after carrying out a reconciliation task is that it needs to create a plan of what requires updating and what should remain the same. So as an example, given by HashiCorp, creators of Terraform, the first day of deployment, the plan stage will not have anything to compare the templates to, so it deploys all that have been defined in the templates. From the second day onwards, there will be an environment to compare against and make the necessary changes.

Terraform Apply

The third step is to actually execute the changes that have been identified. As mentioned in the example above, on the first day, because there is nothing to compare to, Terraform will deploy all infrastructure that has been defined. This step also works out the dependencies to check what needs to be deployed before others. For example, the network needs to be deployed before it can deploy the security groups etc. Terraform also has the intelligence to figure out where components can be deployed in parallel for maximum efficiency.

Terraform destroy

To prepare for decommissioning the components within the infrastructure, Terraform also creates a plan which can be used to destroy the resources as required. It records the history and manages it for it to be recollected later.

This is an overview of how Terraform works and the steps that it takes to deploy the infrastructure to the desired view. This is an efficient process of managing and maintaining infrastructure with the whole lifecycle taken into consideration such as the day 1 view, as well as day 2 + and also what can be done at the end of its lifecycle.

If you enjoyed this blog, please follow us on LinkedIn to ensure you don’t miss any similar blogs.

Leave a Reply

Your email address will not be published. Required fields are marked *