Skip to main content

Amazon DocumentDB Construct Library

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


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:

# vpc: ec2.Vpc

cluster = docdb.DatabaseCluster(self, "Database",
    master_user=docdb.Login(
        username="myuser",  # NOTE: 'admin' is reserved by DocumentDB
        exclude_characters="\"@/:",  # optional, defaults to the set "\"@/" and is also used for eventually created rotations
        secret_name="/myapp/mydocdb/masteruser"
    ),
    instance_type=ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
    vpc_subnets=ec2.SubnetSelection(
        subnet_type=ec2.SubnetType.PUBLIC
    ),
    vpc=vpc
)

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:

# cluster: docdb.DatabaseCluster

cluster.connections.allow_default_port_from_any_ipv4("Open to the world")

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

# cluster: docdb.DatabaseCluster

write_address = cluster.cluster_endpoint.socket_address

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.

# vpc: ec2.Vpc
# cluster: docdb.DatabaseCluster


security_group = ec2.SecurityGroup(self, "SecurityGroup",
    vpc=vpc
)
cluster.add_security_groups(security_group)

Deletion protection

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

# vpc: ec2.Vpc

cluster = docdb.DatabaseCluster(self, "Database",
    master_user=docdb.Login(
        username="myuser"
    ),
    instance_type=ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
    vpc_subnets=ec2.SubnetSelection(
        subnet_type=ec2.SubnetType.PUBLIC
    ),
    vpc=vpc,
    deletion_protection=True
)

Rotating credentials

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

# cluster: docdb.DatabaseCluster

cluster.add_rotation_single_user()
cluster = docdb.DatabaseCluster(stack, "Database",
    master_user=docdb.Login(
        username="docdb"
    ),
    instance_type=ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
    vpc=vpc,
    removal_policy=cdk.RemovalPolicy.DESTROY
)

cluster.add_rotation_single_user()

The multi user rotation scheme is also available:

import aws_cdk.aws_secretsmanager as secretsmanager

# my_imported_secret: secretsmanager.Secret
# cluster: docdb.DatabaseCluster


cluster.add_rotation_multi_user("MyUser",
    secret=my_imported_secret
)

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

# cluster: docdb.DatabaseCluster

my_user_secret = docdb.DatabaseSecret(self, "MyUserSecret",
    username="myuser",
    master_secret=cluster.secret
)
my_user_secret_attached = my_user_secret.attach(cluster) # Adds DB connections information in the secret

cluster.add_rotation_multi_user("MyUser",  # Add rotation using the multi user scheme
    secret=my_user_secret_attached)

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.

Audit and profiler Logs

Sending audit or profiler needs to be configured in two places:

  1. Check / create the needed options in your ParameterGroup for audit and profiler logs.
  2. Enable the corresponding option(s) when creating the DatabaseCluster:
import aws_cdk.aws_iam as iam
import aws_cdk.aws_logs as logs

# my_logs_publishing_role: iam.Role
# vpc: ec2.Vpc


cluster = docdb.DatabaseCluster(self, "Database",
    master_user=docdb.Login(
        username="myuser"
    ),
    instance_type=ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
    vpc_subnets=ec2.SubnetSelection(
        subnet_type=ec2.SubnetType.PUBLIC
    ),
    vpc=vpc,
    export_profiler_logs_to_cloud_watch=True,  # Enable sending profiler logs
    export_audit_logs_to_cloud_watch=True,  # Enable sending audit logs
    cloud_watch_logs_retention=logs.RetentionDays.THREE_MONTHS,  # Optional - default is to never expire logs
    cloud_watch_logs_retention_role=my_logs_publishing_role
)

Release files for aws-cdk.aws-docdb 1.204.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-cdk.aws-docdb 1.204.0
File Size Uploaded
aws-cdk.aws-docdb-1.204.0.tar.gz 162.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-cdk.aws-docdb 1.204.0
File Interpreter ABI Platform
aws_cdk.aws_docdb-1.204.0-py3-none-any.whl Python 3 none any Details

Total release size: 324.4 kB

Release files / aws-cdk.aws-docdb-1.204.0.tar.gz

Download URL aws-cdk.aws-docdb-1.204.0.tar.gz
Size 162.9 kB
Tags Source
SHA-256 checksum
How to use checksums
8eeaf49bb4a6925af0300b027349a5be756bad541f70e375b5c9cfacff360b8c
BLAKE2b-256 checksum
How to use checksums
d08c89bc803429399270f5597007794d022ceed748ceb04439f4cce07b28409b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / aws_cdk.aws_docdb-1.204.0-py3-none-any.whl

Download URL aws_cdk.aws_docdb-1.204.0-py3-none-any.whl
Size 161.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f0c893b7196205f761945478badd15b291c542640a7fc9343e1fce38baf7f190
BLAKE2b-256 checksum
How to use checksums
3eba12d1af3e1c3ae35a7c9e8450a4904f1bd60b85c3e06f21df40eed4953f7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.204.0 This release

2 release files

1.99.0

2 release files

1.98.0

2 release files

1.95.1

2 release files

1.95.0

2 release files

1.94.1

2 release files

1.94.0

2 release files

1.93.0

2 release files

1.91.0

2 release files

1.90.1

2 release files

1.90.0

2 release files

1.87.1

2 release files

1.87.0

2 release files

1.86.0

2 release files

1.85.0

2 release files

1.84.0

2 release files

1.81.0

2 release files

1.80.0

2 release files

1.79.0

2 release files

1.78.0

2 release files

1.75.0

2 release files

1.74.0

2 release files

1.73.0

2 release files

1.71.0

2 release files

1.70.0

2 release files

1.69.0

2 release files

1.68.0

2 release files

1.64.1

2 release files

1.64.0

2 release files

1.63.0

2 release files

1.61.1

2 release files

1.61.0

2 release files

1.60.0

2 release files

1.59.0

2 release files

1.58.0

2 release files

1.55.0

2 release files

1.54.0

2 release files

1.53.0

2 release files

1.52.0

2 release files

1.47.1

2 release files

1.47.0

2 release files

1.46.0

2 release files

1.42.0

2 release files

1.41.0

2 release files

1.40.0

2 release files

1.39.0

2 release files

1.36.1

2 release files

1.36.0

2 release files

1.35.0

2 release files

1.34.1

2 release files

1.34.0

2 release files

1.33.1

2 release files

1.33.0

2 release files

1.32.2

2 release files

1.31.0

2 release files

1.30.0

2 release files

1.29.0

2 release files

1.28.0

2 release files

1.26.0

2 release files

1.25.0

2 release files

1.24.0

2 release files

1.22.0

2 release files

1.21.1

2 release files

1.21.0

2 release files

1.19.0

2 release files

1.18.0

2 release files

1.17.1

2 release files

1.17.0

2 release files

1.16.3

2 release files

1.16.2

2 release files

1.16.1

2 release files

1.16.0

2 release files

1.15.0

2 release files

1.14.0

2 release files

1.13.1

2 release files

1.13.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.36.0

2 release files

0.35.0

2 release files

0.34.0

2 release files

0.33.0

2 release files

0.32.0

2 release files

0.29.0

2 release files

0.27.0

2 release files

0.26.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page