1
1
## AWS RDS Construct Library
2
2
3
- The ` aws-cdk-rds ` package contains Constructs for setting up RDS instances.
4
-
5
- > Note: the functionality this package is currently limited, as the CDK team is
6
- > focusing on other use cases first. If your use case is not listed below, you
7
- > will have to use achieve it using CloudFormation resources.
8
- >
9
- > If you would like to help improve the state of this library, Pull Requests are
10
- > welcome.
11
-
12
- Supported:
13
-
14
- * Clustered databases
15
-
16
- Not supported:
17
-
18
- * Instance databases
19
- * Setting up from a snapshot
20
-
21
-
22
3
### Starting a Clustered Database
23
4
24
- To set up a clustered database (like Aurora), create an instance of ` DatabaseCluster ` . You must
5
+ To set up a clustered database (like Aurora), define a ` DatabaseCluster ` . You must
25
6
always launch a database in a VPC. Use the ` vpcSubnets ` attribute to control whether
26
7
your instances will be launched privately or publicly:
27
8
@@ -45,33 +26,84 @@ By default, the master password will be generated and stored in AWS Secrets Mana
45
26
Your cluster will be empty by default. To add a default database upon construction, specify the
46
27
` defaultDatabaseName ` attribute.
47
28
29
+ ### Starting an Instance Database
30
+ To set up a instance database, define a ` DatabaseInstance ` . You must
31
+ always launch a database in a VPC. Use the ` vpcSubnets ` attribute to control whether
32
+ your instances will be launched privately or publicly:
33
+
34
+ ``` ts
35
+ const instance = new DatabaseInstance (stack , ' Instance' , {
36
+ engine: rds .DatabaseInstanceEngine .OracleSE1 ,
37
+ instanceClass: new ec2 .InstanceTypePair (ec2 .InstanceClass .Burstable2 , ec2 .InstanceSize .Small ),
38
+ masterUsername: ' syscdk' ,
39
+ vpc
40
+ });
41
+ ```
42
+ By default, the master password will be generated and stored in AWS Secrets Manager.
43
+
44
+ Use ` DatabaseInstanceFromSnapshot ` and ` DatabaseInstanceReadReplica ` to create an instance from snapshot or
45
+ a source database respectively:
46
+
47
+ ``` ts
48
+ new DatabaseInstanceFromSnapshot (stack , ' Instance' , {
49
+ snapshotIdentifier: ' my-snapshot' ,
50
+ engine: rds .DatabaseInstanceEngine .Postgres ,
51
+ instanceClass: new ec2 .InstanceTypePair (ec2 .InstanceClass .Burstable2 , ec2 .InstanceSize .Large ),
52
+ vpc
53
+ });
54
+
55
+ new DatabaseInstanceReadReplica (stack , ' ReadReplica' , {
56
+ sourceDatabaseInstance: sourceInstance ,
57
+ engine: rds .DatabaseInstanceEngine .Postgres ,
58
+ instanceClass: new ec2 .InstanceTypePair (ec2 .InstanceClass .Burstable2 , ec2 .InstanceSize .Large ),
59
+ vpc
60
+ });
61
+ ```
62
+ Creating a "production" Oracle database instance with option and parameter groups:
63
+
64
+ [ example of setting up a production oracle instance] ( test/integ.instance.lit.ts )
65
+
66
+
67
+ ### Instance events
68
+ To define Amazon CloudWatch event rules for database instances, use the ` onEvent `
69
+ method:
70
+
71
+ ``` ts
72
+ const rule = instance .onEvent (' InstanceEvent' , { target: new targets .LambdaFunction (fn ) });
73
+ ```
74
+
48
75
### Connecting
49
76
50
- To control who can access the cluster, use the ` .connections ` attribute. RDS database have
77
+ To control who can access the cluster or instance , use the ` .connections ` attribute. RDS databases have
51
78
a default port, so you don't need to specify the port:
52
79
53
80
``` ts
54
81
cluster .connections .allowFromAnyIpv4 (' Open to the world' );
55
82
```
56
83
57
- The endpoints to access your database will be available as the ` .clusterEndpoint ` and ` .readerEndpoint `
84
+ The endpoints to access your database cluster will be available as the ` .clusterEndpoint ` and ` .readerEndpoint `
58
85
attributes:
59
86
60
87
``` ts
61
88
const writeAddress = cluster .clusterEndpoint .socketAddress ; // "HOSTNAME:PORT"
62
89
```
63
90
91
+ For an instance database:
92
+ ``` ts
93
+ const address = instance .instanceEndpoint .socketAddress ; // "HOSTNAME:PORT"
94
+ ```
95
+
64
96
### Rotating master password
65
97
When the master password is generated and stored in AWS Secrets Manager, it can be rotated automatically:
66
98
67
- [ example of setting up master password rotation] ( test/integ.cluster-rotation.lit.ts )
99
+ [ example of setting up master password rotation for a cluster ] ( test/integ.cluster-rotation.lit.ts )
68
100
69
101
Rotation of the master password is also supported for an existing cluster:
70
102
``` ts
71
- new RotationSingleUser (stack , ' Rotation' , {
103
+ new SecretRotation (stack , ' Rotation' , {
72
104
secret: importedSecret ,
73
- engine: DatabaseEngine . Oracle ,
74
- target: importedCluster ,
105
+ application: SecretRotationApplication . OracleRotationSingleUser
106
+ target : importedCluster , // or importedInstance
75
107
vpc: importedVpc ,
76
108
})
77
109
```
@@ -87,3 +119,13 @@ The `importedSecret` must be a JSON string with the following format:
87
119
"port" : " <optional: if not specified, default port will be used>"
88
120
}
89
121
```
122
+
123
+ ### Metrics
124
+ Database instances expose metrics (` cloudwatch.Metric ` ):
125
+ ``` ts
126
+ // The number of database connections in use (average over 5 minutes)
127
+ const dbConnections = instance .metricDatabaseConnections ();
128
+
129
+ // The average amount of time taken per disk I/O operation (average over 1 minute)
130
+ const readLatency = instance .metric (' ReadLatency' , { statistic: ' Average' , periodSec: 60 });
131
+ ```
0 commit comments