Help me, Localstack. You're my only hope.
Source: Dev.to
Introduction
Last year I learned Terraform with a group on Discord. The instructor provided a solid introduction, covering how to set up an AWS EC2 instance inside a specific VPC and subnet using the latest Amazon Linux 2023 AMI, and storing the Terraform state in an S3 bucket. This gave me a strong foundation, but without regular practice the knowledge fades.
Because my daily job doesn’t involve AWS, I need a safe way to keep practicing. Using a real AWS account risks unexpected charges, even with the free tier.
LocalStack emulates AWS services locally, allowing you to practice Terraform without incurring costs.
Install LocalStack
-
Follow the official installation guide for your operating system.
-
Start LocalStack:
localstack start -
In another terminal, verify it’s running:
curl http://localhost:4566/_localstack/health
A JSON response listing available services indicates that LocalStack is working correctly.
Configure an AWS CLI Profile for LocalStack
Even though LocalStack doesn’t validate credentials, you still need an AWS profile.
Create a profile named localstack:
aws configure --profile localstack
Use these dummy values:
- AWS Access Key ID:
test - AWS Secret Access Key:
test - Default region name:
us-east-1 - Default output format:
json
Edit ~/.aws/config and add the endpoint URL:
[profile localstack]
region = us-east-1
output = json
endpoint_url = http://localhost:4566
Note: Ensure you are using AWS CLI v2.13.0 or later, as older versions may ignore
endpoint_urlin the config file.
Check the version:
aws --version
You should see something like aws-cli/2.13.0 Python/3.x. Upgrade if necessary.
Terraform Setup
Create the following two files in a new directory.
terraform.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.92"
}
}
required_version = ">= 1.2"
}
main.tf
provider "aws" {
profile = "localstack"
region = "us-east-1"
s3_use_path_style = true
skip_credentials_validation = true
skip_metadata_api_check = true
endpoints {
s3 = "http://s3.localhost.localstack.cloud:4566"
}
default_tags {
tags = {
Environment = "tutorial"
Project = "localstack-terraform"
}
}
}
resource "aws_s3_bucket" "example" {
bucket_prefix = "localstack-terraform"
}
Explanation of main.tf
| Block | Purpose |
|---|---|
provider "aws" | Declares the AWS provider for Terraform. |
profile = "localstack" | Uses the AWS CLI profile configured earlier. |
region = "us-east-1" | Required by LocalStack even though it runs locally. |
s3_use_path_style = true | Forces path‑style S3 URLs (required by LocalStack). |
skip_credentials_validation = true | Skips credential checks because LocalStack doesn’t validate them. |
skip_metadata_api_check = true | Disables calls to the EC2 metadata service. |
endpoints { s3 = "http://s3.localhost.localstack.cloud:4566" } | Directs all S3 requests to LocalStack. |
default_tags | Automatically adds the specified tags to every resource created by this provider. |
resource "aws_s3_bucket" "example" | Creates an S3 bucket with a unique name prefixed by localstack-terraform. |
Apply the Configuration
Initialize the working directory and apply the configuration:
terraform init
terraform apply
Terraform will create the S3 bucket inside LocalStack. This demonstrates how you can test AWS resources locally without a real AWS account.
Reducing Boilerplate with tflocal
Manually configuring service endpoints, provider flags, and other LocalStack‑specific settings can become cumbersome.
The tflocal wrapper simplifies this process by automatically handling endpoints and reducing the amount of configuration required in your Terraform code. In larger projects, using tflocal leads to cleaner and more maintainable Terraform files when working with LocalStack.