Moto

5.1.22 · active · verified Sat Mar 28

Moto is a Python library that allows developers to easily mock AWS services for testing purposes. It intercepts Boto3 calls and routes them to an in-memory mock, simulating AWS API behavior locally without actual AWS interaction. The current version is 5.1.22, and it typically sees new releases every 1-2 weeks, ensuring continuous updates for AWS service support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to mock an S3 service using the `@mock_aws` decorator. It creates a Boto3 S3 client within the mocked context, creates a bucket, and then verifies its existence, all without interacting with actual AWS infrastructure. Dummy AWS credentials are set to prevent botocore from attempting real AWS calls.

import boto3
from moto import mock_aws
import os

@mock_aws
def test_s3_bucket_creation():
    # Ensure dummy AWS credentials are set for botocore to not attempt real calls
    os.environ['AWS_ACCESS_KEY_ID'] = 'testing'
    os.environ['AWS_SECRET_ACCESS_KEY'] = 'testing'
    os.environ['AWS_SECURITY_TOKEN'] = 'testing'
    os.environ['AWS_SESSION_TOKEN'] = 'testing'
    os.environ['AWS_DEFAULT_REGION'] = 'us-east-1'

    s3_client = boto3.client('s3', region_name='us-east-1')
    bucket_name = 'my-test-bucket-123'
    
    # Create a bucket in the mocked AWS environment
    s3_client.create_bucket(Bucket=bucket_name)
    
    # List buckets and verify the new bucket exists
    response = s3_client.list_buckets()
    buckets = [b['Name'] for b in response['Buckets']]
    
    assert bucket_name in buckets
    print(f"Successfully created and verified bucket: {bucket_name}")

if __name__ == '__main__':
    test_s3_bucket_creation()

view raw JSON →