Terraform Basics: A Step-by-Step Guide for Beginners¶
Welcome to the world of Infrastructure as Code (IaC)! This guide will take you from zero to provisioning your first infrastructure using Terraform.
1. What is Terraform?¶
Terraform is an open-source tool by HashiCorp that allows you to define and provision infrastructure using a high-level configuration language called HCL (HashiCorp Configuration Language).
Key Concepts:
- IaC (Infrastructure as Code): Managing infrastructure via code files rather than manual GUI clicks.
- Provider: A plugin that allows Terraform to interact with cloud platforms (AWS, Azure, GCP, Kubernetes, etc.).
- Resource: A specific piece of infrastructure (e.g., an EC2 instance, a storage bucket).
- State: A file (terraform.tfstate) that keeps track of the resources Terraform manages.
2. Installation¶
Mac (using Homebrew)¶
brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Linux (Ubuntu/Debian)¶
sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
wget -O- https://apt.releases.hashicorp.com/gpg | \
gpg --dearmor | \
sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt-get install terraform
Windows¶
Download the binary from the official website or use Chocolatey:
choco install terraform
Verify installation:
terraform -version
3. Your First Terraform Project (Local File)¶
Let's start with the simplest possible example to understand the workflow: creating a local text file.
-
Create a directory for your project:
mkdir my-first-terraform cd my-first-terraform -
Create a file named
main.tf:resource "local_file" "pet" { filename = "${path.module}/pet.txt" content = "We love pets!" } -
Initialize: This downloads the necessary plugins (the "local" provider).
terraform init -
Plan: See what Terraform intends to do.
Output should show:terraform plan+ resource "local_file" "pet"creates a new resource. -
Apply: Create the resource.
(Typeterraform applyyeswhen prompted). -
Verify: Check if the file was created.
cat pet.txt -
Destroy: Clean up resources.
terraform destroy
4. Basic Terraform Workflow¶
Every Terraform project follows this lifecycle:
- Write: Write HCL code in
.tffiles. terraform init: Initialize the backend and providers.terraform validate: Check syntax.terraform plan: Preview changes.terraform apply: Apply changes to reach the desired state.terraform destroy: Remove infrastructure.
5. Understanding the HCL Syntax¶
<BLOCK TYPE> "<BLOCK LABEL>" "<BLOCK NAME>" {
# Argument matches the specific resource type
identifier = expression
}
Example:
resource "aws_s3_bucket" "example" {
bucket = "my-unique-bucket-name"
}
resource: The block type.
- aws_s3_bucket: The resource type (provided by AWS provider).
- example: The local name (used to refer to this resource elsewhere in the code).
What's Next?¶
Now that you understand the basics, let's look at variables, outputs, and maintaining state.
๐ฌ DevopsPilot Weekly โ Learn DevOps, Cloud & Gen AI the simple way.
๐ Subscribe here