LocalStack

LocalStack is a cloud service emulator that runs in a single container on your laptop or in your CI environment.

localstack-cli Brew Installation

1
brew install localstack/tap/localstack-cli

Running LocalStack using CLI

You can run LocalStack using LocalStack CLI or Docker Compose

  • LocalStack CLI
  • Docker Compose

start LocalStack using CLI

1
localstack start -d

Query the status of services.

1
localstack status services

stop LocalStack

1
localstack stop

Running LocalStack using Docker Compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
version: "3.8"

services:
localstack:
container_name: "${LOCALSTACK_DOCKER_NAME:-localstack-main}"
image: localstack/localstack
ports:
- "127.0.0.1:4566:4566" # LocalStack Gateway
- "127.0.0.1:4510-4559:4510-4559" # external services port range
environment:
# LocalStack configuration: https://docs.localstack.cloud/references/configuration/
- DEBUG=${DEBUG:-0}
volumes:
- "${LOCALSTACK_VOLUME_DIR:-./volume}:/var/lib/localstack"
- "/var/run/docker.sock:/var/run/docker.sock"

How to connect with AWS CLI and SDKs

Manual configuration: Manually configure the SDK to connect to LocalStack services by setting the endpoint URL to http://localhost:4566 or http://localhost.localstack.cloud:4566. This can also be specified using a profile or an environment variable.

Configuring a custom profile
~/.aws/config

1
2
3
4
[profile localstack]
region=us-east-1
output=json
endpoint_url = http://localhost:4566

~/.aws/credentials

1
2
3
[localstack]
aws_access_key_id = test
aws_secret_access_key = test

create a queue in LocalStack using AWS CLI

1
aws sqs create-queue --queue-name MyQueue --profile localstack

Get queue url

1
aws sqs get-queue-url --queue-name MyQueue --profile localstack

Delete queue

1
aws sqs delete-queue --queue-url http://sqs.us-east-1.localhost.localstack.cloud:4566/000000000000/MyQueue --profile localstack

List queues

1
aws sqs list-queues --profile localstack

List S3 bucket

1
aws s3 ls --profile localstack

Connect to LocalStack using Java SDK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Credentials that can be replaced with real AWS values. (To be handled properly and not hardcoded.)
// These can be skipped altogether for LocalStack, but we generally want to avoid discrepancies with production code.
final String ACCESS_KEY = "test";
final String SECRET_KEY = "test";

// Desired region.
Region region = Region.US_EAST_1;

// S3 Client with configured credentials, endpoint directing to LocalStack and desired region.
S3Client s3Client = S3Client.builder()
.endpointOverride(URI.create("http://s3.localhost.localstack.cloud:4566"))
.credentialsProvider(StaticCredentialsProvider.create(
AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY)))
.region(region)
.build();

Reference