@@ -18,17 +18,18 @@ always launch a database in a VPC. Use the `vpcSubnets` attribute to control whe
18
18
your instances will be launched privately or publicly:
19
19
20
20
``` ts
21
- const cluster = new DatabaseCluster (this , ' Database' , {
22
- masterUser: {
23
- username: ' myuser' // NOTE: 'admin' is reserved by DocumentDB
24
- excludeCharacters : ' \" @/:' , // optional, defaults to the set "\"@/" and is also used for eventually created rotations
25
- secretName: ' /myapp/mydocdb/masteruser' , // optional, if you prefer to specify the secret name
26
- },
27
- instanceType: ec2 .InstanceType .of (ec2 .InstanceClass .R5 , ec2 .InstanceSize .LARGE ),
28
- vpcSubnets: {
29
- subnetType: ec2 .SubnetType .PUBLIC ,
30
- },
31
- vpc
21
+ declare const vpc: ec2 .Vpc ;
22
+ const cluster = new docdb .DatabaseCluster (this , ' Database' , {
23
+ masterUser: {
24
+ username: ' myuser' , // NOTE: 'admin' is reserved by DocumentDB
25
+ excludeCharacters: ' \" @/:' , // optional, defaults to the set "\"@/" and is also used for eventually created rotations
26
+ secretName: ' /myapp/mydocdb/masteruser' , // optional, if you prefer to specify the secret name
27
+ },
28
+ instanceType: ec2 .InstanceType .of (ec2 .InstanceClass .R5 , ec2 .InstanceSize .LARGE ),
29
+ vpcSubnets: {
30
+ subnetType: ec2 .SubnetType .PUBLIC ,
31
+ },
32
+ vpc ,
32
33
});
33
34
```
34
35
@@ -42,21 +43,26 @@ To control who can access the cluster, use the `.connections` attribute. Documen
42
43
you don't need to specify the port:
43
44
44
45
``` ts
46
+ declare const cluster: docdb .DatabaseCluster ;
45
47
cluster .connections .allowDefaultPortFromAnyIpv4 (' Open to the world' );
46
48
```
47
49
48
50
The endpoints to access your database cluster will be available as the ` .clusterEndpoint ` and ` .clusterReadEndpoint `
49
51
attributes:
50
52
51
53
``` ts
54
+ declare const cluster: docdb .DatabaseCluster ;
52
55
const writeAddress = cluster .clusterEndpoint .socketAddress ; // "HOSTNAME:PORT"
53
56
```
54
57
55
58
If you have existing security groups you would like to add to the cluster, use the ` addSecurityGroups ` method. Security
56
59
groups added in this way will not be managed by the ` Connections ` object of the cluster.
57
60
58
61
``` ts
59
- const securityGroup = new ec2 .SecurityGroup (stack , ' SecurityGroup' , {
62
+ declare const vpc: ec2 .Vpc ;
63
+ declare const cluster: docdb .DatabaseCluster ;
64
+
65
+ const securityGroup = new ec2 .SecurityGroup (this , ' SecurityGroup' , {
60
66
vpc ,
61
67
});
62
68
cluster .addSecurityGroups (securityGroup );
@@ -67,16 +73,17 @@ cluster.addSecurityGroups(securityGroup);
67
73
Deletion protection can be enabled on an Amazon DocumentDB cluster to prevent accidental deletion of the cluster:
68
74
69
75
``` ts
70
- const cluster = new DatabaseCluster (this , ' Database' , {
71
- masterUser: {
72
- username: ' myuser'
73
- },
74
- instanceType: ec2 .InstanceType .of (ec2 .InstanceClass .R5 , ec2 .InstanceSize .LARGE ),
75
- vpcSubnets: {
76
- subnetType: ec2 .SubnetType .PUBLIC ,
77
- },
78
- vpc ,
79
- deletionProtection: true // Enable deletion protection.
76
+ declare const vpc: ec2 .Vpc ;
77
+ const cluster = new docdb .DatabaseCluster (this , ' Database' , {
78
+ masterUser: {
79
+ username: ' myuser' ,
80
+ },
81
+ instanceType: ec2 .InstanceType .of (ec2 .InstanceClass .R5 , ec2 .InstanceSize .LARGE ),
82
+ vpcSubnets: {
83
+ subnetType: ec2 .SubnetType .PUBLIC ,
84
+ },
85
+ vpc ,
86
+ deletionProtection: true , // Enable deletion protection.
80
87
});
81
88
```
82
89
@@ -85,6 +92,7 @@ const cluster = new DatabaseCluster(this, 'Database', {
85
92
When the master password is generated and stored in AWS Secrets Manager, it can be rotated automatically:
86
93
87
94
``` ts
95
+ declare const cluster: docdb .DatabaseCluster ;
88
96
cluster .addRotationSingleUser (); // Will rotate automatically after 30 days
89
97
```
90
98
@@ -93,22 +101,28 @@ cluster.addRotationSingleUser(); // Will rotate automatically after 30 days
93
101
The multi user rotation scheme is also available:
94
102
95
103
``` ts
104
+ import * as secretsmanager from ' @aws-cdk/aws-secretsmanager' ;
105
+
106
+ declare const myImportedSecret: secretsmanager .Secret ;
107
+ declare const cluster: docdb .DatabaseCluster ;
108
+
96
109
cluster .addRotationMultiUser (' MyUser' , {
97
- secret: myImportedSecret // This secret must have the `masterarn` key
110
+ secret: myImportedSecret , // This secret must have the `masterarn` key
98
111
});
99
112
```
100
113
101
114
It's also possible to create user credentials together with the cluster and add rotation:
102
115
103
116
``` ts
117
+ declare const cluster: docdb .DatabaseCluster ;
104
118
const myUserSecret = new docdb .DatabaseSecret (this , ' MyUserSecret' , {
105
119
username: ' myuser' ,
106
- masterSecret: cluster .secret
120
+ masterSecret: cluster .secret ,
107
121
});
108
122
const myUserSecretAttached = myUserSecret .attach (cluster ); // Adds DB connections information in the secret
109
123
110
124
cluster .addRotationMultiUser (' MyUser' , { // Add rotation using the multi user scheme
111
- secret: myUserSecretAttached // This secret must have the `masterarn` key
125
+ secret: myUserSecretAttached , // This secret must have the `masterarn` key
112
126
});
113
127
```
114
128
@@ -126,8 +140,21 @@ Sending audit or profiler needs to be configured in two places:
126
140
2 . Enable the corresponding option(s) when creating the ` DatabaseCluster ` :
127
141
128
142
``` ts
129
- const cluster = new DatabaseCluster (this , ' Database' , {
130
- ... ,
143
+ import * as iam from ' @aws-cdk/aws-iam' ;
144
+ import * as logs from ' @aws-cdk/aws-logs' ;
145
+
146
+ declare const myLogsPublishingRole: iam .Role ;
147
+ declare const vpc: ec2 .Vpc ;
148
+
149
+ const cluster = new docdb .DatabaseCluster (this , ' Database' , {
150
+ masterUser: {
151
+ username: ' myuser' ,
152
+ },
153
+ instanceType: ec2 .InstanceType .of (ec2 .InstanceClass .R5 , ec2 .InstanceSize .LARGE ),
154
+ vpcSubnets: {
155
+ subnetType: ec2 .SubnetType .PUBLIC ,
156
+ },
157
+ vpc ,
131
158
exportProfilerLogsToCloudWatch: true , // Enable sending profiler logs
132
159
exportAuditLogsToCloudWatch: true , // Enable sending audit logs
133
160
cloudWatchLogsRetention: logs .RetentionDays .THREE_MONTHS , // Optional - default is to never expire logs
0 commit comments