Understanding Terraform (Part 1): An Introduction, Basic Commands.

Benjamin Okoli
5 min readJan 4, 2023

--

A brief understanding of Terraform, its benefits over other Provisioning tools, basic commands, and frequently used terms.

Table Of Contents

Image Source Harshicorp

Introduction

DevOps as a practice involves defining, executing, deploying, updating, and destroying different tasks in the infrastructure. Usually one would perform these tasks manually on the console. This method is slow, prone to human errors, not reusable, and frustrating.

With these drawdowns, Infrastructure as Code (IaC), a practice where you write and execute code to define, update, deploy and destroy infrastructure is a significant mindset shift where software and hardware operations are run with ease.

IaC ensures fast, safe, version-controlled, reusable, documented, and satisfying executions. And it has five broad categories: Ad hoc scripts, Configuration management tools, Server templating tools, Orchestration tools, and Provisioning tools.

For the sake of keeping it short and straight to the point, I will focus on Terraform which is one of the most popular Server Provisioning Tools, others are CloudFormation and OpenStack etc.

What is Terraform?

Terraform is an open-source Infrastructure as Code (IaC) tool under server provisioning, developed by HashiCorp and written in HarshiCorp Configuration Language (HCL), which is a simple syntax of Go Programming Language.

Terraform syntax can provision infrastructures across multiple data centers both in the cloud and on-premises. These infrastructures can also be re-provisioned in response to changes in the configuration setup.

Terraform is one of the most popular infrastructure automation tools and is very convenient for managing cloud, hybrid and multi-cloud environments.

One of the main functions of Terraform is to provision the public cloud on some of the major cloud providers such as AWS, Azure, and GCP.

Secondly, Terraform unlike other IAC tools helps to facilitate multiple cloud deployments, this means that Terraform can be used simultaneously across multiple cloud providers.

Finally, Engineers using Terraform can use the same Terraform code syntax on different tools and technologies with ease.

Benefits of Terraform

One would decide to know the benefits of Terraform over Infrastructure as Code tools, this is a broader comparison, though necessary. But here, my goal is to narrow down and guide you on why and when you should choose to use Terraform over other provisioning tools, as discussed briefly below.

  • Terraform is an open-source tool

Terraform being an open-source tool mean it s backed by a large community contributing to building its plugins in the platform. No matter the provider you use, you can find readily available plugins, extensions, and support materials. This also makes Terraform updates fast, with new benefits and improvements.

  • Terraform is an immutable infrastructure

Immutable infrastructures here mean infrastructures in Terraform can only be replaced rather than changed. Unlike Terraform, most IaC tools create mutable infrastructure, which can accommodate changes in the deployment, this often causes a drift in configuration. As more changes are made, the configuration drifts from the original causing numerous performance issues and bugs, which are often difficult to diagnose.

The servers created by terraform can never be modified after they’re deployed, If something needs to be updated, fixed, or modified in any way, new servers built from a common image with the appropriate changes are provisioned to replace the old ones.

  • Terraform is platform agnostic

While other IaC tools are designed to work with a single cloud provider, Terraform can function conveniently on the cloud service provider.

Basic Terraform Commands

The major task behind using Terraform is knowing and understanding the CLI configurations.

Listed below are the top 6 most common Terraform commands to help you navigate through the CLI.

terraform fmt

The terraform format command is used to confirm the terraform code is well formatted. This code formats the configuration in the standard style, including spacing, indentations, and so on are in the right format.

terraform init

The Terraform init command is used to initialize terraform in a working directory. This command looks at your configuration and determines which providers and modules it needs to allow your configurations to work smoothly.

terraform validate

When a directory has been initialized, it is advisable to use the validate command before the plan or apply command. This catches syntax, and version errors.

terraform plan

The terraform plan code is used to dry run a code to see what it is about to execute, this outputs your plan in other to be applied later.

Terraform apply

This is possibly the most used command in terraform, it is the command that deploys or executes terraform configurations to a provider.

terraform destroy

As the name implies, this command will destroy the infrastructure created or specific resources within the infrastructure.

You can look up basic CLI commands of terraform on Harshicorp’s official website.

Terraform Terminologies

  • Module: Terraform modules are groups of resources deployed and used together. They define input variables and output values.
  • Resource: Resources in terraform can be things like instances, virtual networks, Domain name servers, load balancers, etc. A resource block describes one or more infrastructure objects that instructs Terraform to manage the described resource.
  • State: Terraform state is used to store bindings between objects in a remote system and the resources declared in the configuration file.
  • Backend: Terraform’s backend defines where terraform stores its state data files.
  • Root Module: A root module in terraform refers to where Terraform begins evaluating its configuration.
  • Variables: In Terraform, variables often mean input variables, these are key/ value pairs used in a Terraform module.
  • Output: Terraform output values or outputs are data exported by a terraform module, this data can be displayed by a user and used to program other Terraform codes.

You can find more terraform keywords on the official site’s glossary.

Conclusion

You now have a basic understanding of Terraform, the benefits it has over other server provisioning Infrastructure as Code tools, and some common terms used while working with it. Check the Terraform documentation for any information you need on the topic.

The next part of this article will be more practical, as we would use some of these commands in practice, and use terraform to deploy a couple of services.

Now we have come to the end of this brief article, I hope it was helpful, kindly leave a Clap, a Like, and please leave a Comment on any thoughts, questions, or suggestions, as this would help me improve my learning journey. Until then, have a wonderful day.

--

--