Skip to main content
Simulate AWS Services Locally with LocalStack and Terraform

Simulate AWS Services Locally with LocalStack and Terraform

Key Takeaway

Use LocalStack with Terraform to test AWS infrastructure locally. Create S3 buckets, DynamoDB tables, and Lambda functions without AWS costs or credentials.

Table of Contents

Quick Answer

Install LocalStack via Docker, point the AWS provider endpoints to http://localhost:4566, and run terraform apply. Your Terraform code works identically — but everything runs locally with zero AWS costs.

What is LocalStack?

LocalStack emulates AWS services on your laptop. It supports 70+ services including S3, DynamoDB, Lambda, SQS, SNS, IAM, and API Gateway. Free tier covers most core services; Pro adds advanced services like ECS, EKS, and RDS.

Setup

1. Install LocalStack

# Docker Compose (recommended)
cat > docker-compose.yml << 'EOF'
version: "3.8"
services:
  localstack:
    image: localstack/localstack:latest
    ports:
      - "4566:4566"
    environment:
      - SERVICES=s3,dynamodb,lambda,sqs,sns,iam
      - DEBUG=0
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
EOF

docker-compose up -d
# Or via pip
pip install localstack
localstack start

2. Configure Terraform Provider

provider "aws" {
  region                      = "us-east-1"
  access_key                  = "test"
  secret_key                  = "test"
  skip_credentials_validation = true
  skip_metadata_api_check     = true
  skip_requesting_account_id  = true

  endpoints {
    s3       = "http://localhost:4566"
    dynamodb = "http://localhost:4566"
    lambda   = "http://localhost:4566"
    sqs      = "http://localhost:4566"
    sns      = "http://localhost:4566"
    iam      = "http://localhost:4566"
  }
}

3. Use the Same Terraform Code

resource "aws_s3_bucket" "data" {
  bucket = "my-test-bucket"
}

resource "aws_dynamodb_table" "sessions" {
  name         = "sessions"
  billing_mode = "PAY_PER_REQUEST"
  hash_key     = "SessionID"

  attribute {
    name = "SessionID"
    type = "S"
  }
}

resource "aws_sqs_queue" "tasks" {
  name                       = "task-queue"
  visibility_timeout_seconds = 30
  message_retention_seconds  = 86400
}
terraform init
terraform apply  # Creates everything locally — no AWS account needed

Switch Between Local and AWS

Use Terraform workspaces or variables to toggle:

variable "use_localstack" {
  type    = bool
  default = false
}

locals {
  endpoint = var.use_localstack ? "http://localhost:4566" : null
}

provider "aws" {
  region = "us-east-1"

  dynamic "endpoints" {
    for_each = var.use_localstack ? [1] : []
    content {
      s3       = local.endpoint
      dynamodb = local.endpoint
      lambda   = local.endpoint
    }
  }

  skip_credentials_validation = var.use_localstack
  skip_metadata_api_check     = var.use_localstack
  skip_requesting_account_id  = var.use_localstack
}
# Local testing
terraform apply -var="use_localstack=true"

# Deploy to real AWS
terraform apply -var="use_localstack=false"

Verify Resources with AWS CLI

# Point AWS CLI to LocalStack
export AWS_ENDPOINT_URL=http://localhost:4566
export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export AWS_DEFAULT_REGION=us-east-1

aws s3 ls
aws dynamodb list-tables
aws sqs list-queues

Supported Services (Free Tier)

ServiceStatusNotes
S3✅ FullBuckets, objects, versioning
DynamoDB✅ FullTables, indexes, streams
SQS✅ FullStandard and FIFO queues
SNS✅ FullTopics, subscriptions
Lambda✅ FullFunctions, layers
IAM✅ PartialUsers, roles, policies
CloudFormation✅ PartialStack operations
API Gateway✅ PartialREST APIs

Use Cases

  • Development — Test Terraform changes without AWS costs
  • CI/CD — Run integration tests in pipelines
  • Learning — Experiment with AWS services safely
  • Offline work — No internet needed after initial Docker pull

Limitations

  • Not all AWS services are emulated
  • Some service behaviors differ from real AWS
  • Performance characteristics don’t match production
  • Don’t use for load testing or performance benchmarking

Conclusion

LocalStack + Terraform is the fastest way to develop and test AWS infrastructure locally. Point provider endpoints to localhost:4566, use fake credentials, and run the same Terraform code you’d use in production. Zero cost, instant feedback, no AWS account needed for development.

🚀

Level Up Your Terraform Skills

Hands-on courses, books, and resources from Luca Berton

Luca Berton
Written by

Luca Berton

DevOps Engineer, AWS Partner, Terraform expert, and author. Creator of Ansible Pilot, Terraform Pilot, and CopyPasteLearn.