From 2f720a1d4cea7b23efdab7f0031eacc1914a9c54 Mon Sep 17 00:00:00 2001 From: Ben Fradet Date: Thu, 29 Feb 2024 21:00:34 +0100 Subject: [PATCH] chore(rds): add clusterArn property to DatabaseCluster (#29133) ### Issue # (if applicable) ### Reason for this change This is to bring parity with #10741, we need the cluster arn to enable data api manually waiting for #28574. ### Description of changes This adds a clusterArn property to IDatabaseCluster ### Description of how you validated changes The changes have been validated through a unit test ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../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([