Skip to content

What Is Terraform? How the Tool That Defines Infrastructure in Code Works

Terraform is an open-source tool that defines infrastructure resources such as servers, networks, and databases in code and sends them to cloud providers as API calls. The article explains the tool's workflow, the HCL language, the environments it supports, and its limits.

Nurullah Okatan · Content Writer
· Updated: · 14 min read

What Is Terraform? How the Tool That Defines Infrastructure in Code Works

Terraform is an open-source tool that defines infrastructure resources such as servers, networks, and databases in configuration files and provisions them automatically instead of setting them up by hand. It translates the written code into API calls to the cloud provider and is most often used by teams that need to repeatedly and reliably build the same environment.

Terraform, an open-source tool that defines infrastructure as code

Terraform was developed by HashiCorp. According to Kerteriz, the tool is written in Go and compiled into a single binary file called terraform. That is why installation amounts to downloading the file and placing it on your executable path.

Efficiency in Infrastructure Management with Terraform

Linuxpedi describes Terraform as an Infrastructure as Code (IaC) tool. DevOps engineers use it to provision the resources an application needs programmatically.

In a manual setup, an administrator logs into the cloud console, selects a server, configures the network, and adds security rules one by one. These steps are not recorded anywhere. When the same environment has to be built a second time, small differences appear. In the IaC approach, infrastructure is a text file. The file is stored in a version control system such as Git, changes are reviewed, and it is possible to roll back to an earlier version.

According to HashiCorp's definition, Terraform creates, modifies, and versions cloud and on-premises infrastructure. The resources it can manage fall into two groups. Compute, storage, and networking are low-level components, while DNS records and SaaS features are high-level components. Most servers, security, and backup tasks in infrastructure and support work fall into one of these two groups.

AI assistant for this topic

Türk Bilişim AI answers your questions instantly
Online
  1. Hi! Ask whatever you're curious about.

How does Terraform work?

Terraform reads the written configuration file and turns it into requests to be sent to the cloud provider. According to Kerteriz, the binary parses the code and generates a series of API calls to the providers named in the code. Linuxpedi explains the same process with an example. When a command is given for a server, database, or load balancer, Terraform translates the code into an API call to the provider.

According to HashiCorp, the basic workflow consists of three stages. Each stage corresponds to one step.

StageWhat is doneResult
WriteResources are defined in HCL files.The desired infrastructure is ready as text.
PlanThe terraform plan command is run. Terraform compares the code with the current state.A list is produced of what will be added, changed, or deleted.
ApplyThe plan is approved with terraform apply.API calls are sent, and resources are created or updated.

The planning stage gives a readable report before any change reaches the live environment. The team can see in this report that a database is about to be deleted and stop the operation.

The state file is Terraform's memory. According to AWS's comparison article, Terraform stores information about infrastructure components in state files. This file maps each resource in the code to the real resource in the cloud. Without the file, whose default name is terraform.tfstate, Terraform cannot know which server belongs to it.

Looking for Android App Development?

Writing declarative configuration files in HCL

HCL (HashiCorp Configuration Language) is Terraform's configuration language. Linuxpedi describes it as a JSON-like language. According to AWS's article, HCL is a domain-specific language, and JSON was also used in Terraform v0.12 and earlier. The practical difference is readability. HCL allows comments, variables, and short block syntax, while JSON is better suited to being generated by machines.

An HCL file consists of blocks. The provider block states which provider to work with. The resource block describes the resource to be created, such as a virtual machine's size, region, and disk capacity.

According to HashiCorp, Terraform files are declarative, meaning they describe the desired final state the infrastructure should reach. The file says "there should be three servers" and does not say in what order the servers should be opened. If there are two servers today, Terraform adds one. If there are four, it deletes one.

Terraform works out the dependencies itself. According to IBM's explanation, the user describes the desired state and Terraform builds the execution plan. It identifies the dependencies between resources and builds the components in the right order. If a server is connected to a network, the network is built first. Other articles on writing code are in the Custom Software Development category.

Which cloud providers and services does Terraform work with?

According to Coderspace's roadmap, Terraform works with many cloud providers, including AWS, Azure, and Google Cloud, and with other services. A separate plugin (provider) is installed for each provider.

Advantages and Disadvantages of Terraform

Three clouds can be managed with the same tool and the same language, but not with the same code. An AWS virtual machine block does not work in Azure. Resource names and parameters are provider-specific. What is common is the workflow, the HCL syntax, and the state file logic. A team using multiple clouds writes a separate module for each provider and combines them in a single project.

It can also be used on on-premises servers. HashiCorp defines Terraform as a tool that manages cloud and on-prem resources. Providers also exist for virtualization platforms and network devices. The condition is that the target system exposes an API. Terraform cannot manage an old physical server that has no API.

Turkey's best AI Chatbot and Customer Assistant company, Türk Bilişim, has been developing AI solutions such as chatbots and automation since 2022. For organizations that want to outsource server setup and daily operations, the firm also offers a Web Hosting & Server Management service. The firm has been running digital projects since 1998.

Terraform's limits and what to do in the event of a failure

Terraform does not automatically restore infrastructure after a failure. According to AWS's comparison article, the tool provides no automatic failure recovery. To rebuild infrastructure during an incident, the user writes and runs scripts. Terraform only runs when a command is issued. It is not a process that monitors a crashed server and restarts it.

The order to follow during an incident is as follows:

  1. Run terraform plan and read the difference between the state in the code and the actual state.
  2. Resources that were deleted or broken appear in the plan as "to be added."
  3. If the plan is correct, rebuild the resources with terraform apply.
  4. Data such as database contents is restored from a separate backup, because Terraform builds the resource, not the data.

If the state file is lost, Terraform does not recognize the resources in the cloud. The next apply command may try to create the same resources a second time. To bring existing resources back into the file, run terraform import resource by resource. In large projects this work can take days.

The way to protect against this is not to keep the file on the local disk. The state file is stored in a remote backend with version history enabled and is locked against concurrent writes. The file may contain secret values such as passwords, so it is not added to a Git repository. For organizations that want to set up a backup arrangement, Türk Bilişim offers the Data Backup & Business Continuity service.

Looking for AI Voice Assistant & Call Bot?

Frequently asked questions

Is Terraform free?

Kerteriz and Linuxpedi describe Terraform as an open-source tool. No payment is required to download and use the tool. The cost of the servers and services that are set up is paid to the cloud provider.

Do you need to know programming to learn Terraform?

You do not need to know how to write loops and classes. HCL is a configuration language. The knowledge that is really needed is how the network, servers, and permissions to be set up work.

Do Terraform and Kubernetes do the same job?

No. Terraform builds resources such as servers, networks, and clusters. Kubernetes runs containers on top of those resources and restarts a crashed container. The two tools are used together in most projects.

Is Terraform necessary for a small website?

For a site that runs on a single server and changes a few times a year, it is usually not necessary. If copies of the same environment for testing and production are being set up, or the number of resources is growing, defining them in code saves time.

Have a project?

Türk Bilişim · Free Quote

Get a free quote for your AI Chatbots & Customer Assistants project

Leave your number and we will get back to you right away. The call is free.

Read the privacy notice

Nurullah Okatan Content Writer

The editorial desk of a team delivering websites, e-commerce and software projects since 1998.

LinkedInInstagramFacebook

Share this article

Comments

Comments

No comments yet.

Write a comment

Your rating (optional)

Privacy notice

We will send a confirmation link to your email so that your comment can appear.

Türk Bilişim · Digital Transformation Advisor

Online
Hi! Tell us the project or idea on your mind and let’s begin together.
Ask anything, Türk Bilişim AI answers instantly.