Skip to main content

AWS SAM Testing

CI Test Build PyPI version Python 3.13+

A Python library that provides testing and mocking utilities for AWS SAM (Serverless Application Model) applications. This library builds abstractions around AWS SAM CLI functionality to facilitate local testing of SAM applications with proper AWS resource mocking and isolation.

Features

  • Local API Gateway Testing: Run SAM APIs locally for integration testing
  • CloudFormation Template Processing: Advanced template manipulation and validation
  • AWS Resource Mocking: Seamless integration with moto for AWS service mocking
  • Pytest Integration: Built-in pytest fixtures for AWS contexts and resources
  • Dependency Management: Automatic CloudFormation resource dependency tracking
  • SAM Build Automation: Programmatic SAM build operations

Installation

pip install aws-sam-testing

Requirements

  • Python 3.13+
  • AWS SAM CLI (>=1.139.0)
  • AWS SAM Translator (>=1.97.0)

Quick Start

Basic Usage

from aws_sam_testing import AWSSAMToolkit

# Initialize the toolkit
toolkit = AWSSAMToolkit()

# Run a local API for testing
with toolkit.local_api() as api:
    # api.url contains the local API endpoint
    response = requests.get(f"{api.url}/hello")
    assert response.status_code == 200

CloudFormation Template Processing

from aws_sam_testing.cfn import CloudFormationTemplateProcessor

# Load and manipulate CloudFormation templates
processor = CloudFormationTemplateProcessor("template.yaml")
processor.remove_resource("MyResource")
processor.save()

Pytest Integration

The library provides several pytest fixtures that automatically handle AWS resource mocking and Lambda context creation:

def test_lambda_handler(mock_aws_lambda_context, mock_aws_resources):
    from my_app.app import lambda_handler
    
    # Set environment variables for Lambda function
    with mock_aws_resources.set_environment(lambda_function_logical_name="MyFunction"):
        result = lambda_handler(
            {"httpMethod": "GET", "path": "/hello"},
            mock_aws_lambda_context
        )
        
        assert result["statusCode"] == 200
        
        # Access mocked AWS resources
        table = mock_aws_resources.get_resource("MyDynamoDBTable")
        items = table.scan()["Items"]
        assert len(items) == 1

Available Pytest Fixtures

The library automatically registers the following pytest fixtures:

  • mock_aws_lambda_context: Provides a mock AWS Lambda context object
  • mock_aws_resources: Manages mocked AWS resources based on your CloudFormation template
  • aws_context: General AWS context management

Architecture

Core Components

  • AWSSAMToolkit: Main interface for SAM operations and local API management
  • CloudFormationTemplateProcessor: Advanced CloudFormation template manipulation with support for intrinsic functions (!Ref, !GetAtt, !Sub, etc.)
  • AWSResourceManager: Manages AWS resource lifecycle and mocking
  • LocalApi: Context manager for local API Gateway instances

Design Patterns

  • Context managers for resource lifecycle management
  • Direct integration with SAM CLI internals
  • Recursive dependency resolution for CloudFormation resources
  • Automatic cleanup and isolation

Examples

Testing with DynamoDB and SQS

def test_api_with_resources(mock_aws_lambda_context, mock_aws_resources):
    from api_handler.app import lambda_handler
    
    with mock_aws_resources.set_environment(lambda_function_logical_name="ApiHandler"):
        response = lambda_handler(
            {
                "path": "/items",
                "httpMethod": "POST",
                "requestContext": {
                    "resourcePath": "/items",
                    "httpMethod": "POST",
                },
            },
            mock_aws_lambda_context,
        )
        
        assert response["statusCode"] == 200
        
        # Verify SQS message was sent
        queue = mock_aws_resources.get_resource("MySQSQueue")
        messages = queue.receive_messages()
        assert len(messages) == 1
        
        # Verify DynamoDB item was created
        table = mock_aws_resources.get_resource("MyDynamoDBTable")
        items = table.scan()["Items"]
        assert len(items) == 1

Running Local API Tests

import requests
from aws_sam_testing import AWSSAMToolkit

def test_local_api():
    toolkit = AWSSAMToolkit()
    
    with toolkit.local_api(port=3000) as api:
        # Test GET endpoint
        response = requests.get(f"{api.url}/hello")
        assert response.status_code == 200
        assert response.json()["message"] == "Hello, World!"
        
        # Test POST endpoint
        response = requests.post(f"{api.url}/items")
        assert response.status_code == 200

Configuration

The library uses your CloudFormation template (typically template.yaml) to understand your AWS resources and automatically configure mocking. No additional configuration is required in most cases.

Template Structure

Your SAM template should follow standard CloudFormation/SAM syntax:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.13
      Environment:
        Variables:
          TABLE_NAME: !Ref MyDynamoDBTable
          QUEUE_NAME: !Ref MySQSQueue
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

  MyDynamoDBTable:
    Type: AWS::DynamoDB::Table
    Properties:
      AttributeDefinitions:
        - AttributeName: id
          AttributeType: S
      KeySchema:
        - AttributeName: id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST

  MySQSQueue:
    Type: AWS::SQS::Queue

Testing Best Practices

  1. Use Fixtures: Leverage the provided pytest fixtures for consistent test setup
  2. Environment Variables: Use set_environment() to properly configure Lambda environment variables
  3. Resource Access: Access mocked AWS resources through mock_aws_resources.get_resource()
  4. Isolation: Each test runs with isolated AWS resources
  5. Cleanup: Resources are automatically cleaned up after each test

Contributing

  1. Clone the repository
  2. Install dependencies: make init
  3. Run tests: make test
  4. Format code: make format
  5. Type check: make pyright

License

This project is licensed under the MIT License.

Metadata

Release files for aws-sam-testing 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-sam-testing 0.1.2
File Size Uploaded Provenance Actions
aws_sam_testing-0.1.2.tar.gz 178.9 kB Without provenance Details for aws_sam_testing-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for aws-sam-testing 0.1.2
File Interpreter ABI Platform Actions
aws_sam_testing-0.1.2-py3-none-any.whl Python 3 none any Details for aws_sam_testing-0.1.2-py3-none-any.whl

Total release size: 223.2 kB

Release files / aws_sam_testing-0.1.2.tar.gz

Download URL aws_sam_testing-0.1.2.tar.gz
Size 178.9 kB
Tags Source
SHA-256 checksum
How to use checksums
fe19a681d5a59fe36aa07fad642e170b2bea78b95c945c7dbc0faa9b74e1e662
BLAKE2b-256 checksum
How to use checksums
9c376bf4176c66e0a69b1149b2e9d24e75083d2ea8585774b46dd6989d38b587
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.14

Release files / aws_sam_testing-0.1.2-py3-none-any.whl

Download URL aws_sam_testing-0.1.2-py3-none-any.whl
Size 44.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
5cc7e98d94a8333883d11cfa4a0d76c95a4cbff60663217d2649e76d49b40012
BLAKE2b-256 checksum
How to use checksums
b144de1b5b68ac8b8f871751be427b802ef236f1439fc9c2ecd89184e61e9d3d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.14

Security for aws-sam-testing 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page