Skip to content

Commit 53060a7

Browse files
mrliklkaiz-ioMichael Kaiser
authored
Added elasticfilesystem:ClientMount to the fargate-service-with-ecs example (#924)
* Added elasticfilesystem:ClientMount to the fargate-service-with-ecs example * Fix: Add grant --------- Co-authored-by: Michael Kaiser <[email protected]> Co-authored-by: Michael Kaiser <[email protected]>
1 parent 025a519 commit 53060a7

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

typescript/ecs/fargate-service-with-efs/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ Is based on this blog post: https://aws.amazon.com/blogs/aws/amazon-ecs-supports
99
To build this app, you need to be in this example's root folder. Then run the following:
1010

1111
```bash
12-
npm install -g aws-cdk
1312
npm install
14-
npm run build
13+
npx cdk synth
1514
```
1615

1716
This will install the necessary CDK, then this example's dependencies, and then build your TypeScript files and your CloudFormation template.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"app": "node index"
2+
"app": "npx ts-node --prefer-ts-exts index.ts"
33
}

typescript/ecs/fargate-service-with-efs/index.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as ec2 from 'aws-cdk-lib/aws-ec2';
33
import * as ecs from 'aws-cdk-lib/aws-ecs';
44
import * as ecs_patterns from 'aws-cdk-lib/aws-ecs-patterns';
55
import * as efs from 'aws-cdk-lib/aws-efs';
6-
6+
import * as iam from 'aws-cdk-lib/aws-iam';
77

88

99
class FargateEfs extends cdk.Stack {
@@ -21,6 +21,18 @@ class FargateEfs extends cdk.Stack {
2121
throughputMode: efs.ThroughputMode.BURSTING
2222
});
2323

24+
fileSystem.addToResourcePolicy(
25+
new iam.PolicyStatement({
26+
actions: ['elasticfilesystem:ClientMount'],
27+
principals: [new iam.AnyPrincipal()],
28+
conditions: {
29+
Bool: {
30+
'elasticfilesystem:AccessedViaMountTarget': 'true'
31+
}
32+
}
33+
})
34+
)
35+
2436
const taskDef = new ecs.FargateTaskDefinition(this, "MyTaskDefinition", {
2537
memoryLimitMiB: 512,
2638
cpu: 256,
@@ -60,6 +72,7 @@ class FargateEfs extends cdk.Stack {
6072
albFargateService.targetGroup.setAttribute('deregistration_delay.timeout_seconds', '30');
6173

6274
// Allow access to EFS from Fargate ECS
75+
fileSystem.grantRootAccess(albFargateService.taskDefinition.taskRole.grantPrincipal);
6376
fileSystem.connections.allowDefaultPortFrom(albFargateService.service.connections);
6477
}
6578
}

0 commit comments

Comments
 (0)