Introduction:
Infrastructure as Code (IaC) is transforming how developers and DevOps teams manage cloud resources. HashiCorp Terraform is one of the most popular IaC tools, allowing you to define, provision, and manage infrastructure with simple configuration files. In this article, we’ll walk through how to run Terraform on your local system and deploy your first resource.
Step 1: Install Terraform
Terraform is distributed as a single binary. You can download it from the official HashiCorp website or install via package managers like brew (macOS) or apt (Linux). Once installed, verify using terraform -version
.
and add it to your PATH.
macOS (Homebrew):
brew install hashicorp/tap/terraform
Linux (Debian/Ubuntu):
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install terraform
Check installation:
terraform -version
Step 2: Create a Terraform Project
Start by creating a new directory and a main.tf
file. In this file, define your cloud provider and resources. For example, an AWS S3 bucket.
region = "us-east-1"
}
resource "aws_s3_bucket" "demo" {
bucket = "my-terraform-demo-bucket-1234"
acl = "private"
}
Step 3: Initialize Terraform
Run terraform init
to download provider plugins.
Step 4: Validate and Preview
Before applying changes, run:
terraform plan
This ensures your configuration is correct and previews what Terraform will create.
Step 5: Apply Changes
Execute terraform apply
, confirm with yes, and watch Terraform provision your infrastructure.
Step 6: Clean Up
When you’re done, remove everything with:
terraform destroy
0 Comments