JSPM

@aws-cdk/aws-docdb

1.122.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1706
  • Score
    100M100P100Q22129F
  • License Apache-2.0

The CDK Construct Library for AWS::DocDB

Package Exports

  • @aws-cdk/aws-docdb

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@aws-cdk/aws-docdb) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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:

const cluster = new DatabaseCluster(this, 'Database', {
    masterUser: {
        username: 'myuser' // NOTE: 'admin' is reserved by DocumentDB
    },
    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:

cluster.connections.allowDefaultPortFromAnyIpv4('Open to the world');

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

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.

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

Deletion protection

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

const cluster = new 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:

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:

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:

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.