# Amazon Relational Database Service Construct Library
<!--BEGIN STABILITY BANNER-->---
![cfn-resources: Stable](https://img.shields.io/badge/cfn--resources-stable-success.svg?style=for-the-badge)
![cdk-constructs: Stable](https://img.shields.io/badge/cdk--constructs-stable-success.svg?style=for-the-badge)
---
<!--END STABILITY BANNER-->
```python
# Example automatically generated. See https://github.com/aws/jsii/issues/826
import aws_cdk.aws_rds as rds
```
## Starting a clustered database
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:
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
cluster = rds.DatabaseCluster(self, "Database",
engine=rds.DatabaseClusterEngine.aurora_mysql(version=rds.AuroraMysqlEngineVersion.VER_2_08_1),
credentials=rds.Credentials.from_generated_secret("clusteradmin"), # Optional - will default to 'admin' username and generated password
instance_props={
# optional , defaults to t3.medium
"instance_type": ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
"vpc_subnets": {
"subnet_type": ec2.SubnetType.PRIVATE
},
"vpc": vpc
}
)
```
If there isn't a constant for the exact version you want to use,
all of the `Version` classes have a static `of` method that can be used to create an arbitrary version.
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
custom_engine_version = rds.AuroraMysqlEngineVersion.of("5.7.mysql_aurora.2.08.1")
```
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. To add a default database upon construction, specify the
`defaultDatabaseName` attribute.
Use `DatabaseClusterFromSnapshot` to create a cluster from a snapshot:
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
rds.DatabaseClusterFromSnapshot(stack, "Database",
engine=rds.DatabaseClusterEngine.aurora(version=rds.AuroraEngineVersion.VER_1_22_2),
instance_props={
"vpc": vpc
},
snapshot_identifier="mySnapshot"
)
```
## Starting an instance database
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:
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
instance = rds.DatabaseInstance(self, "Instance",
engine=rds.DatabaseInstanceEngine.oracle_se2(version=rds.OracleEngineVersion.VER_19_0_0_0_2020_04_R1),
# optional, defaults to m5.large
instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.SMALL),
credentials=rds.Credentials.from_generated_secret("syscdk"), # Optional - will default to 'admin' username and generated password
vpc=vpc,
vpc_subnets={
"subnet_type": ec2.SubnetType.PRIVATE
}
)
```
If there isn't a constant for the exact engine version you want to use,
all of the `Version` classes have a static `of` method that can be used to create an arbitrary version.
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
custom_engine_version = rds.OracleEngineVersion.of("19.0.0.0.ru-2020-04.rur-2020-04.r1", "19")
```
By default, the master password will be generated and stored in AWS Secrets Manager.
To use the storage auto scaling option of RDS you can specify the maximum allocated storage.
This is the upper limit to which RDS can automatically scale the storage. More info can be found
[here](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_PIOPS.StorageTypes.html#USER_PIOPS.Autoscaling)
Example for max storage configuration:
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
instance = rds.DatabaseInstance(self, "Instance",
engine=rds.DatabaseInstanceEngine.postgres(version=rds.PostgresEngineVersion.VER_12_3),
# optional, defaults to m5.large
instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.SMALL),
vpc=vpc,
max_allocated_storage=200
)
```
Use `DatabaseInstanceFromSnapshot` and `DatabaseInstanceReadReplica` to create an instance from snapshot or
a source database respectively:
```python
# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
rds.DatabaseInstanceFromSnapshot(stack, "Instance",
snapshot_identifier="my-snapshot",
engine=rds.DatabaseInstanceEngine.postgres(version=rds.PostgresEngineVersion.VER_12_3),
# optional, defaults to m5.large
instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.LARGE),
vpc=vpc
)
rds.DatabaseInstanceReadReplica(stack, "ReadReplica",
source_database_instance=source_instance,
instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE2, ec2.InstanceSize.LARGE),
vpc=vpc
)
```
Creating a "production" Oracle database instance with option and parameter groups:
```python
# Example automatically generated. See https://github.com/aws/jsii/issues/826
# Set open cursors with parameter group
parameter_group = rds.ParameterGroup(self, "ParameterGroup",
engine=rds.DatabaseInstanceEngine.oracle_se2(version=rds.OracleEngineVersion.VER_19_0_0_0_2020_04_R1),
parameters={
"open_cursors": "2500"
}
)
option_group = rds.OptionGroup(self, "OptionGroup",
engine=rds.DatabaseInstanceEngine.oracle_se2(version=rds.OracleEngineVersion.VER_19_0_0_0_2020_04_R1),
configurations=[OptionConfiguration(
name="LOCATOR"
), OptionConfiguration(
name="OEM",
port=1158,
vpc=vpc
)
]
)
# Allow connections to OEM
option_group.option_connections.OEM.connections.allow_default_port_from_any_ipv4()
# Database instance with production values
instance = rds.DatabaseInstance(self, "Instance",
engine=rds.DatabaseInstanceEngine.oracle_se2(version=rds.OracleEngineVersion.VER_19_0_0_0_2020_04_R1),
license_model=rds.LicenseModel.BRING_YOUR_OWN_LICENSE,
instance_type=ec2.InstanceType.of(ec2.InstanceClass.BURSTABLE3, ec2.InstanceSize.MEDIUM),
multi_az=True,
storage_type=rds.StorageType.IO1,
credentials=rds.Credentials.from_username("syscdk"),
vpc=vpc,
database_name="ORCL",
storage_encrypted=True,
backup_retention=cdk.Duration.days(7),
monitoring_interval=cdk.Duration.seconds(60),
enable_performance_insights=True,
cloudwatch_logs_exports=["trace", "audit", "alert", "listener"
],
cloudwatch_logs_retention=logs.RetentionDays.ONE_MONTH,
auto_minor_version_upgrade=False,
option_group=option_group,
parameter_group=parameter_group
)
# Allow connections on default port from any IPV4
instance.connections.allow_default_port_from_any_ipv4()
# Rotate the master user password every 30 days
instance.add_rotation_single_user()
# Add alarm for high CPU
cloudwatch.Alarm(self, "HighCPU",
metric=instance.metric_cPUUtilization(),
threshold=90,
evaluation_periods=1
)
# Trigger Lambda function on instance availability events
fn = lambda_.Function(self, "Function",
code=lambda_.Code.from_inline("exports.handler = (event) => console.log(event);"),
handler="index.handler",
runtime=lambda_.Runtime.NODEJS_10_X
)
availability_rule = instance.on_event("Availability", target=targets.LambdaFunction(fn))
availability_rule.add_event_pattern(
detail={
"EventCategories": ["availability"
]
}
)
```
Add XMLDB and OEM with option group
```python
# Example automatically generated. See https:/