From e3ed21661ce2c61e019d6970790b4c2d475cc2a0 Mon Sep 17 00:00:00 2001 From: Kazuho CryerShinozuka Date: Thu, 20 Jul 2023 23:08:31 +0900 Subject: [PATCH 1/2] feat: add arnForTasks method --- packages/aws-cdk-lib/aws-ecs/lib/cluster.ts | 10 ++++++++++ packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts | 14 ++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/packages/aws-cdk-lib/aws-ecs/lib/cluster.ts b/packages/aws-cdk-lib/aws-ecs/lib/cluster.ts index d4193aac5834d..c3e7813b45ba2 100644 --- a/packages/aws-cdk-lib/aws-ecs/lib/cluster.ts +++ b/packages/aws-cdk-lib/aws-ecs/lib/cluster.ts @@ -583,6 +583,16 @@ export class Cluster extends Resource implements ICluster { } } + /** + * Returns an ARN that represents all tasks within the cluster that match + * the task pattern specified. To represent all tasks, specify ``"*"``. + * + * @param keyPattern Task id pattern + */ + public arnForTasks(keyPattern: string): string { + return this.clusterArn.replace(/cluster\/(.*)$/, `task/$1/${keyPattern}`); + } + private configureWindowsAutoScalingGroup(autoScalingGroup: autoscaling.AutoScalingGroup, options: AddAutoScalingGroupCapacityOptions = {}) { // clear the cache of the agent autoScalingGroup.addUserData('Remove-Item -Recurse C:\\ProgramData\\Amazon\\ECS\\Cache'); diff --git a/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts b/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts index a05083dcc0a5c..52f655a7c8c28 100644 --- a/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts +++ b/packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts @@ -1055,6 +1055,20 @@ describe('cluster', () => { expect(cluster.defaultCloudMapNamespace!.namespaceName).toBe('foo'); }); + test('arnForTasks returns a task arn from key pattern', () => { + // GIVEN + const stack = new cdk.Stack(); + const vpc = new ec2.Vpc(stack, 'MyVpc', {}); + const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc }); + const taskIdPattern = 'taskIdPattern'; + + // THEN + const taskArn = cluster.arnForTasks(taskIdPattern); + expect(taskArn).toEqual( + `arn:aws:ecs:${cluster.env.region}:${cluster.env.account}:task/${cluster.clusterName}/${taskIdPattern}`, + ); + }); + /* * TODO:v2.0.0 END OF OBSOLETE BLOCK */ From 4af6f02889600b6a640a6e40895be664bb450766 Mon Sep 17 00:00:00 2001 From: Kazuho CryerShinozuka Date: Mon, 24 Jul 2023 23:09:41 +0900 Subject: [PATCH 2/2] fix: update README.md --- packages/aws-cdk-lib/aws-ecs/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/packages/aws-cdk-lib/aws-ecs/README.md b/packages/aws-cdk-lib/aws-ecs/README.md index c050947d2c93e..671662c4fb113 100644 --- a/packages/aws-cdk-lib/aws-ecs/README.md +++ b/packages/aws-cdk-lib/aws-ecs/README.md @@ -186,6 +186,15 @@ const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider' cluster.addAsgCapacityProvider(capacityProvider); ``` +It is also possible to obtain the ARN of a task based on the key pattern from Cluster. + +```ts +const cluster = new ecs.Cluster(this, 'Cluster', { + vpc, +}); +const taskArn = cluster.arnForTasks('*') // arn:aws:ecs:::task//* +``` + ### Bottlerocket