From 80e67d449b614b066380738361652c47de847614 Mon Sep 17 00:00:00 2001 From: benfradet Date: Fri, 16 Feb 2024 11:41:56 +0100 Subject: [PATCH] feat(rds): add clusterArn property to DatabaseCluster --- .../aws-cdk-lib/aws-rds/lib/cluster-ref.ts | 5 +++ packages/aws-cdk-lib/aws-rds/lib/cluster.ts | 12 +++++++ .../aws-cdk-lib/aws-rds/test/cluster.test.ts | 32 +++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/packages/aws-cdk-lib/aws-rds/lib/cluster-ref.ts b/packages/aws-cdk-lib/aws-rds/lib/cluster-ref.ts index 796e05d978573..a55a0c4f52396 100644 --- a/packages/aws-cdk-lib/aws-rds/lib/cluster-ref.ts +++ b/packages/aws-cdk-lib/aws-rds/lib/cluster-ref.ts @@ -50,6 +50,11 @@ export interface IDatabaseCluster extends IResource, ec2.IConnectable, secretsma */ readonly engine?: IClusterEngine; + /** + * The ARN of the database cluster + */ + readonly clusterArn: string; + /** * Add a new db proxy to this cluster. */ diff --git a/packages/aws-cdk-lib/aws-rds/lib/cluster.ts b/packages/aws-cdk-lib/aws-rds/lib/cluster.ts index b0a062776c496..b5d531f2dc7d9 100644 --- a/packages/aws-cdk-lib/aws-rds/lib/cluster.ts +++ b/packages/aws-cdk-lib/aws-rds/lib/cluster.ts @@ -457,6 +457,18 @@ export abstract class DatabaseClusterBase extends Resource implements IDatabaseC */ public abstract readonly connections: ec2.Connections; + /** + * The ARN of the cluster + */ + public get clusterArn(): string { + return Stack.of(this).formatArn({ + service: 'rds', + resource: 'cluster', + arnFormat: ArnFormat.COLON_RESOURCE_NAME, + resourceName: this.clusterIdentifier, + }); + } + /** * Add a new db proxy to this cluster. */ diff --git a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts index 585cd870181a3..b368b79e6836c 100644 --- a/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-rds/test/cluster.test.ts @@ -4052,6 +4052,38 @@ describe('cluster', () => { ], }); }); + + test('clusterArn property', () => { + // GIVEN + const stack = testStack(); + const vpc = ec2.Vpc.fromLookup(stack, 'VPC', { isDefault: true }); + const cluster = new DatabaseCluster(stack, 'Database', { + engine: DatabaseClusterEngine.auroraPostgres({ version: AuroraPostgresEngineVersion.VER_14_3 }), + instanceProps: { vpc }, + }); + const exportName = 'DbCluterArn'; + + // WHEN + new cdk.CfnOutput(stack, exportName, { + exportName, + value: cluster.clusterArn, + }); + + // THEN + expect( + stack.resolve(cluster.clusterArn), + ).toEqual({ + 'Fn::Join': [ + '', + [ + 'arn:', + { Ref: 'AWS::Partition' }, + ':rds:us-test-1:12345:cluster:', + { Ref: 'DatabaseB269D8BB' }, + ], + ], + }); + }); }); test.each([