Registry / database / pymongo-auth-aws

pymongo-auth-aws

JSON →
library1.3.0pypypi✓ verified 26d ago

This library provides MONGODB-AWS authentication support for PyMongo, enabling applications to connect to MongoDB using AWS IAM credentials, temporary AWS IAM credentials, or credentials assigned to EC2 instances or ECS tasks. It is currently at version 1.3.0 and aims to align its Python version support with PyMongo's releases.

pip install pymongo-auth-aws
INSTALL
IMPORT
SIG · PYMONGO-AUTH-AWS
P
pymongo-auth-aws
databasepythonv1.3.0
Install
4.3s avg
Import
705ms
Disk
60MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.737s · 59.5MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.3s · import 0.672s · 62MB
60MB installed
● package 60MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

AwsCredential
from pymongo_auth_aws import AwsCredential
from pymongo import MongoClient
AwsSaslContext
from pymongo_auth_aws import AwsSaslContext
from pymongo import MongoClient
PyMongoAuthAwsError
from pymongo_auth_aws import PyMongoAuthAwsError
from pymongo import MongoClient

This example demonstrates connecting to MongoDB Atlas using the `MONGODB-AWS` authentication mechanism. It assumes `pymongo-auth-aws` is installed and AWS credentials (access key ID, secret access key, and optionally a session token) are configured in the environment variables (e.g., `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`). The `authMechanism=MONGODB-AWS` and `authSource=$external` parameters are crucial in the connection URI.

import os from pymongo import MongoClient from pymongo.server_api import ServerApi # Set these environment variables for authentication: # os.environ['AWS_ACCESS_KEY_ID'] = 'YOUR_AWS_ACCESS_KEY_ID' # os.environ['AWS_SECRET_ACCESS_KEY'] = 'YOUR_AWS_SECRET_ACCESS_KEY' # os.environ['AWS_SESSION_TOKEN'] = 'YOUR_AWS_SESSION_TOKEN' # Optional, for temporary credentials # Replace <YOUR_CLUSTER_URI> with your MongoDB Atlas connection string # Ensure authMechanism=MONGODB-AWS and authSource=$external are set in the URI # For example: mongodb+srv://<cluster_name>.mongodb.net/?authMechanism=MONGODB-AWS&authSource=%24external&retryWrites=true&w=majority ATLAS_URI = os.environ.get("MONGODB_AWS_URI", "mongodb+srv://user:pass@host/db?authMechanism=MONGODB-AWS&authSource=%24external") client = None try: # MongoClient will automatically pick up AWS credentials from environment variables # or other boto3-supported sources if not provided in the URI. client = MongoClient(ATLAS_URI, server_api=ServerApi('1')) client.admin.command('ping') print("Pinged your deployment. You successfully connected to MongoDB using MONGODB-AWS!") except Exception as e: print(f"Connection failed: {e}") finally: if client: client.close()
Debug
Known issues
breakingVersion 1.2.0 dropped support for several older Python versions, specifically Python 2.7, 3.4, 3.5, 3.6, and 3.7. Users on these versions must remain on `pymongo-auth-aws<1.2.0` or upgrade their Python environment.
fix
Upgrade to Python 3.8+ or pin `pymongo-auth-aws<1.2.0`.
affects: >=1.2.0
gotchaWith `pymongo-auth-aws>=1.1.0`, the order of credential lookup for AWS authentication now aligns with `boto3`'s default behavior. This means shared AWS credentials or config files might be prioritized over environment variables if not explicitly overridden, which could alter credential resolution compared to earlier versions.
fix
Review the `boto3` credential chain documentation. To explicitly control, set environment variables like `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_SESSION_TOKEN`, or set `AWS_SHARED_CREDENTIALS_FILE=""` to prevent loading from shared files.
affects: >=1.1.0
gotchaThe MONGODB-AWS authentication mechanism requires MongoDB server version 4.4+ and PyMongo driver version 3.11+. Ensure your MongoDB deployment and PyMongo version meet these requirements.
fix
Upgrade your MongoDB server to 4.4+ and your PyMongo library to 3.11+ (or use `pymongo[aws]` to ensure compatibility).
affects: <4.4 (MongoDB), <3.11 (PyMongo)
gotchaWhen using MONGODB-AWS authentication, you must specify `authSource=$external` in your MongoDB connection URI or as a `MongoClient` option. Failing to do so will result in authentication errors.
fix
Add `authSource=$external` to your connection URI (e.g., `mongodb+srv://...&authSource=%24external`) or pass `authSource='$external'` to the `MongoClient` constructor.
affects: *
Upgrade
Version history
1.3.0latest on PyPI · released Sep 11, 2024
Audit
Dependencies
pymongorequiredThis library provides an authentication mechanism for PyMongo; it is a required peer dependency.
boto3requiredUsed internally by pymongo-auth-aws for managing and resolving AWS credentials.
Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
pymongo-auth-aws — pip install pymongo-auth-aws · libregistry