1
 
 
Account
In your account you can view the status of your application, save incomplete applications and view current news and events
November 12, 2024

Terraform Tips: Scalable and Reliable Infrastructure-as-Code

What is the article about?

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 Workflow: From Code to Cloud Resources

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:

Configuration analysis

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.

Planning

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.

Execution

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.

State Management

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.

Resource Management

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.

Terraform Infrastructure as Code: Workflow
Terraform Infrastructure as Code: Workflow

Terraform: The Essential Commands for Managing Your Cloud Infrastructure

The Terraform lifecycle includes several phases from planning to deployment. Here are the key commands we should know:

  • terraform init: This command initialises the working directory where your Terraform configurations are stored. It downloads the required provider plugins and modules preparing Terraform for execution.

  • terraform plan: This command creates an execution plan that shows what actions will be taken to transition the current state of the infrastructure to the desired state. This allows you to review the planned changes before they are actually applied.

  • terraform apply: This command executes the changes described in the plan and brings the infrastructure to the desired status.

  • terraform destroy: This command removes all managed resources. This is useful when we want to completely reset an environment.
Terraform Lifecycle
Terraform Lifecycle

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.

Modular Design and Reusability of Our Resources

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.

How to Structure Terraform Files: Bad Way of API Calls
How to Structure Terraform Files: Bad Way of API Calls

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:

  • Consistency and Reproducibility: By using modules, we can easily deploy best practices in different projects.
  • Maintenance and Updates: Changes can be centrally managed and applied to all instances of a module, making maintenance easier.
  • Clarity: Modules allow you to break down complex infrastructures into manageable, well-structured units, thereby enhancing troubleshooting capabilities.
  • Collaboration: Clearly defined and documented modules promote teamwork, as they can be more easily shared and developed together. You could centralize the reusable modules in their own directory or even repository.
  • Security Practices: By using modules, best security practices and compliance standards can be more easily implemented and enforced, improving the overall quality and security of the infrastructure.
How to Structure Terraform Files: Good Way of API Calls
How to Structure Terraform Files: Good Way of API Calls

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?

3 people like this.

0No comments yet.

Write a comment
Answer to: Reply directly to the topic

Written by

Nina Braunger
Nina Braunger
Junior Software Developer

Similar Articles

We want to improve out content with your feedback.

How interesting is this blogpost?

We have received your feedback.

Allow cookies?

OTTO and four partners need your consent (click on "OK") for individual data uses in order to store and/or retrieve information on your device (IP address, user ID, browser information).
Data is used for personalized ads and content, ad and content measurement, and to gain insights about target groups and product development. More information on consent can be found here at any time. You can refuse your consent at any time by clicking on the link "refuse cookies".

Data uses

OTTO works with partners who also process data retrieved from your end device (tracking data) for their own purposes (e.g. profiling) / for the purposes of third parties. Against this background, not only the collection of tracking data, but also its further processing by these providers requires consent. The tracking data will only be collected when you click on the "OK" button in the banner on otto.de. The partners are the following companies:
Google Ireland Limited, Meta Platforms Ireland Limited, LinkedIn Ireland Unlimited Company, TikTok Information Technologies UK Limited
For more information on the data processing by these partners, please see the privacy policy at otto.de/jobs. The information can also be accessed via a link in the banner.
You can also withdraw your consent at any time without giving any reason by clicking on the button 'Cookie Settings' in the footer of the website and 'Refuse Cookies'.