This is a developer preview (public beta) module. Releases might lack important features and might have future breaking changes.
This API is still under active development and subject to non-backward compatible changes or removal in any future version. Use of the API is not recommended in production environments. Experimental APIs are not subject to the Semantic Versioning model.
To set up a clustered database (like Aurora), 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', {
engine: DatabaseClusterEngine.AURORA,
masterUser: {
username: 'admin'
},
instanceProps: {
instanceType: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpcSubnets: {
subnetType: ec2.SubnetType.PUBLIC,
},
vpc
}
});
By default, the master password will be generated and stored in AWS Secrets Manager.
Your cluster will be empty by default. To add a default database upon construction, specify the
defaultDatabaseName
attribute.
To set up a instance database, define a DatabaseInstance
. 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 instance = new DatabaseInstance(stack, 'Instance', {
engine: rds.DatabaseInstanceEngine.ORACLE_SE1,
instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
masterUsername: 'syscdk',
vpc
});
By default, the master password will be generated and stored in AWS Secrets Manager.
Use DatabaseInstanceFromSnapshot
and DatabaseInstanceReadReplica
to create an instance from snapshot or
a source database respectively:
new DatabaseInstanceFromSnapshot(stack, 'Instance', {
snapshotIdentifier: 'my-snapshot',
engine: rds.DatabaseInstanceEngine.POSTGRES,
instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.LARGE),
vpc
});
new DatabaseInstanceReadReplica(stack, 'ReadReplica', {
sourceDatabaseInstance: sourceInstance,
engine: rds.DatabaseInstanceEngine.POSTGRES,
instanceClass: ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.LARGE),
vpc
});
Creating a "production" Oracle database instance with option and parameter groups:
example of setting up a production oracle instance
To define Amazon CloudWatch event rules for database instances, use the onEvent
method:
const rule = instance.onEvent('InstanceEvent', { target: new targets.LambdaFunction(fn) });
To control who can access the cluster or instance, use the .connections
attribute. RDS databases have
a default port, so you don't need to specify the port:
cluster.connections.allowFromAnyIpv4('Open to the world');
The endpoints to access your database cluster will be available as the .clusterEndpoint
and .readerEndpoint
attributes:
const writeAddress = cluster.clusterEndpoint.socketAddress; // "HOSTNAME:PORT"
For an instance database:
const address = instance.instanceEndpoint.socketAddress; // "HOSTNAME:PORT"
When the master password is generated and stored in AWS Secrets Manager, it can be rotated automatically:
example of setting up master password rotation for a cluster
Rotation of the master password is also supported for an existing cluster:
new SecretRotation(stack, 'Rotation', {
secret: importedSecret,
application: SecretRotationApplication.ORACLE_ROTATION_SINGLE_USER
target: importedCluster, // or importedInstance
vpc: importedVpc,
})
The importedSecret
must be a JSON string with the following format:
{
"engine": "<required: database engine>",
"host": "<required: instance host name>",
"username": "<required: username>",
"password": "<required: password>",
"dbname": "<optional: database name>",
"port": "<optional: if not specified, default port will be used>"
}
Database instances expose metrics (cloudwatch.Metric
):
// The number of database connections in use (average over 5 minutes)
const dbConnections = instance.metricDatabaseConnections();
// The average amount of time taken per disk I/O operation (average over 1 minute)
const readLatency = instance.metric('ReadLatency', { statistic: 'Average', periodSec: 60 });