Overview
Terraform is an Infrastructure as Code (IaC) tool by HashiCorp that allows you to define, provision, and manage infrastructure across cloud providers using declarative configuration files.
Why Terraform¶
- Ideal for creating and managing infrastructure.
- Uses HCL (HashiCorp Configuration Language) — a human-readable, declarative language.
Example¶
How Terraform Communicates with Cloud Providers¶
-
Providers (Plugins) Terraform uses provider plugins to communicate with clouds.
-
hashicorp/awsfor AWS hashicorp/azurermfor Azure-
hashicorp/googlefor GCP -
APIs Each provider calls the official cloud API (REST or SDK) to create resources.
-
Authentication Terraform needs credentials to access APIs:
provider "aws" {
access_key = "YOUR_ACCESS_KEY"
secret_key = "YOUR_SECRET_KEY"
region = "us-east-1"
}
-
Terraform Workflow
-
init→ Download providers plan→ Preview changesapply→ Create or modify resourcesstate→ Save results locally or remotely
Example Flow¶
Terraform uses the AWS provider to authenticate, send an API call, and record the result in terraform.tfstate.
Summary: Terraform communicates through provider plugins that use official cloud APIs to create and manage resources.
AWS CLI Installation & Configuration¶
1. Install AWS CLI¶
macOS
Windows
Manual installers are available at: https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html
2. Configure AWS CLI¶
Enter:
AWS Access Key ID [None]: test
AWS Secret Access Key [None]: test
Default region name [None]: us-east-1
Default output format [None]: json
Configuration files:
Windows path:
3. Test Configuration¶
With LocalStack:
4. Optional: Custom Profiles¶
aws configure --profile localstack
aws --profile localstack --endpoint-url=http://localhost:4566 s3 ls
LocalStack Setup¶
LocalStack emulates AWS services locally using Docker — perfect for testing Terraform and AWS CLI without real AWS credentials.
Why Use LocalStack¶
- Avoid AWS costs
- Work offline
- Fast and safe testing
- Supports S3, EC2, Lambda, DynamoDB, and more
1. Prerequisites¶
| Tool | Purpose | macOS Install Command |
|---|---|---|
| Docker Desktop | Runs containers | brew install --cask docker |
| Python 3.8+ | Required for CLI | Preinstalled |
| pip | Python package manager | python3 -m ensurepip --upgrade |
2. Install LocalStack¶
3. Start LocalStack¶
Runs on http://localhost:4566.
4. Web Dashboard¶
Terminal

5. Test¶
6. Docker Compose Example¶
version: "3.8"
services:
localstack:
image: localstack/localstack
ports:
- "4566:4566"
environment:
- SERVICES=s3,ec2,lambda
- DEBUG=1
volumes:
- "./localstack:/var/lib/localstack"
Run:
Terraform + LocalStack Integration¶
Project Structure¶
Step 1: Create a Test File¶
Step 2: provider.tf¶
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
required_version = ">= 1.5.0"
}
provider "aws" {
region = "us-east-1"
access_key = "test"
secret_key = "test"
s3_force_path_style = true
skip_credentials_validation = true
skip_metadata_api_check = true
skip_requesting_account_id = true
endpoints {
s3 = "http://localhost:4566"
}
}
Step 3: main.tf¶
resource "aws_s3_bucket" "demo" {
bucket = "terraform-localstack-demo"
}
resource "aws_s3_object" "file_upload" {
bucket = aws_s3_bucket.demo.id
key = "hello.txt"
source = "hello.txt"
etag = filemd5("hello.txt")
}
Step 4: outputs.tf¶
Step 5: Deploy¶
Verify:
Cleanup and Destroy¶
Destroy Resources¶
Output: