Skip to content

Commit 889d673

Browse files
fix(efs): EFS fails to create when using a VPC with multiple subnets per availability zone (#12097)
Fixes #10170 ### Fixes This PR has been modified so that it does not create a mount target if only VPC is specified in the EFS file system. ### Bug Currently, if a VPC is specified and no subnet is specified, the subnet search criteria are passed as undefined, all subnets in the VPC are retrieved, and mount targets are generated for all subnets in the VPC. I will. Since mount targets can only be created in one subnet for each Availability Zone, the above behavior will result in duplicate Availability Zones between mount targets, resulting in an error when creating the mount target. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 5060782 commit 889d673

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

packages/@aws-cdk/aws-efs/lib/efs-file-system.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ export class FileSystem extends Resource implements IFileSystem {
272272
defaultPort: ec2.Port.tcp(FileSystem.DEFAULT_PORT),
273273
});
274274

275-
const subnets = props.vpc.selectSubnets(props.vpcSubnets);
275+
const subnets = props.vpc.selectSubnets(props.vpcSubnets ?? { onePerAz: true });
276276

277277
// We now have to create the mount target for each of the mentioned subnet
278278
let mountTargetCount = 0;

packages/@aws-cdk/aws-efs/test/efs-file-system.test.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect as expectCDK, haveResource, ResourcePart } from '@aws-cdk/assert';
1+
import { expect as expectCDK, haveResource, ResourcePart, countResources } from '@aws-cdk/assert';
22
import * as ec2 from '@aws-cdk/aws-ec2';
33
import * as kms from '@aws-cdk/aws-kms';
44
import { RemovalPolicy, Size, Stack, Tags } from '@aws-cdk/core';
@@ -240,3 +240,17 @@ test('can specify backup policy', () => {
240240
},
241241
}));
242242
});
243+
244+
test('can create when using a VPC with multiple subnets per availability zone', () => {
245+
// create a vpc with two subnets in the same availability zone.
246+
const oneAzVpc = new ec2.Vpc(stack, 'Vpc', {
247+
maxAzs: 1,
248+
subnetConfiguration: [{ name: 'One', subnetType: ec2.SubnetType.ISOLATED }, { name: 'Two', subnetType: ec2.SubnetType.ISOLATED }],
249+
natGateways: 0,
250+
});
251+
new FileSystem(stack, 'EfsFileSystem', {
252+
vpc: oneAzVpc,
253+
});
254+
// make sure only one mount target is created.
255+
expectCDK(stack).to(countResources('AWS::EFS::MountTarget', 1));
256+
});

0 commit comments

Comments
 (0)