Skip to main content

cognito-local

A lightweight, persistent Cognito mock server using Moto with file-based state persistence.

alt text

alt text

alt text alt text alt text alt text alt text

This is a wrapper around the moto library in a lightweight Docker container and adds a custom persistence layer that auto-saves your data to disk.

✨ Features

💾 Persistent. ⚡ Blazing Fast. 🆓 Free & Open. 🐳 Docker Ready.

Cryptography Support: Includes patches to correctly serialize RSA keys used for JWT signing.

🔁 Auto-Save: Background thread saves state every interval and handles graceful shutdowns (SIGTERM).

It supports multiple regions and can be configured to use a custom data directory.

🚀 Quick Start

Using Docker Compose (Recommended)

Add this to your docker-compose.yml.

services:
  cognito:
    image: usermalikhan/cognito-local:latest
    container_name: cognito-local
    ports:
      - "4566:4566"
    volumes:
      - ./cognito-data:/data
    environment:
      - AWS_DEFAULT_REGION=eu-central-1
      - PORT=4566

Using Docker CLI

docker run -d \
  -p 4566:4566 \
  -v $(pwd)/cognito-data:/data \
  --region eu-central-1 \
  --name cognito-local \
  usermalikhan/cognito-local:latest

🛠 How It Works

This project is a Python wrapper around the moto[server] library.

The Engine: It uses moto's ThreadedMotoServer to handle AWS API requests.

The Patch: It injects a custom serializer into Python's pickle module using copyreg. This allows it to save complex C-based cryptography objects (like RSA Private Keys) which standard Python cannot pickle.

The Loop: A background thread monitors the Moto backend object. Every interval (or on Docker shutdown signals), it dumps the entire memory state to a file (/data/cognito.db).

The Restoration: On startup, it checks for the database file and injects the data back into Moto's memory before the server starts accepting requests.

🔌 Connecting to It

Since this is a local mock, you must tell your AWS clients (CLI, Boto3, JS SDK) to use the local endpoint.

Note: You must specify a region and fake credentials.

AWS CLI

1. Configure fake credentials for the session

export AWS_ACCESS_KEY_ID=test
export AWS_SECRET_ACCESS_KEY=test
export AWS_DEFAULT_REGION=eu-central-1

2. Run commands pointing to localhost

aws --endpoint-url=http://localhost:4566 cognito-idp create-user-pool --pool-name "MyPersistentPool"

Python (Boto3)

import boto3

cognito = boto3.client(
    "cognito-idp",
    region_name="eu-central-1",
    endpoint_url="http://localhost:4566",
    aws_access_key_id="test",
    aws_secret_access_key="test"
)
cognito.create_user_pool(PoolName="MyTestPool")

JavaScript / TypeScript (AWS SDK v3)

import { CognitoIdentityProviderClient } from "@aws-sdk/client-cognito-identity-provider";

const client = new CognitoIdentityProviderClient({
  region: "eu-central-1",
  endpoint: "http://localhost:4566",
  credentials: {
    accessKeyId: "test",
    secretAccessKey: "test",
  },
});

⚙️ Configuration

You can configure the behavior using Environment Variables in Docker:

Variable Default Description
PORT 4566 The port the server listens on.
DATA_FILE /data/cognito.db Path inside the container where data is saved.
SAVE_INTERVAL 60 How often (in seconds) to auto-save to disk.

🧪 Development

If you want to contribute or run this locally without Docker:

1. Clone repo

git clone https://github.com/m-ali-ubit/cognito-local.git
cd cognito-local

2. Install dependencies

pip install -e .[dev]

3. Run Server

python -m cognito_local.main

4. Run Tests

pytest

⚖️ License

Apache License 2.0

This project is not affiliated with AWS or LocalStack. It is an independent open-source tool based on the Moto library.

Release files for cognito-local 0.1.0

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

Source distribution (sdist)

Source distribution for cognito-local 0.1.0
File Size Uploaded
cognito_local-0.1.0.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cognito-local 0.1.0
File Interpreter ABI Platform
cognito_local-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 26.2 kB

Release files / cognito_local-0.1.0.tar.gz

Download URL cognito_local-0.1.0.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
05117b35279d511f68bec422a2a98eb20db627f0dc9f9adfbbae97c4d1606d38
BLAKE2b-256 checksum
How to use checksums
b89c7fad7f97ba38e0cfd397940c00a41c51ea3bab730393b90464a2fe5b0104
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / cognito_local-0.1.0-py3-none-any.whl

Download URL cognito_local-0.1.0-py3-none-any.whl
Size 10.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
283971d353651ed81dd392369f39b84ffe6bc9e1c4c8bbc017ef41774a61ef2e
BLAKE2b-256 checksum
How to use checksums
f1fa6ca93aef54f4aba60c9f2ada1b50e36112e8997a2af9ef610cfa5d5fe812
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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