Before you begin
Three ways DocumentDB differs from PostgreSQL
Worth reading before you plan. Each of these fails in a way that does not name its own cause.Its IAM is a different mechanism
Both engines can use IAM, but not the same way. RDS PostgreSQL mints a short livedrds-db:connect token and presents it as the password, which is why it needs an IAM policy naming a dbuser ARN.
DocumentDB has no such token. The driver signs each handshake itself using the MONGODB-AWS SASL mechanism, so there is no IAM policy to attach. Instead you map the agent’s role ARN to a user inside the cluster. Nothing has to be enabled on the cluster or its parameter group first.
The agent connects per instance, not per cluster
A cluster endpoint points at the current primary and a reader endpoint balances across replicas. Either moves the agent to a different node behind its back, and the metrics then describe a host they are not attributed to. So the agent reaches every instance directly, and the config carries one entry per instance. Terraform expands a cluster into those entries for you, sodocumentdb_clusters stays a list of clusters rather than a list of hostnames you maintain by hand.
Discovery calls aws docdb describe-db-clusters, because hashicorp/aws ships no aws_docdb_cluster data source and the endpoints cannot be derived from the cluster endpoint by string surgery. That is why the AWS CLI has to be present wherever you run Terraform.
TLS is required and the agent does not ship this CA
DocumentDB requires TLS and presents a certificate from a private Amazon PKI that is in no operating system trust store. Under PostgreSQL managed authentication the agent carries the RDS bundle itself; for MongoDB it does not. The agent host downloads the bundle at boot and checks that what arrived really is a certificate, because a proxy returning an HTML error page otherwise leaves a file that exists and fails every connection.Get the Terraform
Store the license key
Configure
A DocumentDB only setup, withdatabases left empty:
Replica sets
A DocumentDB cluster is a replica set: one primary and zero or more replicas. You do not configure the replicas individually. Give Terraform the cluster identifier and it discovers the members, then writes one config entry per instance:cluster_name so
they are grouped together in the UI. Check what was resolved:
Why one entry per member and not one cluster endpoint
A replica set has three kinds of address, and only one of them is stable per node:
If the agent used the cluster endpoint, a failover would silently move it to a
different node while the metrics kept the old host’s name. Reporting each
member separately is what lets you see replication lag, per node load, and a
replica falling behind.
The bootstrap is the one thing that does use the cluster endpoint. Creating the
monitoring user is a write, and only the primary accepts writes, so it has to
follow the primary wherever it currently is.
After scaling the cluster
Discovery runs at plan time, so adding or removing a replica is picked up by an apply:instance_endpoints by hand instead of letting discovery run,
that list is now stale as well, and it will not follow a scale out. That is the
main reason to prefer discovery.
Failover
Nothing needs to change. Every member is already being collected from directly, so when the primary moves, the agent keeps reporting all of them and the roles swap over in the data. The IAM mapping is per cluster, not per node, so it survives a failover untouched.Monitoring several clusters
documentdb_clusters is a map, and one agent host collects from every entry:
terraform output monitored_target_count reports how many entries were produced, and documentdb_instance_endpoints shows which hosts they point at.
Create the database user
The agent authenticates as a user that has to exist in the cluster before it starts. There are two ways to create it.Automatically, at first boot
This is the option worth taking here, because the user’s name is the agent’s own IAM role ARN, and that role does not exist until Terraform creates it. Doing it on the host removes an apply, read the ARN, create the user, restart sequence.createUser is a write, and creates the user. If it already exists, only the missing roles are granted, so a re run never revokes anything granted out of band. The call is retried, because cloud init can win the race against the cluster becoming reachable.
By hand
Connect to the cluster’s primary as your master user, after the apply, and use the role ARN from the outputs:IAM authentication does not work for the master user, and does not work on elastic clusters or on engine 3.6 and 4.0. Confirm the target is an instance based 5.0 cluster before wiring this in.
Using a password instead
If you would rather not use IAM, give each cluster its own password parameter and turn managed authentication off:Apply
Verify
Monitoring both engines from one host
The agent readsconf.d/postgresql.d/ and conf.d/mongodb.d/ independently, so one machine covers both. Add a databases map alongside documentdb_clusters:
use_managed_authentication covers PostgreSQL and documentdb_use_managed_authentication covers DocumentDB, and the second defaults to the first, so one setting turns IAM on for both. Set them separately to keep DocumentDB on a password while PostgreSQL uses IAM.
The PostgreSQL side has its own prerequisites, including pg_stat_statements and a database reboot you may need to schedule. See Set Up the Agent with Terraform for PostgreSQL.
Removing it
atatus user and the SSM parameters are not Terraform’s and stay behind: