TechyPilots Info
TechyPilots.INFO
Home/Tutorials/Terraform/Terraform for Beginners
Infrastructure as Code

Terraform for Beginners

Learn the fundamentals of Terraform and Infrastructure as Code, including providers, resources, variables, state, modules, and the Terraform workflow.

Level: Beginner
Topic: Terraform & IaC

Terraform Workflow

Write, Plan & Apply

Write Configuration

Initialize Providers

Validate Configuration

Create Execution Plan

Apply Changes

Manage Infrastructure

Introduction

What is Terraform?

Terraform is an Infrastructure as Code tool that allows you to define, change, and manage cloud and on-premises infrastructure using human-readable configuration files.

Instead of manually creating infrastructure through cloud dashboards, you define the desired infrastructure in code. Terraform then compares that configuration with the managed infrastructure and determines the required changes.

Terraform follows a declarative approach, meaning you describe the desired end state while Terraform determines the operations required to create or modify the infrastructure.

Core Concepts

Understanding Terraform fundamentals

Before creating infrastructure with Terraform, it is important to understand the main building blocks used in Terraform configurations and workflows.

01

Infrastructure as Code

Infrastructure as Code allows you to define and manage infrastructure using configuration files instead of manually creating resources through dashboards.

Key Topics

  • Declarative configuration
  • Version control
  • Repeatable infrastructure
  • Automation
02

Providers

Terraform providers connect Terraform to cloud platforms, infrastructure services, and other APIs so Terraform can manage their resources.

Key Topics

  • AWS
  • Azure
  • Google Cloud
  • Kubernetes
03

Resources

Resources represent infrastructure objects that Terraform can create, update, and manage through a provider.

Key Topics

  • Virtual machines
  • Networks
  • Storage
  • Databases
04

Terraform State

Terraform tracks managed infrastructure using state information, which helps Terraform determine the changes required to reach the desired configuration.

Key Topics

  • State tracking
  • Resource mapping
  • Change planning
  • Remote state

Project Structure

Common Terraform files

Terraform configurations can be organized into multiple files. The Terraform CLI loads configuration files from the working directory and evaluates them together.

main.tf

Usually contains the primary infrastructure resources and provider configuration for a Terraform project.

variables.tf

Defines input variables that allow Terraform configurations to be reused with different values.

outputs.tf

Defines output values that Terraform displays after applying infrastructure changes.

terraform.tf

Can define Terraform version requirements, required providers, backend configuration, and other Terraform settings.

terraform.tfvars

Can provide values for input variables used by the Terraform configuration.

modules/

A directory that can contain reusable Terraform configuration components.

Your First Configuration

Define infrastructure with Terraform

Terraform configurations use blocks and arguments to describe the infrastructure you want to manage. Providers connect Terraform to infrastructure APIs, while resource blocks define the objects to manage.

The example below demonstrates a simple provider and resource configuration structure.

Example main.tf

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "web" {
  ami           = "ami-example"
  instance_type = "t2.micro"

  tags = {
    Name = "terraform-web-server"
  }
}

Example variables.tf

variable "region" {
  description = "AWS region"
  type        = string
  default     = "us-east-1"
}

variable "instance_type" {
  description = "EC2 instance type"
  type        = string
  default     = "t2.micro"
}

Input Variables

Make Terraform configurations reusable

Variables allow you to parameterize Terraform configurations. Instead of hardcoding values for every environment, you can define reusable input variables.

This approach makes it easier to use the same Terraform configuration across development, testing, and production environments.

Output Values

Display useful infrastructure information

Output values allow Terraform configurations to expose information about resources after Terraform applies the configuration.

Outputs can be useful for displaying resource IDs, addresses, endpoints, and other information generated during infrastructure provisioning.

Example outputs.tf

output "instance_id" {
  description = "ID of the created instance"
  value       = aws_instance.web.id
}

Terraform Workflow

The Infrastructure as Code lifecycle

Terraform follows a repeatable workflow for defining, reviewing, applying, and managing infrastructure changes.

01

Write

Create Terraform configuration files that describe the infrastructure you want to manage.

02

Initialize

Run terraform init to prepare the working directory and install required providers and modules.

03

Plan

Run terraform plan to review the proposed infrastructure changes before applying them.

04

Apply

Run terraform apply to execute the approved changes and provision or update infrastructure.

05

Manage

Update configuration files as infrastructure requirements change and repeat the Terraform workflow.

06

Destroy

When infrastructure is no longer required, Terraform can remove managed resources using terraform destroy.

Terraform Commands

Essential commands for beginners

The Terraform CLI provides commands for initializing projects, formatting configuration, validating syntax, reviewing changes, applying infrastructure, and removing managed resources.

Always review planned infrastructure changes before applying them, especially when working with existing or production resources.

Basic Terraform Commands

# Initialize Terraform
terraform init

# Format configuration
terraform fmt

# Validate configuration
terraform validate

# Review planned changes
terraform plan

# Apply infrastructure changes
terraform apply

# Remove managed infrastructure
terraform destroy

Use Cases

What can you do with Terraform?

Cloud Infrastructure

Provision and manage cloud resources such as virtual machines, networks, storage, and databases.

Multi-Cloud Environments

Manage infrastructure across different cloud providers using Terraform configurations and providers.

Kubernetes Infrastructure

Manage Kubernetes-related infrastructure and resources using Infrastructure as Code workflows.

Reusable Infrastructure

Create reusable Terraform modules for common infrastructure patterns and environments.

DevOps Automation

Integrate Terraform workflows into automated pipelines for consistent infrastructure changes.

Environment Management

Use configuration, variables, and reusable components to manage different infrastructure environments.

Learning Roadmap

How to learn Terraform

Start with the basics of Infrastructure as Code and gradually move toward reusable configurations, state management, modules, cloud infrastructure, and automated Terraform workflows.

Step 1

Understand Infrastructure as Code

Learn why infrastructure can be defined using version-controlled configuration instead of manual resource creation.

Step 2

Install Terraform

Install the Terraform CLI and verify that the command is available in your terminal environment.

Step 3

Learn Terraform Syntax

Understand blocks, arguments, expressions, resources, variables, and outputs in Terraform configuration files.

Step 4

Configure a Provider

Connect Terraform to a cloud platform or infrastructure service using the appropriate provider.

Step 5

Create Infrastructure

Write Terraform resources and use the plan and apply workflow to manage infrastructure.

Step 6

Learn Advanced Concepts

Continue with state management, modules, remote backends, reusable configurations, and automation workflows.

Best Practices

Terraform best practices for beginners

Use Version Control

Store Terraform configuration in a version control system so infrastructure changes can be tracked and reviewed.

Run terraform fmt

Format Terraform configuration consistently to improve readability and maintain a standard project structure.

Validate Configuration

Use validation tools and review your configuration before applying infrastructure changes.

Review the Plan

Inspect the proposed execution plan before applying changes to understand what Terraform intends to modify.

Protect State

Treat Terraform state carefully because it is important for tracking managed infrastructure and may contain sensitive information.

Build Reusable Modules

As projects grow, organize repeated infrastructure patterns into reusable and configurable modules.

Next Step

Start building infrastructure with Terraform

Practice writing Terraform configurations, defining resources, using variables and outputs, reviewing execution plans, and managing infrastructure through Infrastructure as Code.