Terraform for Beginners

Jaseetha Satkurunathan
4 min readOct 27, 2023

--

Image from google search

What is Iac? Terraform is primarily focused on managing cloud resources but can also be used for managing on-premises infrastructure to a certain extent. While Terraform has a wide range of providers designed for cloud platforms like AWS, Azure, Google Cloud, and others. It also provides support for some on-premises infrastructure providers such as VMware, OpenStack, and various network devices.

Architecture of Terraform and how it works

Major elements of terraforms are,

  • Terraform Core
  • Providers
  • Provisioners
  • State file
  • Plugins

Terraform Core: Terraform Core is the central engine that drives the entire Terraform process. It takes the infrastructure code written in HashiCorp Configuration Language (HCL) and translates it into an execution plan. This execution plan outlines what changes need to be made to achieve the desired infrastructure state.

Providers: Providers are essential plugins in Terraform that act as intermediaries between Terraform and the various cloud, infrastructure, or service platforms. They provide the necessary APIs and communication channels to interact with these platforms.

Provisioners: Provisioners are a feature in Terraform that allows you to execute scripts or actions on resources after they are created or updated. This can be useful for tasks like bootstrapping, configuration, or software installation on the newly created infrastructure.

State File: The state file is a critical component that Terraform uses to keep track of the current state of your infrastructure. It contains information about all the managed resources, their attributes, and the mapping to the configuration. Terraform uses this state file to plan and execute changes.

Plugins: Plugins are modular components in Terraform that extend its functionality. They enable Terraform to interact with various providers and resources.

The core Terraform workflow has three steps:

  1. Write — Author infrastructure as code.
  2. Plan — Preview changes before applying.
  3. Apply — Provision reproducible infrastructure

Use Cases

  1. Multi-Cloud Deployment : Terraform lets you use the same workflow to manage multiple providers and handle cross-cloud dependencies.
  2. Terraform simplifies multi-tier app infrastructure deployment, scaling, and monitoring, managing dependencies for efficient resource provisioning.
  3. Self-Service Clusters : At a large organization, your centralized operations team may get many repetitive infrastructure requests. You can use Terraform to build a “self-serve” infrastructure model that lets product teams manage their own infrastructure independently.
  4. PaaS Application Setup
  5. Parallel Environments: Terraform lets you rapidly spin up and decommission infrastructure for development, test, QA, and production

Aspects to be aware of with Terraform

  1. Complexity: Terraform can be challenging for newcomers due to its extensive feature set, making it hard to grasp its full capabilities.
  2. State Management: Terraform relies on a state file to track resources, which can lead to issues if it falls out of sync, potentially causing problems when changes occur outside of Terraform.
  3. Performance: Managing large infrastructures in Terraform may be slower than some other IaC tools, often due to the need to communicate with multiple APIs and manage the state file.
  4. Limited Error Handling: Terraform’s error handling is not robust, making it tough to diagnose and resolve issues efficiently during infrastructure deployments.
  5. Limited Rollback Capabilities: Terraform lacks a built-in rollback feature, complicating the process of undoing changes if something goes wrong. The ‘terraform destroy’ command can be used, but it’s time-consuming and not always practical.

Basic Terraform Commands

Terraform init

Terraform init command initializes a Terraform working directory by downloading and installing any required plugins and dependencies. It should be run before any other Terraform commands.

terraform init

Terraform Validate

Terraform Validate checks your code for internal consistency and syntax errors in the configuration files within your current directory. Only the configuration files (*.tf) in the active working directory are examined
terraform validate

terraform validate

Terraform Plan

Terraform Plan examines your infrastructure code and predicts the changes it will make to the resources when applied. It doesn’t make any changes but helps you understand the impact of your configuration changes before you actually apply them

#Create Plan
terraform plan -out demo.tfplan
#view plan
terraform show demo.tfplan

Terraform Apply

Terraform Apply is the action phase. It takes your configuration and creating or updating resources as specified.

terraform apply

Terraform Destroy

Terraform destroy command will destroy all the resources created by Terraform in the current working directory. It is a useful command for tearing down your infrastructure when you no longer need it

terraform destroy

--

--

Jaseetha Satkurunathan

DevOps Engineer | DevAIOps Enthusiast | Multi-cloud Architect