LocalStack CLI (Python Package)
LocalStack is a cloud service emulator that runs entirely on your local machine. It provides a fully functional local cloud environment for developing, testing, and debugging AWS applications without deploying to a remote cloud. This specific Python package primarily provides the `localstack` command-line interface (CLI) for managing the LocalStack container. The current version is 2026.3.0, and it follows a calendar-based release cadence, often releasing monthly or more frequently.
Warnings
- gotcha The `localstack` Python package primarily provides the CLI. For interacting with AWS services running on LocalStack, you should use `boto3` (or other AWS SDKs) and configure the `endpoint_url` to point to your LocalStack instance (e.g., `http://localhost:4566`).
- breaking LocalStack's versioning scheme changed from semantic versioning (e.g., `1.x.y`) to calendar-based versioning (e.g., `YYYY.M.D`). Be mindful of this when checking for compatibility or specific feature availability.
- deprecated Several configuration environment variables have been renamed or deprecated over time. A notable example is `LOCALSTACK_HOST`, which was deprecated in favor of `LS_HOST`.
- gotcha Persistence is not enabled by default. If LocalStack restarts without persistence configured, all your data (e.g., S3 buckets, DynamoDB tables) will be lost.
- gotcha LocalStack requires Docker to be running on your system. It relies heavily on Docker containers for providing its emulated services. Without Docker, `localstack start` will fail.
Install
-
pip install localstack
Imports
- main
from localstack.main import main
- boto3.client
import boto3; client = boto3.client('s3', endpoint_url='http://localhost:4566')
Quickstart
import subprocess
import time
import os
import boto3
def start_localstack():
print('Starting LocalStack in the background...')
# Use subprocess to run 'localstack start' non-blocking
# Note: Requires 'localstack' CLI to be in PATH
process = subprocess.Popen(['localstack', 'start'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Give LocalStack some time to start up
time.sleep(15)
# Check if LocalStack is running via 'localstack status'
status_output = subprocess.run(['localstack', 'status'], capture_output=True, text=True)
if 'running' in status_output.stdout:
print('LocalStack is running.')
else:
print('LocalStack failed to start or is not running correctly.')
print('STDOUT:', status_output.stdout)
print('STDERR:', status_output.stderr)
# Optionally, terminate the Popen process if it didn't start correctly
# process.terminate()
# return None
return process
def stop_localstack(process):
print('Stopping LocalStack...')
subprocess.run(['localstack', 'stop'])
if process: # Terminate the Popen process if it was started this way
process.terminate()
process.wait()
print('LocalStack stopped.')
def main():
localstack_process = None
try:
localstack_process = start_localstack()
if not localstack_process:
return
# Now, interact with a service (e.g., S3) using boto3
print('\nInteracting with S3 on LocalStack...')
s3_client = boto3.client(
's3',
region_name='us-east-1',
endpoint_url='http://localhost:4566',
aws_access_key_id='test',
aws_secret_access_key='test'
)
bucket_name = 'my-local-test-bucket'
try:
s3_client.create_bucket(Bucket=bucket_name)
print(f"Bucket '{bucket_name}' created successfully.")
response = s3_client.list_buckets()
print('Existing buckets:')
for bucket in response['Buckets']:
print(f" - {bucket['Name']}")
except Exception as e:
print(f"Error interacting with S3: {e}")
finally:
stop_localstack(localstack_process)
if __name__ == '__main__':
# Ensure Docker is running before executing this script
# On first run, 'localstack start' will download necessary Docker images.
main()