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

feat(ecs): give cluster a method that provides task arn #26453

Closed
wants to merge 3 commits 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
9 changes: 9 additions & 0 deletions packages/aws-cdk-lib/aws-ecs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:<region>:<account>:task/<clusterName>/*
```


### Bottlerocket

Expand Down
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
Loading