Skip navigation links

Package software.amazon.awscdk.services.docdb

Amazon DocumentDB Construct Library

See: Description

Package software.amazon.awscdk.services.docdb Description

Amazon DocumentDB Construct Library

---

cfn-resources: Stable

cdk-constructs: Stable


Starting a Clustered Database

To set up a clustered DocumentDB database, define a DatabaseCluster. You must always launch a database in a VPC. Use the vpcSubnets attribute to control whether your instances will be launched privately or publicly:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object cluster = DatabaseCluster.Builder.create(this, "Database")
         .masterUser(Map.of(
                 "username", "myuser"))
         .instanceType(ec2.InstanceType.of(ec2.InstanceClass.getR5(), ec2.InstanceSize.getLARGE()))
         .vpcSubnets(Map.of(
                 "subnetType", ec2.SubnetType.getPUBLIC()))
         .vpc(vpc)
         .build();
 

By default, the master password will be generated and stored in AWS Secrets Manager with auto-generated description.

Your cluster will be empty by default.

Connecting

To control who can access the cluster, use the .connections attribute. DocumentDB databases have a default port, so you don't need to specify the port:

 // Example automatically generated. See https://github.com/aws/jsii/issues/826
 cluster.connections.allowDefaultPortFromAnyIpv4("Open to the world");
 

The endpoints to access your database cluster will be available as the .clusterEndpoint and .clusterReadEndpoint attributes:

 // Example automatically generated. See https://github.com/aws/jsii/issues/826
 Object writeAddress = cluster.clusterEndpoint.getSocketAddress();
 

If you have existing security groups you would like to add to the cluster, use the addSecurityGroups method. Security groups added in this way will not be managed by the Connections object of the cluster.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object securityGroup = SecurityGroup.Builder.create(stack, "SecurityGroup")
         .vpc(vpc)
         .build();
 cluster.addSecurityGroups(securityGroup);
 

Deletion protection

Deletion protection can be enabled on an Amazon DocumentDB cluster to prevent accidental deletion of the cluster:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object cluster = DatabaseCluster.Builder.create(this, "Database")
         .masterUser(Map.of(
                 "username", "myuser"))
         .instanceType(ec2.InstanceType.of(ec2.InstanceClass.getR5(), ec2.InstanceSize.getLARGE()))
         .vpcSubnets(Map.of(
                 "subnetType", ec2.SubnetType.getPUBLIC()))
         .vpc(vpc)
         .deletionProtection(true)
         .build();
 

Rotating credentials

When the master password is generated and stored in AWS Secrets Manager, it can be rotated automatically:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 cluster.addRotationSingleUser();
 

 // Example automatically generated. See https://github.com/aws/jsii/issues/826
 DatabaseCluster cluster = new DatabaseCluster(stack, "Database", new DatabaseClusterProps()
         .masterUser(new Login()
                 .username("docdb"))
         .instanceType(ec2.InstanceType.of(ec2.InstanceClass.getR5(), ec2.InstanceSize.getLARGE()))
         .vpc(vpc)
         .removalPolicy(cdk.RemovalPolicy.getDESTROY()));
 
 cluster.addRotationSingleUser();
 

The multi user rotation scheme is also available:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 cluster.addRotationMultiUser("MyUser", Map.of(
         "secret", myImportedSecret));
 

It's also possible to create user credentials together with the cluster and add rotation:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Object myUserSecret = DatabaseSecret.Builder.create(this, "MyUserSecret")
         .username("myuser")
         .masterSecret(cluster.getSecret())
         .build();
 Object myUserSecretAttached = myUserSecret.attach(cluster);// Adds DB connections information in the secret
 
 cluster.addRotationMultiUser("MyUser", Map.of(// Add rotation using the multi user scheme
         "secret", myUserSecretAttached));
 

Note: This user must be created manually in the database using the master credentials. The rotation will start as soon as this user exists.

See also @aws-cdk/aws-secretsmanager for credentials rotation of existing clusters.

Skip navigation links