Introduction to Terraform: Understanding Basic Architecture

|
Published:
|
|

In this tutorial, we will dive into introduction to Terraform so as to understand its basic architecture. With the ever evolving technology, infrastructure management has become more complex right from virtualization, cloud services and distributed systems. This has called for a need of a tool that can DevOps engineers to seamlessly provision, manage and scale the infrastructure services. This is where Terraform, an Infrastructure as Code tool, comes in. So, let’s look at what Terraform is, key concepts and its basic architecture.

Introduction to Terraform: Understanding Basic Architecture

What is Terraform?

Terraform is an open source IaC (Infrastructure as Code) tool created by HashiCorp to simplify the process of provisioning infrastructure resources by allowing you to specify the desired end state of your infrastructure leaving it to Terraform to handle the details of reaching that state through the use of a declarative language. This approach offers flexibility in managing and updating your resources across cloud platforms like AWS, GCP, Azure… and even, on premise environments in an effective manner.

Assume a situation where you want to deploy your application. Before you can deploy your application, you need an infrastructure to run that application atop. The infrastructure will most probably require servers to be setup, user accounts and relevant permissions created, setup networking, install various packages and system security controls in place. Therefore, this is where Terraform come into play, to help you with all this hassle.

One of the key feature of Terraform is the ability to provision an immutable infrastructure through version control. What this means is that, if you have provisioned your infrastructure using Terraform, then whenever there are changes that needs to be made on the current infrastructure, you can re-provision the infrastructure as a new version by implementing the relevant changes only and leaving the previous infrastructure state as a previous version. See, this even allows you to roll back to the previous state in case things doesn’t go as planned.

Basic Terraform Concepts

Infrastructure as Code (IaC)

IaC is the core concept of Terraform. IaC takes all the manual processes that you usually take to setup your infrastructure through the use of traditional scripts and turns them into a code that is defined in a configuration file. This gives you the ability to automate your whole infrastructure setup without actually stating all the necessary steps that are needed to be taken to have your infrastructure up and running. At the same time, it reduces errors that infrastructure admins are likely to make, gives the ability to version control, review, and collaborate on infrastructure changes just like how any other software development processes are done.

Declarative Configuration

In programming, a declarative language is a language which states what needs to be done without giving steps required to take to have the task done. It is also called non-procedural language. Terraform configuration code is declarative and thus, you can only define a desired state of your infrastructure without outlining the steps to get to that state as long as you know what resources are required to get your infrastructure up and running.

Providers

As already mentioned, Terraform is used to automate the provisioning of infrastructure resources across multiple cloud platforms, and even on-premise environments. For Terraform to connect and interact with such platforms/services/environments, it requires some plugins. These plugins is what is hereby called providers. The providers are defined in the Terraform configuration.

A comprehensive list of providers is documented well on the Terraform Registry.

Resources

Resources are the building blocks of Terraform language. They define specific components of the infrastructure that you want to be provisioned. For example virtual machines, networks, storage buckets, databases, local files, etc. Every provider has its own resource types along with the attributes that can be used to customize respective resource.

Resources are the building blocks of Terraform language. They define specific components of the infrastructure that you want to be provisioned. For example virtual machines, networks, storage buckets, databases, local files, etc. Every provider has its own resource types along with the attributes that can be used to customize respective resource.

Resources are defined within a resource block in a Terraform configuration. When defining a resource, you specify which provider the resource belongs to so that Terraform is aware which API endpoints to interact with and which operations to perform on the resource.

Data Sources

While Terraform can use resources defined within a single configuration to manage an infrastructure, sometime it may need to use other resources defined in other Terraform configurations. A data resource is declared using a data block in a Terraform configuration.

Terraform Provisioners

Provisioners is a featured used in Terraform that can be used to execute tasks that Terraform cannot natively handle such as executing scripts or commands on local or remote machines after a resource has been created or updated. Such scripts/commands maybe be used to run commands to install packages, create files/directories, configuring some services, running initial commands, copying files, e.t.c on provisioned instances.

There are three types of provisioners;

  • local-exec: This is used to execute commands on the local Terraform machine where the code is running.
  • remote-exec: This is used to connect to a provisioned instance (remote machine) via SSH and execute commands. As such, you need to ensure that SSH access to provisioned instances is secure and properly configured.
  • file: This is used to copy/or transfers files from the local machine to a provisioned remote machine.

Terraform Variables and Outputs

Terraform uses variables to parameterize its configurations. It makes it easy to define dynamic values or user-defined values for customizing your infrastructure deployments based on different environments, deployment scenarios, or user inputs.

There are different types of Terraform variables.

  • Input variables: These are variables that are explicitly defined by a user when running Terraform commands, either interactively or via the use of command line arguments.
  • Environment variables: These are variables that are part of the environment in which a process is running. Terraform program automatically searches and read all environment variables with the TF_VAR_ prefix found within the environment it is running.
  • Expression variables: These are variables that hold values based off a Terraform expression.

Terraform Outputs on the other hand provide a way to expose some information about a resource after it has been created. For example, you can declare a variable to expose an IP address of an instance, an instance ID, a username, password, a computed value from a specific process e.t.c.

Terraform Modules

Terraform modules define a collections of Terraform configurations (.tf ot .tf.json) residing within a directory and managed as a group. A group may include a main Terraform configuration, variable configuration, outputs configuration…They make it easy to reuse the configurations over and over.

A Terraform modules can either be root module, which is the main Terraform configuration or a child module, which is a module that is called by other main modules for inclusion in their resource configuration.

Terraform modules can reside in the local filesystem or can be published to a private or publish Terraform registry.

Terraform Checks

Terraform checks defines the mechanism that Terraform uses to check for correctness, consistency and validity of Terraform resource configurations.

Terraform State

Terraform stores the map of the current state of your infrastructure in a file called terraform.tfstate located in the same directory as your Terraform configuration files. The state map stores various information about managed infrastructure resources such as instances, instance IDs, virtual machines… thus helping Terraform to understand the existing resources, their attributes, and any dependencies between them. It serves as the source of truth for Terraform, enabling it to determine the necessary actions to achieve the desired state declared in your configuration files.

Terraform CLI

The terraform command provides a command line interface for Terraform. The command servers as the primary interface to interact with Terraform on command line. terraform command provides various command line options to initialize or prepare Terraform working directory, validate the Terraform configuration, show changes required by the current configuration, create or update infrastructure or even destroy previously-created infrastructure.

Terraform’s Basic Architecture

How does Terraform actually provision infrastructure resources? Here is a basic Terraform architecture with a summary of its operation.

basic terraform architecture

There are two main components of Terraform:

  • Terraform Core: At the core of Terraform are Terraform configuration and providers/plugins.
    • Terraform configurations: These are document files written in Terraform declarative language that defines a desired state of a resource to be provisioned along with their dependencies. Terraform configurations are text files with the .tf file extension or .tf.json for the JSON variants.
    • State Files: Terraform state files, often named terraform.tfstate, is used to store and track the current state of the infrastructure. This file is refreshed and updated by Terraform with the current state of the infrastructure before any changes are applied. It is recommended to store this file in cloud, versioned, encrypted and secured.
  • Terraform Providers/Plugins: As already mentioned, Providers are plugins that interface with various infrastructure platforms. They offer a collection of resources and data sources that Terraform can manage.

Typical Terraform Workflow involves a number of steps, that can be controlled using Terraform CLI;

  1. Write Terraform configuration to define the desired state of your infrastructure and its dependencies.
  2. Initialize Terraform Working Directory. Once you are done writing the configuration, you can initialize it using terraform init command. The initialization command scans the Terraform configuration and downloads the required plugins from the registry. This information is stored on the .terraform subdirectory within the working directory. Another lock file is also created to track the provider dependencies and store checksum hashes for installed plugin binaries (to ensure integrity). The file is named as .terraform.lock.hcl and is stored under the working directory.
  3. Planning: After preparing your Terraform working directory, you can now execute terraform plan command to view the changes that Terraform will make on the infrastructure.
  4. Application: To create or update your infrastructure based on your planned actions, execute terraform apply command.

And of course, you can view more terraform command line options for other actions.

Install Terraform on Ubuntu Linux

Now that you have a basic understanding of the core concepts and workflow of Terraform, you are ready to get started.

Check the guide below on how to install Terraform so you can play around with it.

Install Terraform on Ubuntu 24.04

Conclusion

That concludes our guide on basic introduction to Terraform and its basic architecture. Happy Terraforming!

Read more on Terraform Documentation page.

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment