Modern QA2026Testing with LocalStack — tiles
Log inJoin
46 / 75 · 08 Infrastructure as Code Testing · Serverless Function Testing← prev⊞ allnext →☰ Read as one page

8.5Testing with LocalStack

LocalStack provides a local AWS cloud stack for testing serverless functions with real AWS service emulations:

# tests/integration/test_lambda_localstack.py
import boto3
import json
import pytest

@pytest.fixture(scope="module")
def aws_clients():
    """Create AWS clients pointing to LocalStack."""
    endpoint = "http://localhost:4566"
    return {
        "lambda": boto3.client("lambda", endpoint_url=endpoint,
                               region_name="us-east-1",
                               aws_access_key_id="test",
                               aws_secret_access_key="test"),
        "sqs": boto3.client("sqs", endpoint_url=endpoint,
                            region_name="us-east-1",
                            aws_access_key_id="test",
                            aws_secret_access_key="test"),
        "dynamodb": boto3.client("dynamodb", endpoint_url=endpoint,
                                 region_name="us-east-1",
                                 aws_access_key_id="test",
                                 aws_secret_access_key="test"),
    }

def test_lambda_processes_sqs_message(aws_clients):
    """Test the full flow: SQS message triggers Lambda, result in DynamoDB."""
    sqs = aws_clients["sqs"]
    dynamodb = aws_clients["dynamodb"]

    # Send message to SQS
    queue_url = sqs.create_queue(QueueName="orders")["QueueUrl"]
    sqs.send_message(
        QueueUrl=queue_url,
        MessageBody=json.dumps({"orderId": "ORD-TEST", "amount": 42.00})
    )

    # Wait for Lambda to process (triggered by SQS event source mapping)
    import time
    time.sleep(5)

    # Verify the result in DynamoDB
    result = dynamodb.get_item(
        TableName="orders",
        Key={"orderId": {"S": "ORD-TEST"}}
    )
    assert "Item" in result
    assert result["Item"]["status"]["S"] == "processed"

The combination of unit tests (fast, isolated), local emulation (SAM Local, Functions Framework), and integration tests (LocalStack, Testcontainers) provides comprehensive coverage for serverless functions without requiring a deployed cloud environment for every test run.