Wednesday, October 29, 2025

How to Install Terraform and Create Your First Resource (Step-by-Step)

How to Install Terraform and Create Your First Resource (Step-by-Step)
Terraform Installation Beginner Tutorial DevOps

How to Install Terraform and Create Your First Resource

Published on: October 30, 2023 | Author: DevOps Engineering Team

Hands-On Terraform: Your First Infrastructure as Code

Welcome to Part 2 of our Terraform Mastery Series! In this hands-on guide, you'll install Terraform on your machine and write your first configuration to create a real cloud resource. By the end of this tutorial, you'll have executed the complete Terraform workflow and understand the fundamental concepts of Infrastructure as Code.

Prerequisites: What You Need Before Starting

Before we dive into installation, let's make sure you have everything you need:

Required Tools

  • A supported operating system: Windows, macOS, or Linux
  • Command-line access: Terminal, Command Prompt, or PowerShell
  • Cloud provider account (optional but recommended): AWS, Azure, or Google Cloud
  • Text editor: VS Code, Sublime Text, or any code editor

Cloud Account Setup

For this tutorial, we'll use AWS as our cloud provider. If you don't have an AWS account, you can create a free tier account. Don't worry - we'll create resources that fall within the free tier limits.

Installing Terraform on Your System

Terraform is a single binary, making installation straightforward. Choose your operating system below for specific instructions:

Windows
macOS
Linux
Package Managers

Windows Installation

Method 1: Chocolatey (Recommended)

choco install terraform

Method 2: Manual Installation

  1. Download the Windows AMD64 package from terraform.io
  2. Unzip the package to a directory (e.g., C:\terraform)
  3. Add the directory to your system PATH

macOS Installation

Method 1: Homebrew (Recommended)

brew tap hashicorp/tap brew install hashicorp/tap/terraform

Method 2: Manual Installation

  1. Download the macOS AMD64 package from terraform.io
  2. Unzip the package
  3. Move the binary to /usr/local/bin

Linux Installation

Method 1: Using the package manager

# For Ubuntu/Debian 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 install terraform

Method 2: Manual Installation

  1. Download the Linux AMD64 package from terraform.io
  2. Unzip the package
  3. Move the binary to /usr/local/bin

Package Manager Options

Terraform is available through various package managers:

  • Windows: Chocolatey, Windows Package Manager
  • macOS: Homebrew, MacPorts
  • Linux: apt (Ubuntu/Debian), yum (CentOS/RHEL), dnf (Fedora)

Using package managers ensures you can easily update Terraform later.

Verifying Your Installation

After installation, verify that Terraform is working correctly:

terraform version

You should see output similar to:

Terraform v1.5.0 on linux_amd64

Installation Successful!

If you see the version information, congratulations! Terraform is correctly installed on your system. If you encounter any issues, double-check that the Terraform binary is in your system PATH.

Setting Up Your First Terraform Project

Now let's create your first Terraform project. Follow these steps:

1

Create Project Directory

Create a new directory for your Terraform project and navigate into it:

mkdir my-first-terraform-project cd my-first-terraform-project
2

Set Up Cloud Credentials

Configure your cloud provider credentials. For AWS, you can use the AWS CLI:

aws configure

Or set environment variables:

export AWS_ACCESS_KEY_ID="your-access-key" export AWS_SECRET_ACCESS_KEY="your-secret-key" export AWS_DEFAULT_REGION="us-east-1"
my-first-terraform-project/
main.tf
variables.tf (optional)
outputs.tf (optional)

Writing Your First Configuration

Create a file called main.tf in your project directory with the following content:

# Configure the AWS Provider
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

# Create a S3 bucket
resource "aws_s3_bucket" "my_first_bucket" {
  bucket = "my-unique-bucket-name-12345"  # Change this to a unique name

  tags = {
    Name        = "My First Bucket"
    Environment = "Dev"
    Project     = "Terraform Learning"
  }
}

# Output the bucket name
output "bucket_name" {
  value = aws_s3_bucket.my_first_bucket.bucket
}

Understanding the Configuration

  • Terraform Block: Specifies required providers and their versions
  • Provider Block: Configures the AWS provider with your region
  • Resource Block: Defines the S3 bucket to create
  • Output Block: Displays information after deployment

Important: Unique Bucket Name

S3 bucket names must be globally unique. Change my-unique-bucket-name-12345 to something unique, like adding your name or random numbers.

Running the Complete Terraform Workflow

Now let's execute the three core Terraform commands:

1

terraform init

Initialize your Terraform working directory:

terraform init

This command downloads the required provider plugins and sets up the backend.

2

terraform plan

Create an execution plan:

terraform plan

Terraform will show you what resources will be created. Review the plan carefully!

3

terraform apply

Apply the changes to create your infrastructure:

terraform apply

Terraform will ask for confirmation. Type yes to proceed. Watch as your S3 bucket is created!

Congratulations!

You've successfully created your first infrastructure with Terraform! You should see output showing your bucket name and a message confirming the creation.

Cleaning Up Resources

Since we're just practicing, let's clean up the resources we created to avoid unnecessary charges:

terraform destroy

Terraform will show you what resources will be destroyed and ask for confirmation. Type yes to proceed.

Why Destroy?

Running terraform destroy removes all resources managed by your Terraform configuration. This is great for:

  • Avoiding cloud costs for demo resources
  • Testing your cleanup process
  • Ensuring you can tear down environments completely

What's Next in Your Journey

You've taken your first major step in mastering Terraform! Here's what you've accomplished:

Installation Complete

You've successfully installed Terraform on your local machine.

First Configuration

You've written your first Terraform configuration file.

Full Workflow Executed

You've run the complete init → plan → apply → destroy workflow.

Real Infrastructure

You've created and destroyed actual cloud resources.

In the next part of our series, we'll dive deeper into the Terraform Configuration Language (HCL), where you'll learn about variables, data sources, and more complex resource configurations.

Master HCL Syntax

Learn about resources, variables, outputs, and data sources in our next tutorial.

Practice with Examples

Try creating different AWS resources like EC2 instances and security groups.

Explore State Management

Understand how Terraform tracks your infrastructure state.

Learn Best Practices

Discover how to structure projects and organize your code.

Key Takeaways

  • Terraform installation is straightforward across all major operating systems
  • The core workflow is initplanapplydestroy
  • Always run terraform plan before apply to review changes
  • Clean up resources with terraform destroy when you're done experimenting
  • Terraform configurations use the HashiCorp Configuration Language (HCL)

No comments:

Post a Comment

Terraform State Deep Dive: Why it's Crucial and How to Manage It

Terraform State Deep Dive: Why it's Crucial and How to Manage It ...