Skip to content

Commit 7635bbc

Browse files
feat(ecs): Implement method in ECS cluster to retrieve task ARN (#28381)
This pull request introduces a new method within the Cluster class, designed to retrieve the Amazon Resource Names (ARNs) of tasks that are part of a given ECS cluster. Example of how to get task ARN ```ts declare cluster: Cluster; // arn:aws:ecs:{region}:{regionId}:task/{clusterName}/* const taskArnPattern = cluster.arnForTasks("*"); ``` Closes #26232 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 0932027 commit 7635bbc

File tree

3 files changed

+71
-1
lines changed

3 files changed

+71
-1
lines changed

packages/aws-cdk-lib/aws-ecs/README.md

+17-1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,22 @@ const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider'
186186
cluster.addAsgCapacityProvider(capacityProvider);
187187
```
188188

189+
The following code retrieve the Amazon Resource Names (ARNs) of tasks that are a part of a specified ECS cluster.
190+
It's useful when you want to grant permissions to a task to access other AWS resources.
191+
192+
```ts
193+
declare const cluster: ecs.Cluster;
194+
declare const taskDefinition: ecs.TaskDefinition;
195+
const taskARNs = cluster.arnForTasks('*'); // arn:aws:ecs:<region>:<regionId>:task/<clusterName>/*
196+
197+
// Grant the task permission to access other AWS resources
198+
taskDefinition.addToTaskRolePolicy(
199+
new iam.PolicyStatement({
200+
actions: ['ecs:UpdateTaskProtection'],
201+
resources: [taskARNs],
202+
})
203+
)
204+
```
189205

190206
### Bottlerocket
191207

@@ -1600,4 +1616,4 @@ taskDefinition.addContainer('TheContainer', {
16001616
softLimit: 128,
16011617
}],
16021618
});
1603-
```
1619+
```

packages/aws-cdk-lib/aws-ecs/lib/cluster.ts

+15
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,21 @@ export class Cluster extends Resource implements ICluster {
583583
}
584584
}
585585

586+
/**
587+
* Returns an ARN that represents all tasks within the cluster that match
588+
* the task pattern specified. To represent all tasks, specify ``"*"``.
589+
*
590+
* @param keyPattern Task id pattern
591+
*/
592+
public arnForTasks(keyPattern: string): string {
593+
return Stack.of(this).formatArn({
594+
service: 'ecs',
595+
resource: 'task',
596+
resourceName: `${this.clusterName}/${keyPattern}`,
597+
arnFormat: ArnFormat.SLASH_RESOURCE_NAME,
598+
});
599+
}
600+
586601
private configureWindowsAutoScalingGroup(autoScalingGroup: autoscaling.AutoScalingGroup, options: AddAutoScalingGroupCapacityOptions = {}) {
587602
// clear the cache of the agent
588603
autoScalingGroup.addUserData('Remove-Item -Recurse C:\\ProgramData\\Amazon\\ECS\\Cache');

packages/aws-cdk-lib/aws-ecs/test/cluster.test.ts

+39
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { testDeprecated } from '@aws-cdk/cdk-build-tools';
22
import { Match, Template } from '../../assertions';
33
import * as autoscaling from '../../aws-autoscaling';
44
import * as ec2 from '../../aws-ec2';
5+
import * as iam from '../../aws-iam';
56
import * as kms from '../../aws-kms';
67
import * as logs from '../../aws-logs';
78
import * as s3 from '../../aws-s3';
@@ -1084,6 +1085,44 @@ describe('cluster', () => {
10841085
expect(cluster.defaultCloudMapNamespace!.namespaceName).toBe('foo');
10851086
});
10861087

1088+
test('arnForTasks returns a task arn from key pattern', () => {
1089+
// GIVEN
1090+
const stack = new cdk.Stack();
1091+
const vpc = new ec2.Vpc(stack, 'MyVpc', {});
1092+
const cluster = new ecs.Cluster(stack, 'EcsCluster', { vpc });
1093+
const taskIdPattern = '*';
1094+
1095+
// WHEN
1096+
const policyStatement = new iam.PolicyStatement({
1097+
resources: [cluster.arnForTasks(taskIdPattern)],
1098+
actions: ['ecs:RunTask'],
1099+
principals: [new iam.ServicePrincipal('ecs.amazonaws.com')],
1100+
});
1101+
1102+
// THEN
1103+
expect(stack.resolve(policyStatement.toStatementJson())).toEqual({
1104+
Action: 'ecs:RunTask',
1105+
Effect: 'Allow',
1106+
Principal: { Service: 'ecs.amazonaws.com' },
1107+
Resource: {
1108+
'Fn::Join': [
1109+
'',
1110+
[
1111+
'arn:',
1112+
{ Ref: 'AWS::Partition' },
1113+
':ecs:',
1114+
{ Ref: 'AWS::Region' },
1115+
':',
1116+
{ Ref: 'AWS::AccountId' },
1117+
':task/',
1118+
{ Ref: 'EcsCluster97242B84' },
1119+
`/${taskIdPattern}`,
1120+
],
1121+
],
1122+
},
1123+
});
1124+
});
1125+
10871126
/*
10881127
* TODO:v2.0.0 END OF OBSOLETE BLOCK
10891128
*/

0 commit comments

Comments
 (0)