LocalStack Python Client
The localstack-client is a lightweight Python client designed to facilitate interaction with LocalStack, a cloud service emulator. It provides a thin wrapper around the popular boto3 library, automatically configuring endpoint URLs to point to your local LocalStack instance. The library is actively maintained with frequent releases; version 2.11 was released in January 2026.
Warnings
- breaking The environment variables `LOCALSTACK_HOST` and `USE_SSL` are deprecated for configuring the LocalStack endpoint. They have been superseded by `AWS_ENDPOINT_URL`.
- gotcha The `localstack-client` library acts as a wrapper for `boto3` to connect to LocalStack. It requires an *active and running* LocalStack instance to function. Without it, operations will fail.
- gotcha When running applications (e.g., AWS Lambda functions, Docker containers) that need to connect to LocalStack, direct usage of `localhost` or `127.0.0.1` from within those containers might not work due to networking isolation. LocalStack itself only binds to IPv4 addresses.
- gotcha The `localstack-client` is a thin wrapper. Compatibility with specific AWS service behaviors relies heavily on the underlying LocalStack version. New or changed AWS API features might not be supported if your LocalStack instance is outdated, or conversely, older LocalStack versions might have compatibility issues with newer AWS SDK (boto3) features.
- gotcha LocalStack (the emulator) has undergone significant breaking changes in versions 3.0 and 4.0, including changes to configuration variables (e.g., removal of `AUTOSTART_UTIL_CONTAINERS`, changes to CloudPods client) and authentication methods (`localstack auth login` deprecated). These changes in the core LocalStack platform can indirectly affect how you set up your environment for the Python client.
Install
-
pip install localstack-client
Imports
- Session
from localstack_client.session import Session
Quickstart
import os
from localstack_client.session import Session
# Ensure LocalStack is running before executing this code.
# If running LocalStack in Docker, make sure port 4566 is mapped.
# You can optionally set AWS_ENDPOINT_URL, but localstack-client defaults to localhost:4566
# os.environ['AWS_ENDPOINT_URL'] = 'http://localhost:4566'
session = Session()
s3_client = session.client('s3')
try:
# Create a bucket (ensure it doesn't already exist)
bucket_name = 'my-local-bucket-123'
s3_client.create_bucket(Bucket=bucket_name)
print(f"Bucket '{bucket_name}' created successfully.")
# List buckets to verify
response = s3_client.list_buckets()
print("\nExisting buckets:")
for bucket in response['Buckets']:
print(f"- {bucket['Name']}")
# Upload a file
file_content = b'Hello from LocalStack!'
file_name = 'hello.txt'
s3_client.put_object(Bucket=bucket_name, Key=file_name, Body=file_content)
print(f"\nFile '{file_name}' uploaded to '{bucket_name}'.")
# Download the file
download_response = s3_client.get_object(Bucket=bucket_name, Key=file_name)
downloaded_content = download_response['Body'].read().decode('utf-8')
print(f"\nDownloaded content: '{downloaded_content}'")
except Exception as e:
print(f"An error occurred: {e}")