This package has been deprecated

Author message:

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 https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html

@aws-cdk/aws-docdb
TypeScript icon, indicating that this package has built-in type declarations

1.204.0 • Public • Published

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:

declare const vpc: ec2.Vpc;
const cluster = new docdb.DatabaseCluster(this, 'Database', {
  masterUser: {
    username: 'myuser', // NOTE: 'admin' is reserved by DocumentDB
    excludeCharacters: '\"@/:', // optional, defaults to the set "\"@/" and is also used for eventually created rotations
    secretName: '/myapp/mydocdb/masteruser', // optional, if you prefer to specify the secret name
  },
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
  vpcSubnets: {
    subnetType: ec2.SubnetType.PUBLIC,
  },
  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:

declare const cluster: docdb.DatabaseCluster;
cluster.connections.allowDefaultPortFromAnyIpv4('Open to the world');

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

declare const cluster: docdb.DatabaseCluster;
const writeAddress = cluster.clusterEndpoint.socketAddress;   // "HOSTNAME:PORT"

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.

declare const vpc: ec2.Vpc;
declare const cluster: docdb.DatabaseCluster;

const securityGroup = new ec2.SecurityGroup(this, 'SecurityGroup', {
  vpc,
});
cluster.addSecurityGroups(securityGroup);

Deletion protection

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

declare const vpc: ec2.Vpc;
const cluster = new docdb.DatabaseCluster(this, 'Database', {
  masterUser: {
    username: 'myuser',
  },
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
  vpcSubnets: {
    subnetType: ec2.SubnetType.PUBLIC,
  },
  vpc,
  deletionProtection: true, // Enable deletion protection.
});

Rotating credentials

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

declare const cluster: docdb.DatabaseCluster;
cluster.addRotationSingleUser(); // Will rotate automatically after 30 days

example of setting up master password rotation for a cluster

The multi user rotation scheme is also available:

import * as secretsmanager from '@aws-cdk/aws-secretsmanager';

declare const myImportedSecret: secretsmanager.Secret;
declare const cluster: docdb.DatabaseCluster;

cluster.addRotationMultiUser('MyUser', {
  secret: myImportedSecret, // This secret must have the `masterarn` key
});

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

declare const cluster: docdb.DatabaseCluster;
const myUserSecret = new docdb.DatabaseSecret(this, 'MyUserSecret', {
  username: 'myuser',
  masterSecret: cluster.secret,
});
const myUserSecretAttached = myUserSecret.attach(cluster); // Adds DB connections information in the secret

cluster.addRotationMultiUser('MyUser', { // Add rotation using the multi user scheme
  secret: myUserSecretAttached, // This secret must have the `masterarn` key
});

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 * as iam from '@aws-cdk/aws-iam';
import * as logs from'@aws-cdk/aws-logs';

declare const myLogsPublishingRole: iam.Role;
declare const vpc: ec2.Vpc;

const cluster = new docdb.DatabaseCluster(this, 'Database', {
  masterUser: {
    username: 'myuser',
  },
  instanceType: ec2.InstanceType.of(ec2.InstanceClass.R5, ec2.InstanceSize.LARGE),
  vpcSubnets: {
    subnetType: ec2.SubnetType.PUBLIC,
  },
  vpc,
  exportProfilerLogsToCloudWatch: true, // Enable sending profiler logs
  exportAuditLogsToCloudWatch: true, // Enable sending audit logs
  cloudWatchLogsRetention: logs.RetentionDays.THREE_MONTHS, // Optional - default is to never expire logs
  cloudWatchLogsRetentionRole: myLogsPublishingRole, // Optional - a role will be created if not provided
});

Versions

Current Tags

VersionDownloads (Last 7 Days)Published

Version History

VersionDownloads (Last 7 Days)Published
1.203.01
1.202.00
1.201.06
1.200.01
1.199.00
1.198.10
1.198.00
1.197.00
1.196.00
1.195.00
1.194.00
1.193.00
1.192.00
1.191.00
1.190.01
1.189.00
1.188.00
1.187.02
1.186.10
1.186.00
1.185.00
1.184.11
1.184.00
1.183.00
1.182.00
1.181.11
1.181.02
1.180.099
1.179.00
1.178.00
1.177.00
1.176.00
1.175.00
1.174.00
1.173.01
1.172.00
1.171.00
1.170.10
1.170.00
1.169.00
1.168.00
1.167.00
1.166.10
1.165.00
1.164.00
1.163.21
1.163.10
1.163.00
1.162.09
1.161.01
1.160.00
1.159.01
1.158.00
1.157.00
1.156.10
1.156.00
1.155.00
1.154.00
1.153.10
1.153.00
1.152.00
1.151.00
1.150.00
1.149.00
1.148.00
1.147.00
1.146.00
1.145.00
1.144.00
1.143.00
1.142.00
1.141.00
1.140.03
1.139.00
1.138.20
1.138.10
1.138.00
1.137.00
1.136.00
1.135.01
1.134.00
1.133.00
1.132.00
1.131.00
1.130.04
1.129.00
1.128.00
1.127.04
1.126.03
1.125.02
1.124.03
1.123.00
1.122.015
1.121.01
1.120.01
1.119.04
1.118.00
1.117.05
1.116.00
1.115.015
1.114.00
1.113.00
1.112.03
1.111.01
1.110.12
1.110.00
1.109.00
1.108.10
1.108.00
1.107.00
1.106.17
1.106.00
1.105.00
1.104.00
1.103.00
1.102.00
1.101.00
1.100.00
1.99.00
1.98.00
1.97.00
1.96.00
1.95.20
1.95.10
1.95.00
1.94.16
1.94.00
1.93.00
1.92.00
1.91.00
1.90.10
1.90.00
1.89.02
1.88.00
1.87.15
1.87.00
1.86.00
1.85.00
1.84.00
1.83.00
1.82.00
1.81.00
1.80.07
1.79.07
1.78.08
1.77.09
1.76.07
1.75.05
1.74.015
1.73.01
1.72.04
1.71.00
1.70.00
1.69.00
1.68.00
1.67.00
1.66.00
1.65.00
1.64.10
1.64.00
1.63.00
1.62.00
1.61.10
1.61.00
1.60.00
1.59.07
1.58.00
1.57.00
1.56.00
1.55.00
1.54.00
1.53.00
1.52.00
1.51.00
1.50.00
1.49.10
1.49.00
1.48.00
1.47.10
1.47.00
1.46.00
1.45.00
1.44.00
1.43.00
1.42.10
1.42.00
1.41.00
1.40.00
1.39.01
1.38.00
1.37.00
1.36.10
1.36.00
1.35.00
1.34.10
1.34.00
1.33.10
1.33.00
1.32.21
1.32.10
1.32.00
1.31.00
1.30.00
1.29.00
1.28.00
1.27.00
1.26.00
1.25.00
1.24.00
1.23.00
1.22.00
1.21.10
1.21.00
1.20.00
1.19.00
1.18.00
1.17.1109
1.17.00
1.16.38
1.16.20
1.16.10
1.16.00
1.15.01
1.14.00
1.13.11
1.13.00
1.12.00
1.11.00
1.10.16
1.10.00
1.9.03
1.8.00
1.7.01
1.6.10
1.6.00
1.5.00
1.4.00
1.3.00
1.2.00
1.1.00
1.0.00
0.39.00
0.38.00
0.37.00
0.36.20
0.36.10
0.36.00
0.35.00
0.34.00
0.33.00
0.32.00
0.31.00
0.30.00
0.29.00
0.28.00
0.27.00
0.26.00
0.25.30
0.25.20
0.25.10
0.25.00
0.24.10
0.24.00
0.23.00
0.22.00

Package Sidebar

Install

npm i @aws-cdk/aws-docdb

Weekly Downloads

701

Version

1.204.0

License

Apache-2.0

Unpacked Size

736 kB

Total Files

29

Last publish

Collaborators

  • amzn-oss
  • aws-cdk-team