DEV Community

Cover image for Introduction to Terraform: Simplifying Infrastructure as Code
Ritesh Kokam
Ritesh Kokam

Posted on

Introduction to Terraform: Simplifying Infrastructure as Code

In today's fast-paced world of software development, managing infrastructure can be a complex and time-consuming task. However, with the maturity of Infrastructure as Code (IaC) tools, developers and system administrators have powerful solutions to automate and streamline provisioning. In this post, we'll delve into Terraform, the industry-standard tool for this purpose, while also touching on the shifts in its ecosystem that every engineer should know about.


What is Terraform?

Terraform, developed by HashiCorp (now an IBM company), is a source-available infrastructure provisioning and configuration management tool. It enables developers and operations teams to define and manage infrastructure resources declaratively using simple, human-readable configuration files.​

With Terraform, you can provision and manage infrastructure components, such as virtual machines, networks, and storage across a variety of providers, including AWS, Microsoft Azure, and Google Cloud Platform (GCP).

Note on Licensing: In 2023, Terraform transitioned from a purely open-source license (MPL) to the Business Source License (BSL). While it remains free for personal and internal corporate use, this change led to the creation of OpenTofu, a Linux Foundation-backed open-source fork that maintains strict community governance. The concepts covered here generally apply to both tools.​


Why is Terraform used?

Terraform offers numerous benefits that make it a preferred choice for infrastructure provisioning:

  1. Infrastructure as Code (IaC): Terraform treats infrastructure as code, allowing you to define, version, and manage configurations in a repository. This approach enables collaboration via Git, version control, and CI/CD automation, leading to greater consistency.

  2. Multi-Cloud and Hybrid Support: Terraform provides a unified interface to provision resources across multiple cloud providers. It acts as a common language (HCL) for managing resources, whether they are on AWS, Azure, or on-premises VMWare.

  3. Declarative Language: The HashiCorp Configuration Language (HCL) is declarative, focusing on describing the desired end state rather than the step-by-step scripts to achieve it. You tell Terraform what you want, and it figures out how to build it.

  4. State Management & Dependency Graph: Terraform maintains a "state file" that maps your code to real-world resources. It analyzes dependencies (e.g., a server needing a subnet) and automatically determines the correct order for provisioning, minimizing human error.​

  5. Immutable Infrastructure: Terraform encourages an immutable approach where, instead of patching old servers, you replace them with new ones. This reduces configuration drift and ensures reliable deployments.


How does Terraform work?

Terraform operates using a standard three-step workflow:

Define (Write): Infrastructure configurations are defined in .tf files. You describe resources, providers, and variables using HCL.

Plan: You run terraform plan. Terraform compares your code against your current infrastructure state and generates an "execution plan." This preview shows exactly what will be created, modified, or destroyed, allowing you to catch errors before they happen.

Apply: After approval, you run terraform apply. Terraform interacts with the cloud provider APIs to execute the plan and reach the desired state.

The Terraform workflow has three steps: Write, Plan, and Apply


Terraform Example

Here is a modern example of Terraform usage. This configuration follows current best practices by using a data source to automatically fetch the latest Amazon Machine Image (AMI) and locking provider versions for stability.

# main.tf

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "web_server" {
  ami           = "ami-008fe2fc65df48dac"
  instance_type = "t3.micro" 

  tags = {
    Name        = "production-web-server"
    Environment = "Production"
  }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

  • The terraform block locks the provider version to ensure stability.
  • The data block dynamically queries AWS for the latest Ubuntu image ID.
  • The resource block provisions the EC2 instance using that dynamic ID.

Conclusion

Terraform empowers developers to manage infrastructure with the same rigor as application code. By leveraging its declarative syntax and state management, teams can achieve efficiency, reproducibility, and scalability. Whether you choose HashiCorp's Terraform or the open-source OpenTofu, mastering the HCL syntax remains one of the most valuable skills in modern DevOps and cloud engineering.

Top comments (0)