LocalStack (MiniStack)#

AWS service emulator fixtures. localstack_service runs MiniStack by default and accepts a LocalStack image as an opt-in. The fixture exposes the AWS gateway on port 4566 and runs AWS commands inside the service container. No Python AWS SDK or host-side AWS CLI is required.

Installation#

AWS service emulator support is included in the base package:

pip install pytest-databases

Image and Authentication#

The default image is ministackorg/ministack:latest, an actively maintained, MIT-licensed drop-in emulator that requires zero auth tokens and runs offline without cloud account credentials.

If you require LocalStack commercial features or enterprise extensions, you can opt into a custom LocalStack image by setting LOCALSTACK_IMAGE (and optionally LOCALSTACK_AUTH_TOKEN):

export LOCALSTACK_IMAGE=localstack/localstack:latest
export LOCALSTACK_AUTH_TOKEN=your-token

The container provides awslocal or aws and health check endpoints. The fixture uses those in-container tools for readiness and AWS operations without mounting a container-engine socket.

Configuration#

  • LOCALSTACK_IMAGE: Image override (default: ministackorg/ministack:latest).

  • LOCALSTACK_AUTH_TOKEN: Optional token for commercial LocalStack images.

  • LOCALSTACK_REGION: AWS region (default: us-east-1).

  • LOCALSTACK_ACCESS_KEY: Test access key (default: test).

  • LOCALSTACK_SECRET_KEY: Test secret key (default: test).

  • LOCALSTACK_SERVICES: Optional comma-separated service allowlist.

  • LOCALSTACK_RESOURCE_PREFIX: Resource-name prefix override. By default, each xdist worker receives a distinct prefix.

Each setting is also a session fixture of the same lowercase name (for example localstack_region); overriding the fixture in conftest.py changes the container that localstack_service starts.

Usage Example#

exec_aws_cli() executes in the emulator container. This example creates, sends to, receives from, and deletes an SQS queue without importing an AWS client library:

import json

from pytest_databases.docker.localstack import LocalStackService

pytest_plugins = ["pytest_databases.docker.localstack"]


def test_sqs(localstack_service: LocalStackService) -> None:
    queue_name = f"{localstack_service.resource_prefix}messages"
    created = json.loads(
        localstack_service.exec_aws_cli(
            "sqs", "create-queue", "--queue-name", queue_name, "--output", "json"
        )
    )
    queue_url = created["QueueUrl"]
    localstack_service.exec_aws_cli(
        "sqs", "send-message", "--queue-url", queue_url, "--message-body", "hello"
    )
    received = json.loads(
        localstack_service.exec_aws_cli(
            "sqs", "receive-message", "--queue-url", queue_url, "--output", "json"
        )
    )
    message = received["Messages"][0]
    assert message["Body"] == "hello"
    localstack_service.exec_aws_cli(
        "sqs",
        "delete-message",
        "--queue-url",
        queue_url,
        "--receipt-handle",
        message["ReceiptHandle"],
    )
    localstack_service.exec_aws_cli("sqs", "delete-queue", "--queue-url", queue_url)

Available Fixtures#

  • localstack_image

  • localstack_auth_token

  • localstack_region

  • localstack_access_key

  • localstack_secret_key

  • localstack_services

  • localstack_resource_prefix

  • xdist_localstack_isolation_level

  • localstack_service

The default xdist isolation level is database: workers share a server and use unique resource prefixes. Override to server to start one transient container per worker.

Service API#

AWS service emulator fixtures (MiniStack by default, LocalStack opt-in) and in-container CLI helpers.

class pytest_databases.docker.localstack.LocalStackService(container: docker.models.containers.Container, host: str, port: int, endpoint_url: str, region: str, access_key: str, secret_key: str, services: tuple[str, ...] | None, resource_prefix: str)[source]#

Bases: ServiceContainer

Metadata and CLI helper for a running AWS emulator container (MiniStack or LocalStack).

endpoint_url: str#
region: str#
access_key: str#
secret_key: str#
services: tuple[str, ...] | None#
resource_prefix: str#
exec_aws_cli(*arguments: str) → str[source]#

Run an AWS command inside the emulator container without a host-side AWS client.

Returns:

Decoded stdout/stderr from the AWS CLI command.

Raises:

RuntimeError – If the AWS CLI command exits with a non-zero status.

__init__(container: docker.models.containers.Container, host: str, port: int, endpoint_url: str, region: str, access_key: str, secret_key: str, services: tuple[str, ...] | None, resource_prefix: str) → None#
pytest_databases.docker.localstack.localstack_access_key() → str[source]#

Provide the test AWS access key ID for the emulator.

pytest_databases.docker.localstack.localstack_auth_token() → str | None[source]#

Provide an optional auth token for commercial LocalStack image opt-in.

pytest_databases.docker.localstack.localstack_image() → str[source]#

Provide the MiniStack or LocalStack container image.

pytest_databases.docker.localstack.localstack_region() → str[source]#

Provide the default AWS region for the emulator.

pytest_databases.docker.localstack.localstack_resource_prefix() → str[source]#

Provide a worker-scoped resource name prefix for emulator tests.

pytest_databases.docker.localstack.localstack_secret_key() → str[source]#

Provide the test AWS secret access key for the emulator.

pytest_databases.docker.localstack.localstack_service(container_service: ContainerService, localstack_image: str, localstack_region: str, localstack_access_key: str, localstack_secret_key: str, localstack_auth_token: str | None, localstack_services: tuple[str, ...] | None, localstack_resource_prefix: str, xdist_localstack_isolation_level: XdistIsolationLevel) → Generator[LocalStackService, None, None][source]#

Start and yield a ready LocalStack service container.

Yields:

A configured LocalStackService instance.

pytest_databases.docker.localstack.localstack_services() → tuple[str, ...] | None[source]#

Provide an optional allowlist of AWS service names to enable.

pytest_databases.docker.localstack.provide_localstack_service(container_service: ContainerService, localstack_image: str, localstack_region: str, localstack_access_key: str, localstack_secret_key: str, localstack_auth_token: str | None, localstack_services: tuple[str, ...] | None, localstack_resource_prefix: str, xdist_localstack_isolation_level: XdistIsolationLevel) → Generator[LocalStackService, None, None][source]#

Start and yield a ready MiniStack or LocalStack service container.

Yields:

A configured LocalStackService instance.

pytest_databases.docker.localstack.xdist_localstack_isolation_level() → Literal['database', 'server'][source]#

Provide the pytest-xdist isolation level for MiniStack and LocalStack.