A Django integration for AWS Secrets Manager with caching and lazy loading support.
django-simple-secrets
provides a clean and efficient way to access AWS Secrets Manager from a Django applications. It features built-in caching to reduce API calls and supports lazy loading of secrets through Django's SimpleLazyObject
.
use_cache=False
.
- Simple, intuitive API for accessing AWS Secrets Manager
- Built-in caching mechanism to minimize API calls
- Lazy loading support for improved performance
- Configured retry logic and timeouts
- Type hints for better IDE support
- Compatible with Python 3.10+ and Django 4.x/5.x
pip install django-simple-secrets
Note: This package requires boto3
, which must be installed separately:
pip install boto3
Ensure your AWS credentials are properly configured either through:
- Environment variables (
AWS_ACCESS_KEY_ID
,AWS_SECRET_ACCESS_KEY
) - AWS credentials file
- IAM role (when running on AWS infrastructure)
from django_secrets import get_secret
# Get an entire secret
database_config = get_secret('prod/database')
# Get a specific key from a secret
database_password = get_secret('prod/database', key='password')
from django_secrets import get_secret_lazy
# Secret won't be fetched until actually accessed
database_config = get_secret_lazy('prod/database')
from django_secrets import clear_cache
# Clear a specific secret from cache
clear_cache('prod/database')
# Clear entire cache
clear_cache()
# Bypass cache for a single request
fresh_secret = get_secret('prod/database', use_cache=False)
The module raises standard boto3 exceptions:
try:
secret = get_secret('prod/database')
except ClientError as e:
# Handle AWS-specific errors
pass
except KeyError as e:
# Handle missing key errors
pass
- The module maintains a single boto3 client instance
- Caching is enabled by default to minimize API calls
- Timeout settings: 2 seconds for both connect and read
- Maximum retry attempts: 3
This project is licensed under the MIT License. See the LICENSE file for details.