Terraform 101

Rumesh Bandara
devopsanswers
Published in
4 min readJan 12, 2023
Terraform by Hashicorp

Introduction

Terraform is an open-source tool created by HashiCorp, and it has gained popularity in recent years as more and more companies are moving towards cloud-based infrastructure. It supports multiple cloud providers, such as Amazon Web Services (AWS), Microsoft Azure, Google Cloud Platform (GCP) as well as on-premises and other custom solutions.

Features

One of the key features of Terraform is its ability to represent infrastructure as code. This means that infrastructure can be defined using a simple, human-readable programming language, rather than manually configuring resources through a web interface. This has several benefits including version control, collaboration and repeatability.

Another important feature of Terraform is its ability to manage infrastructure across multiple cloud providers. This is particularly useful for organizations that use multiple cloud providers, or that need to migrate workloads between cloud providers. Terraform can also manage on-premises resources, such as servers and networking equipment, in addition to cloud-based resources.

Terraform Workflow

Terraform Workflow by Hashicorp

Terraform includes a built-in plan and apply workflow, which allows users to preview changes to infrastructure before they are applied. This can help prevent mistakes and unintended changes to resources. Additionally, Terraform’s state management capabilities allow users to keep track of the current state of their infrastructure, and roll back changes if necessary.

The typical workflow for using Terraform includes the following steps:

  1. Initialize Terraform: Before you can start using Terraform, you need to initialize it by running the terraform init command. This command downloads the required providers and sets up the necessary files for Terraform to run.
  2. Write Terraform Configuration: Next, you need to write your Terraform configuration, which defines the resources you want to create, modify, or delete. Terraform configuration files use the HashiCorp Configuration Language (HCL) and are saved with the .tf file extension.
  3. Plan the Changes: Once you have written your Terraform configuration, you can use the terraform plan command to create an execution plan. This command shows you what changes Terraform will make to your infrastructure and allows you to preview the changes before they are applied.
  4. Apply the Changes: If the plan looks good, you can apply the changes by running the terraform apply command. This command creates or modifies the resources defined in your Terraform configuration.
  5. Monitor and Manage State: After applying the changes, you can use the terraform show command to view the current state of your infrastructure, and the terraform state command to manage the state of your resources.
  6. Destroy the Resources: When you no longer need the resources created by Terraform, you can use the terraform destroy command to delete them.

Why Terraform State?

Terraform State by gruntwork

In Terraform, the state refers to the current configuration of your infrastructure as Terraform understands it. This includes information about all the resources that Terraform is managing, such as their current properties, IDs, and dependencies. The state is stored in a file called the state file, which is typically saved locally but can also be stored remotely.

The state file is used by Terraform to keep track of the current state of your infrastructure, and it is updated every time you run the terraform apply command. This allows Terraform to know what changes need to be made to your infrastructure in order to bring it in line with your configuration files.

Terraform uses the state file to know what resources need to be created, updated, or deleted. It also uses the state file to track the metadata of the resources, such as their unique identifiers and properties. This information is used to ensure that Terraform makes the correct changes to your infrastructure when you run the terraform apply command.

Remote State

There are different state management options that Terraform provides, such as storing the state file locally, in a remote backend like S3, Consul, or etcd. Remote state management is useful when you have a team working on the same infrastructure, it allows to share the state information and ensure consistency.

Managing the state file is an important aspect of using Terraform, and it’s important to keep it safe and secure. It’s also important to make sure that you have a backup of the state file so that you can recover your infrastructure in case of an emergency.

Integration

Also, Terraform has great integration with other tools like Ansible, Jenkins, and many other CI/CD tools, you can use Terraform in conjunction with these tools to automate your entire infrastructure provisioning and management workflow.

In summary, Terraform is a powerful tool for managing infrastructure as code, across multiple cloud providers and on-premises resources. Its ability to preview and track changes, along with its state management capabilities, make it a valuable tool for organizations looking to improve their infrastructure management processes.

Stay tuned for the next article which we can dive deep into Terraform in Action.

More Cloud Learning Resources

Kubernetes The Easy Way

--

--