How to Connect to Amazon DocumentDB with Python (`pymongo`)

Amazon DocumentDB is a fully managed document database service that supports MongoDB workloads. While it behaves similarly to MongoDB, connecting to it requires a few extra steps โ€” especially with SSL and replica sets.

In this short guide, I’ll show you how to connect to your Amazon DocumentDB cluster using Python and the pymongo driver.

๐Ÿ“ฆ Prerequisites

Before jumping into the code, make sure you have the following ready:

โœ… Amazon DocumentDB cluster (with rs0 as the replica set)
โœ… A user and password to authenticate
โœ… The AWS-provided SSL certificate
โœ… Python 3.7+
โœ… The pymongo library

Install pymongo via pip:

pip install pymongo

Download the global CA bundle (required for SSL):

curl -o global-bundle.pem https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem

๐Ÿง  Understanding the Connection Requirements

Amazon DocumentDB requires:

  • TLS/SSL enabled (ssl=true)
  • Replica set name specified (replicaSet=rs0)
  • Retryable writes disabled (retryWrites=false)

๐Ÿงช Python Example: check_connection.py

from pymongo import MongoClient

# Replace with your actual credentials and endpoint
username = "myadmin"
password = "**********"
cluster_endpoint = "docdb-dima-1.cluster-xxxxxxxxxxxx.us-east-1.docdb.amazonaws.com"
port = 27017
database_name = "test"
ca_cert_path = "global-bundle.pem"  # Path to Amazon CA certificate

# Construct the URI
uri = (
    f"mongodb://{username}:{password}@{cluster_endpoint}:{port}/"
    f"?ssl=true"
    f"&replicaSet=rs0"
    f"&readPreference=secondaryPreferred"
    f"&retryWrites=false"
)

# Create MongoClient with SSL configuration
client = MongoClient(uri, tlsCAFile=ca_cert_path)

# Access the database and print collections
db = client[database_name]
print(db.list_collection_names())

โš ๏ธ Common Pitfalls

Here are a few gotchas to watch out for:

Networking issues

  • Ensure your client can reach the DocumentDB cluster (same VPC, VPN, or public access if configured).
  • Port 27017 must be open in your cluster’s security group.

SSL certificate mismatch

Incorrect replica set name

  • DocumentDB uses a static replica set name: rs0.

Retry writes

  • Disable retryable writes: retryWrites=false. DocumentDB doesn’t support them.

โœ… Output Example

If everything is configured correctly, the script will print the list of collections in your specified database:

['users', 'orders', 'logs']

๐Ÿš€ Final Thoughts

Connecting to Amazon DocumentDB is easy once you get past the SSL and replica set nuances. This Python script provides a solid foundation for building apps that securely interact with your DocumentDB cluster.

Similar Posts