Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rds): Add engine to IDatabaseInstance, IClusterInstance #9257

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/@aws-cdk/aws-rds/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ const instance = new rds.DatabaseInstance(this, 'Instance', {
});
```

To import an already existing instance:

```ts
rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Database', {
engine: rds.DatabaseInstanceEngine.mysql({ version: rds.MysqlEngineVersion.VER_5_7_30 }),
instanceEndpointAddress: 'address',
instanceIdentifier: 'identifier',
port: 3306,
securityGroups: [ec2.SecurityGroup.fromSecurityGroupId(stack, 'SG', 'sg-123456789', {
allowAllOutbound: false,
})],
});
```

Use `DatabaseInstanceFromSnapshot` and `DatabaseInstanceReadReplica` to create an instance from snapshot or
a source database respectively:

Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/cluster-ref.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import * as ec2 from '@aws-cdk/aws-ec2';
import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import { IResource } from '@aws-cdk/core';
import { IClusterEngine } from './cluster-engine';
import { Endpoint } from './endpoint';
import { DatabaseProxy, DatabaseProxyOptions } from './proxy';

/**
* Create a clustered database with a given number of instances.
*/
export interface IDatabaseCluster extends IResource, ec2.IConnectable, secretsmanager.ISecretAttachmentTarget {
/**
* The database engine
*/
readonly engine: IClusterEngine;

/**
* Identifier of the cluster
*/
Expand Down Expand Up @@ -45,6 +51,11 @@ export interface IDatabaseCluster extends IResource, ec2.IConnectable, secretsma
* Properties that describe an existing cluster instance
*/
export interface DatabaseClusterAttributes {
/**
* The database engine
*/
readonly engine: IClusterEngine;

/**
* The database port
*/
Expand Down
11 changes: 11 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ export interface DatabaseClusterProps {
* A new or imported clustered database.
*/
abstract class DatabaseClusterBase extends Resource implements IDatabaseCluster {
/**
* The database engine
*/
public abstract readonly engine: IClusterEngine;
/**
* Identifier of the cluster
*/
Expand Down Expand Up @@ -271,6 +275,7 @@ export class DatabaseCluster extends DatabaseClusterBase {
securityGroups: attrs.securityGroups,
defaultPort: this.defaultPort,
});
public readonly engine = attrs.engine;
public readonly clusterIdentifier = attrs.clusterIdentifier;
public readonly instanceIdentifiers: string[] = [];
public readonly clusterEndpoint = new Endpoint(attrs.clusterEndpointAddress, attrs.port);
Expand All @@ -281,6 +286,11 @@ export class DatabaseCluster extends DatabaseClusterBase {
return new Import(scope, id);
}

/**
* The database engine
*/
public readonly engine: IClusterEngine;

/**
* Identifier of the cluster
*/
Expand Down Expand Up @@ -528,6 +538,7 @@ export class DatabaseCluster extends DatabaseClusterBase {

const defaultPort = ec2.Port.tcp(this.clusterEndpoint.port);
this.connections = new ec2.Connections({ securityGroups, defaultPort });
this.engine = props.engine;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-rds/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { CfnDBInstance, CfnDBInstanceProps, CfnDBSubnetGroup } from './rds.gener
* A database instance
*/
export interface IDatabaseInstance extends IResource, ec2.IConnectable, secretsmanager.ISecretAttachmentTarget {
/**
* The database engine.
*/
readonly engine: IInstanceEngine;

/**
* The instance identifier.
*/
Expand Down Expand Up @@ -64,6 +69,11 @@ export interface IDatabaseInstance extends IResource, ec2.IConnectable, secretsm
* Properties that describe an existing instance
*/
export interface DatabaseInstanceAttributes {
/**
* The database engine.
*/
readonly engine: IInstanceEngine;

/**
* The instance identifier.
*/
Expand Down Expand Up @@ -99,6 +109,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
securityGroups: attrs.securityGroups,
defaultPort: this.defaultPort,
});
public readonly engine = attrs.engine;
public readonly instanceIdentifier = attrs.instanceIdentifier;
public readonly dbInstanceEndpointAddress = attrs.instanceEndpointAddress;
public readonly dbInstanceEndpointPort = attrs.port.toString();
Expand All @@ -108,6 +119,7 @@ export abstract class DatabaseInstanceBase extends Resource implements IDatabase
return new Import(scope, id);
}

public abstract readonly engine: IInstanceEngine;
public abstract readonly instanceIdentifier: string;
public abstract readonly dbInstanceEndpointAddress: string;
public abstract readonly dbInstanceEndpointPort: string;
Expand Down Expand Up @@ -776,6 +788,7 @@ export interface DatabaseInstanceProps extends DatabaseInstanceSourceProps {
* @resource AWS::RDS::DBInstance
*/
export class DatabaseInstance extends DatabaseInstanceSource implements IDatabaseInstance {
public readonly engine: IInstanceEngine;
public readonly instanceIdentifier: string;
public readonly dbInstanceEndpointAddress: string;
public readonly dbInstanceEndpointPort: string;
Expand Down Expand Up @@ -804,6 +817,7 @@ export class DatabaseInstance extends DatabaseInstanceSource implements IDatabas
storageEncrypted: props.storageEncryptionKey ? true : props.storageEncrypted,
});

this.engine = props.engine;
this.instanceIdentifier = instance.ref;
this.dbInstanceEndpointAddress = instance.attrEndpointAddress;
this.dbInstanceEndpointPort = instance.attrEndpointPort;
Expand Down Expand Up @@ -862,6 +876,7 @@ export interface DatabaseInstanceFromSnapshotProps extends DatabaseInstanceSourc
* @resource AWS::RDS::DBInstance
*/
export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource implements IDatabaseInstance {
public readonly engine: IInstanceEngine;
public readonly instanceIdentifier: string;
public readonly dbInstanceEndpointAddress: string;
public readonly dbInstanceEndpointPort: string;
Expand Down Expand Up @@ -900,6 +915,7 @@ export class DatabaseInstanceFromSnapshot extends DatabaseInstanceSource impleme
: props.masterUserPassword && props.masterUserPassword.toString(),
});

this.engine = props.engine;
this.instanceIdentifier = instance.ref;
this.dbInstanceEndpointAddress = instance.attrEndpointAddress;
this.dbInstanceEndpointPort = instance.attrEndpointPort;
Expand Down Expand Up @@ -957,6 +973,7 @@ export interface DatabaseInstanceReadReplicaProps extends DatabaseInstanceNewPro
* @resource AWS::RDS::DBInstance
*/
export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements IDatabaseInstance {
public readonly engine: IInstanceEngine;
public readonly instanceIdentifier: string;
public readonly dbInstanceEndpointAddress: string;
public readonly dbInstanceEndpointPort: string;
Expand All @@ -974,6 +991,7 @@ export class DatabaseInstanceReadReplica extends DatabaseInstanceNew implements
storageEncrypted: props.storageEncryptionKey ? true : props.storageEncrypted,
});

this.engine = props.sourceDatabaseInstance.engine;
this.instanceType = props.instanceType;
this.instanceIdentifier = instance.ref;
this.dbInstanceEndpointAddress = instance.attrEndpointAddress;
Expand Down
12 changes: 6 additions & 6 deletions packages/@aws-cdk/aws-rds/lib/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as secretsmanager from '@aws-cdk/aws-secretsmanager';
import * as cdk from '@aws-cdk/core';
import { IDatabaseCluster } from './cluster-ref';
import { IDatabaseInstance } from './instance';
import { CfnDBCluster, CfnDBInstance, CfnDBProxy, CfnDBProxyTargetGroup } from './rds.generated';
import { CfnDBProxy, CfnDBProxyTargetGroup } from './rds.generated';

/**
* SessionPinningFilter
Expand Down Expand Up @@ -65,17 +65,17 @@ export class ProxyTarget {
* Bind this target to the specified database proxy.
*/
public bind(_: DatabaseProxy): ProxyTargetConfig {
let engine: string | undefined;
let engineType: string | undefined;
if (this.dbCluster && this.dbInstance) {
throw new Error('Proxy cannot target both database cluster and database instance.');
} else if (this.dbCluster) {
engine = (this.dbCluster.node.defaultChild as CfnDBCluster).engine;
engineType = this.dbCluster.engine.engineType;
} else if (this.dbInstance) {
engine = (this.dbInstance.node.defaultChild as CfnDBInstance).engine;
engineType = this.dbInstance.engine.engineType;
}

let engineFamily;
switch (engine) {
switch (engineType) {
case 'aurora':
case 'aurora-mysql':
case 'mysql':
Expand All @@ -86,7 +86,7 @@ export class ProxyTarget {
engineFamily = 'POSTGRESQL';
break;
default:
throw new Error(`Unsupported engine type - ${engine}`);
throw new Error(`Unsupported engine type - ${engineType}`);
}

return {
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-rds/test/test.cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export = {
const stack = testStack();

const cluster = DatabaseCluster.fromDatabaseClusterAttributes(stack, 'Database', {
engine: DatabaseClusterEngine.auroraMysql({ version: AuroraMysqlEngineVersion.VER_2_08_1 }),
clusterEndpointAddress: 'addr',
clusterIdentifier: 'identifier',
instanceEndpointAddresses: ['addr'],
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-rds/test/test.instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export = {
const stack = new cdk.Stack();

const instance = rds.DatabaseInstance.fromDatabaseInstanceAttributes(stack, 'Database', {
engine: rds.DatabaseInstanceEngine.mysql({ version: rds.MysqlEngineVersion.VER_5_7_30 }),
instanceEndpointAddress: 'address',
instanceIdentifier: 'identifier',
port: 3306,
Expand Down