DEV Community

Manthan Ankolekar
Manthan Ankolekar

Posted on

Terraform Setup Guide: Getting Started with Infrastructure as Code

Terraform, developed by HashiCorp, is one of the most widely used tools for Infrastructure as Code (IaC). It allows you to define, provision, and manage cloud resources across providers like AWS, Azure, GCP, and many others using simple, declarative configuration files.

If you’re just getting started, this guide will walk you through the setup process of Terraform, from installation to writing your first configuration file.


🔧 Prerequisites

Before diving in, make sure you have:

  • A working terminal (Linux, macOS, or Windows with WSL/PowerShell).
  • A cloud provider account (AWS, Azure, or GCP).
  • Basic understanding of command line operations.

📥 Step 1: Install Terraform

On macOS (using Homebrew)

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
Enter fullscreen mode Exit fullscreen mode

On Linux (Debian/Ubuntu)

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
Enter fullscreen mode Exit fullscreen mode

On Windows

  1. Download the Terraform binary from Terraform Downloads.
  2. Extract the .zip file.
  3. Add the Terraform binary path to your System Environment Variables.

✅ Verify installation:

terraform -v
Enter fullscreen mode Exit fullscreen mode

📂 Step 2: Configure Cloud Provider Access

Terraform uses provider plugins (like AWS, Azure, GCP). You’ll need credentials to let Terraform interact with your cloud.

AWS Example

Install AWS CLI and configure:

aws configure
Enter fullscreen mode Exit fullscreen mode

Provide:

  • AWS Access Key ID
  • AWS Secret Access Key
  • Default region

Terraform will automatically use these credentials.


📝 Step 3: Create Your First Terraform Project

  1. Create a new project folder:
mkdir terraform-setup && cd terraform-setup
Enter fullscreen mode Exit fullscreen mode
  1. Create a configuration file main.tf:
provider "aws" {
  region = "us-east-1"
}

resource "aws_s3_bucket" "example" {
  bucket = "my-terraform-setup-bucket"
  acl    = "private"
}
Enter fullscreen mode Exit fullscreen mode

This configuration:

  • Defines AWS as the provider.
  • Creates a private S3 bucket named my-terraform-setup-bucket.

▶️ Step 4: Initialize Terraform

Run:

terraform init
Enter fullscreen mode Exit fullscreen mode

This downloads necessary plugins (e.g., AWS provider).


🔍 Step 5: Validate and Plan

Check if configuration is valid:

terraform validate
Enter fullscreen mode Exit fullscreen mode

Preview changes:

terraform plan
Enter fullscreen mode Exit fullscreen mode

🚀 Step 6: Apply Configuration

Deploy resources:

terraform apply
Enter fullscreen mode Exit fullscreen mode

Type yes when prompted. Terraform will create the S3 bucket.


🗑 Step 7: Destroy Resources

To clean up:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

Confirm with yes.


🌟 Conclusion

You’ve successfully installed and set up Terraform, configured your first provider, and deployed an AWS S3 bucket. From here, you can scale to more complex infrastructures—VPCs, databases, load balancers, Kubernetes clusters, and beyond.

By mastering Terraform, you’ll gain full control over your infrastructure in a reproducible, version-controlled, and automated way.

Top comments (0)