Skip to main content

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

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cognito_local-0.1.0.tar.gz (15.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cognito_local-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file cognito_local-0.1.0.tar.gz.

File metadata

  • Download URL: cognito_local-0.1.0.tar.gz
  • Upload date:
  • Size: 15.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cognito_local-0.1.0.tar.gz
Algorithm Hash digest
SHA256 05117b35279d511f68bec422a2a98eb20db627f0dc9f9adfbbae97c4d1606d38
MD5 c01f413985487d26a8ad97e10a540cde
BLAKE2b-256 b89c7fad7f97ba38e0cfd397940c00a41c51ea3bab730393b90464a2fe5b0104

See more details on using hashes here.

File details

Details for the file cognito_local-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cognito_local-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for cognito_local-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 283971d353651ed81dd392369f39b84ffe6bc9e1c4c8bbc017ef41774a61ef2e
MD5 8a4d2cbaaf6bc30ffb795641d7ee8596
BLAKE2b-256 f1fa6ca93aef54f4aba60c9f2ada1b50e36112e8997a2af9ef610cfa5d5fe812

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page