LocalStack Python Client

2.11 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a session with localstack-client and interact with a local S3 service. It creates a bucket, uploads a file, lists buckets, and downloads the file, all against a running LocalStack instance. Remember that a LocalStack instance must be running and accessible before executing this code.

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}")

view raw JSON →