Skip to content

Commit

Permalink
feat: add arnForTasks method
Browse files Browse the repository at this point in the history
  • Loading branch information
badmintoncryer committed Jul 24, 2023
1 parent 1df243a commit e3ed216
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/lib/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
14 changes: 14 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit e3ed216

Please sign in to comment.