Have you ever found yourself wondering how you could manage your cloud infrastructure more efficiently? Terraform is a powerful open-source tool from Hashicorp that allows developers to define Infrastructure-as-Code (IaC). Many teams in OTTO IT use it to effectively orchestrate, automate, and manage their cloud infrastructure. This two-part article provides an overview of the main phases of the Terraform workflow and practical tips for optimal use. In this first part, we'll discuss the main phases of the Terraform workflow, the Terraform lifecycle, and Terraform modules.
Terraform configuration files are declarative, meaning they describe the desired end state of your infrastructure. Instead of writing step-by-step instructions to create resources, we simply define what we need. Terraform then takes care of the underlying logic.
When changes are necessary, Terraform can quickly update the existing infrastructure without manual effort. This makes infrastructure management more efficient, consistent, and scalable while minimizing errors.
Let’s take a closer look at how Terraform and its workflow phases operate at the operational level. These steps are crucial for consistent management of IaC:
Your infrastructure is described in Terraform configuration files, which are in HCL format and have a .tf extension. These files contain resources, data, variables, and outputs. The first step is for Terraform to analyse these configuration files and create a graph of infrastructure dependencies. This graph shows how different resources are interconnected and what dependencies exist between them. The efficiency of Terraform lies in its ability to create or modify non-dependent resources in parallel.
Once the analysis is complete, Terraform generates an execution plan. This plan outlines the changes required to the infrastructure to create, modify or remove resources in line with the configuration. It also highlights the added, changed, or removed resources as well as any dependencies or conflicts that need to be addressed.
Once the plan has been approved, Terraform will proceed with the necessary actions to create, adjust or remove the infrastructure according to the plan. This may entail the creation of a new virtual machine, the updating of existing security policies, or the removal of resources that are no longer required.
Terraform manages the state of the infrastructure in a so-called "state file." This file stores the current status of the provisioned resources as well as metadata about the configuration. It is used to track which resources Terraform has created and how they have changed over time.
Terraform communicates with the APIs of various cloud computing providers or other infrastructure providers enabling us to create, modify, or remove the required resources. This typically occurs via HTTP requests to the respective APIs.
The Terraform lifecycle includes several phases from planning to deployment. Here are the key commands we should know:
Following an examination of the execution steps of Terraform, we can now draw useful conclusions regarding the handling and organisation of Terraform files. Furthermore, there are numerous valuable insights and lessons learned from practical experience that can help us avoid potential issues. Some of these will be discussed in more detail below.
As previously stated, Terraform's resource management can be conceptualised as follows: Each action undertaken via Terraform is a series of API calls. Terraform facilitates the creation, modification and deletion of infrastructure components by making API calls to the cloud provider on your behalf. Consequently, when multiple resources or modules are linked together, there is a high probability that these dependencies will result in a complex network of API calls.
This scenario can result in a significant number of API calls, all of which must succeed in order to provision the desired infrastructure (e.g. cloud monitoring) correctly. As API calls can be prone to errors, this can directly impact the stability and performance of infrastructure provisioning.
You can observe this by examining Terraform's execution plans. If the plan shows a large number of dependent resources, Terraform has to manage a complex chain of API calls. A reduction in the number of dependent API calls will result in a more stable and efficient provisioning process. This enables us to draw useful conclusions about how to minimise dependencies and associated API calls from the outset.
Terraform supports reusable configuration components known as "modules." A modular design allows you to break down Terraform configurations into reusable and composable blocks. These modules encapsulate infrastructure layers and are designed to be as independent as possible. It is advisable to avoid hard-coded values and instead pass them as input variables to ensure flexibility.
Furthermore, modules are semantically versioned to facilitate easy upgrades. These modules define configurable collections of infrastructure, saving time and promoting best practices. There are two options for sourcing modules: We can use publicly available modules from the Terraform Registry or write our own modules.
Using modules offers several advantages:
Terraform is a powerful tool that enables you to manage your cloud infrastructure efficiently. By following the described workflow phases and Terraform best practices, we can significantly enhance the quality and maintainability of your infrastructure.
The following section will present specific tips and techniques for handling input and output values, as well as optimising directory structures and resource organisation.
Stay tuned – the next part of the Terraform tips will be out soon! ;-)
Want to become part of the team?
We have received your feedback.