DocumentDB has two halves, and this page walks through both. The AWS SDK creates the cluster, attaches an instance, and hands you an endpoint. A MongoDB driver then connects to that endpoint over TLS and runs your documents. The SDK never runs a query.
Each example is small and shows either the AWS SDK call or, where relevant, how its result feeds a normal MongoDB driver. Read them in order - create the cluster, add an instance, read the endpoints, connect, then manage credentials.
A DocumentDB cluster owns the storage but has no compute until you attach an instance. Create the cluster with CreateDBCluster, then add an instance with CreateDBInstance, then wait for the instance to become available.
# --- Python (boto3) ---docdb.create_db_cluster( DBClusterIdentifier="app-docdb", Engine="docdb", MasterUsername="appuser", ManageMasterUserPassword=True, StorageEncrypted=True,)docdb.create_db_instance( DBInstanceIdentifier="app-docdb-1", DBClusterIdentifier="app-docdb", Engine="docdb", DBInstanceClass="db.t3.medium",)docdb.get_waiter("db_instance_available").wait(DBInstanceIdentifier="app-docdb-1")print("cluster ready - it now has an instance to connect to")
// --- TypeScript (AWS SDK v3) ---import { CreateDBClusterCommand, CreateDBInstanceCommand, waitUntilDBInstanceAvailable,} from "@aws-sdk/client-docdb";await docdb.send(new CreateDBClusterCommand({ DBClusterIdentifier: "app-docdb", Engine: "docdb", MasterUsername: "appuser", ManageMasterUserPassword: true, StorageEncrypted: true,}));await docdb.send(new CreateDBInstanceCommand({ DBInstanceIdentifier: "app-docdb-1", DBClusterIdentifier: "app-docdb", Engine: "docdb", DBInstanceClass: "db.t3.medium",}));await waitUntilDBInstanceAvailable({ client: docdb, maxWaitTime: 1200 }, { DBInstanceIdentifier: "app-docdb-1" });console.log("cluster ready - it now has an instance to connect to");
Engine is "docdb" on both the cluster and the instance - not "mongodb".
The cluster is storage; nothing is connectable until an instance is attached.
ManageMasterUserPassword puts the credential in Secrets Manager instead of your code.
Wait on db_instance_available - the endpoint is unusable while the instance is creating.
The endpoint from the previous step feeds a MongoDB driver. This is a driver connection, not an AWS SDK call - the SDK is done once you have the host. DocumentDB requires TLS and does not support retryable writes.
# --- Python (pymongo driver - NOT the AWS SDK) ---from pymongo import MongoClientclient = MongoClient( host="app-docdb.cluster-abc123.us-east-1.docdb.amazonaws.com", port=27017, tls=True, tlsCAFile="global-bundle.pem", username="appuser", password="from-secrets-manager", retryWrites=False, # required: DocumentDB has no retryable writes)client["shop"]["orders"].insert_one({"sku": "A-1", "qty": 2})print(client["shop"]["orders"].find_one({"sku": "A-1"}))
This is pymongo, a MongoDB driver - the AWS SDK has no query API for DocumentDB.
tls=True with the downloaded CA bundle is mandatory; connections without TLS are refused.
retryWrites=False is required - DocumentDB does not implement retryable writes.
The equivalent Node.js driver is mongodb; the same host, TLS, and options apply.
ManageMasterUserPassword stores the master credential in Secrets Manager instead of your code. Read its ARN from the cluster, then fetch the password your driver needs.
A second instance in the same cluster becomes a reader. Because storage is shared, it attaches in seconds without copying data, and the reader endpoint starts load-balancing across it.
Standard RDS provisions a database in a single CreateDBInstance. DocumentDB always separates storage from compute like Aurora: CreateDBCluster creates the shared storage volume, and CreateDBInstance with a DBClusterIdentifier attaches compute. A cluster with zero instances exists but has nothing to connect to - the writer endpoint has no instance behind it. Always create at least one instance and wait on db_instance_available before pointing a driver at the endpoint.
A create call returns in milliseconds with the resource in creating, and the endpoint is unusable until an instance is available. Use the db_instance_available waiter (or poll Status on the cluster and DBInstanceStatus on the instance) before the next step depends on the new state. Give the waiter a generous maxWaitTime because instance provisioning takes several minutes.
Connecting to a cluster with no instance. A bare cluster owns storage but has no compute. Fix: call CreateDBInstance and wait for it before connecting.
Omitting TLS in the driver. DocumentDB refuses non-TLS connections by default. Fix: pass tls=True with the downloaded CA bundle.
Leaving retryWrites on. The MongoDB default enables retryable writes, which DocumentDB rejects. Fix: set retryWrites=False in the driver.
Reading the endpoint too early. The endpoint is unusable while the instance is creating. Fix: wait on db_instance_available, then describe.
Expecting the SDK to run queries. There is no docdb API to find or insert. Fix: connect with pymongo / the node mongodb driver.
A cluster owns storage but has no compute until you attach an instance. Call CreateDBInstance with the cluster id, wait on db_instance_available, and then connect. The endpoint has no instance behind it until then.
Which endpoint do I give my MongoDB driver?
Use the cluster Endpoint (the writer) for writes and read-after-write, and the ReaderEndpoint for read-only queries. Both come from DescribeDBClusters and both listen on port 27017.
Why does my driver connection fail with a TLS error?
DocumentDB requires TLS by default and rejects plaintext connections. Download the DocumentDB CA bundle and pass tls=True with tlsCAFile pointing at it. Also set retryWrites=False, which DocumentDB requires.
Where does the master password live?
If you set ManageMasterUserPassword, DocumentDB stores and rotates it in AWS Secrets Manager and reports the secret ARN under MasterUserSecret. Your app reads the current value at connect time instead of holding a password in code.
Does the AWS SDK let me insert or query documents?
No. The docdb client only provisions and manages the cluster. Documents go through a MongoDB driver such as pymongo or the node mongodb package over the endpoint. DocumentDB has no Data API.
How do I scale reads?
Add reader instances to the cluster with CreateDBInstance. Because storage is shared, they attach in seconds without copying data. Route read-only queries to the reader endpoint, optionally with readPreference=secondaryPreferred in the driver.
What instance class should I start with?
db.t3.medium is a reasonable small default for development. For production, size to real CPU and memory use with a db.r6g or db.r5 class, and match reader size to writer size for balanced load.