aws / aws/aws-cdk

(AWS EFS): (Circular Dependencies when trying to grant perms to imported task role for efs filesystem and access point)

Open
#27,690 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-efs bug effort/medium p3
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

When defining an EFS filesystem and an access point, with an imported task role that needs access to the filesystem. The template generated warns of circular dependencies for all infra defined in that stack.

Example code:

```typescript
const taskRole = iam.Role.fromRoleArn(this, 'TaskRoleMaster', Fn.importValue(`${props.environmentName}-${props.projectName}-ECSTaskRole`))

const s3Bucket = new s3.Bucket(this, 'BucketEFSSync', {
versioned: true,
})

new s3deploy.BucketDeployment(this, 'DeployEfs', {
sources: [s3deploy.Source.asset('src/resources/efs')],
destinationBucket: s3Bucket,
})

const efsSecurityGroup = new ec2.SecurityGroup(this, 'EfsSecurityGroup', {
vpc: vpcEnvironment.vpc,
})
efsSecurityGroup.connections.allowFrom(efsSecurityGroup, ec2.Port.tcp(2049))

const efsFileSystem = new efs.FileSystem(this, 'EfsFileSystem', {
vpc: vpcEnvironment.vpc,
vpcSubnets: vpcEnvironment.vpc.selectSubnets({ subnetGroupName: 'Application' }),
performanceMode: efs.PerformanceMode.GENERAL_PURPOSE,
encrypted: true,
securityGroup: efsSecurityGroup,
enableAutomaticBackups: true,
})
efsFileSystem.applyRemovalPolicy(RemovalPolicy.DESTROY)

const efsAccessPoint = efsFileSystem.addAccessPoint('AP', {
createAcl: {
ownerGid: '33',
ownerUid: '33',
permissions: '0770',
},
posixUser: {
uid: '33',
gid: '33',
},
path: 'sites/default/files',
})

const efsPolicy = new iam.Policy(this, 'EFS', {
statements: [
new iam.PolicyStatement({
actions: [
'elasticfilesystem:ClientRootAccess',
'elasticfilesystem:ClientWrite',
'elasticfilesystem:ClientMount',
'elasticfilesystem:DescribeMountTargets',
],
resources: [`arn:aws:elasticfilesystem:${Aws.REGION}:${Aws.ACCOUNT_ID}:file-system/${efsFileSystem.fileSystemId}`],
}),
],
})
taskRole.attachInlinePolicy(efsPolicy)
efsFileSystem.grant(taskRole, 'elasticfilesystem:ClientWrite')
efsFileSystem.grant(taskRole, 'elasticfilesystem:ClientRootAccess')
```

Further in the stack is a datasync task between s3 and efs, once generated there is a circular dependency between the filesystem, mount points, task role policy, data sync task.

However, using an inline defined task role does not seem to cause this circular dependency invalidation which is a quick fix for now but not one I would like to use permanently.

### Expected Behavior

EFS filesystem and access point created, imported ecs task role has read/write and root access to efs and the inverse.

### Current Behavior

Template generated when put in validator or deployment attempted returns circular dependencies error for EFS filesystem, mount points, access point, Datasync task, Task role policy.

### Reproduction Steps

```typescript
const taskRole = iam.Role.fromRoleArn(this, 'TaskRoleMaster', Fn.importValue(`${props.environmentName}-${props.projectName}-ECSTaskRole`))

const efsSecurityGroup = new ec2.SecurityGroup(this, 'EfsSecurityGroup', {
vpc: vpcEnvironment.vpc,
})
efsSecurityGroup.connections.allowFrom(efsSecurityGroup, ec2.Port.tcp(2049))

const efsFileSystem = new efs.FileSystem(this, 'EfsFileSystem', {
vpc: vpcEnvironment.vpc,
vpcSubnets: vpcEnvironment.vpc.selectSubnets({ subnetGroupName: 'Application' }),
performanceMode: efs.PerformanceMode.GENERAL_PURPOSE,
encrypted: true,
securityGroup: efsSecurityGroup,
enableAutomaticBackups: true,
})
efsFileSystem.applyRemovalPolicy(RemovalPolicy.DESTROY)

const efsAccessPoint = efsFileSystem.addAccessPoint('AP', {
createAcl: {
ownerGid: '33',
ownerUid: '33',
permissions: '0770',
},
posixUser: {
uid: '33',
gid: '33',
},
path: 'sites/default/files',
})

const efsPolicy = new iam.Policy(this, 'EFS', {
statements: [
new iam.PolicyStatement({
actions: [
'elasticfilesystem:ClientRootAccess',
'elasticfilesystem:ClientWrite',
'elasticfilesystem:ClientMount',
'elasticfilesystem:DescribeMountTargets',
],
resources: [`arn:aws:elasticfilesystem:${Aws.REGION}:${Aws.ACCOUNT_ID}:file-system/${efsFileSystem.fileSystemId}`],
}),
],
})
taskRole.attachInlinePolicy(efsPolicy)
efsFileSystem.grant(taskRole, 'elasticfilesystem:ClientWrite')
efsFileSystem.grant(taskRole, 'elasticfilesystem:ClientRootAccess')
```

### Possible Solution

_No response_

### Additional Information/Context

_No response_

### CDK CLI Version

2.102.0

### Framework Version

_No response_

### Node.js Version

18.14.2

### OS

Ubuntu

### Language

TypeScript

### Language Version

_No response_

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by synthesizing the reproduction with CDK CLI 2.102.0 and inspect the generated template's dependency graph. Trace dependencies among the EFS filesystem, mount points, access point, imported task-role policy, and DataSync task; done means the stack validates or deploys without a circular dependency while preserving the stated EFS permissions.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure, security
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.