Creating a new ecs.FargateService with specific subnet-ids via its vpcSubnets: ec2.SubnetSelection property fails even when only subnet-id is required.
Note: we have an environment constraint and can't use vpc.fromLookup() during CDK Synth to use CDK Context and automatically load all subnets and their attributes.
Due to environment constraints, our AWS CDK Construct must depend on existing CloudFormation Stack Exported Outputs in the AWS Environment.
// Create FargateService with default `networkConfiguration`
const svc = new ecs.FargateService(this, "FargateService", {
taskDefinition,
cluster: this.ecsCluster,
desiredCount: this.data.desired,
// Use default subnetSelection ...
// Public subnets if `assignPublicIp` is set, otherwise the first available one of Private, Isolated, Public, in that order.
// vpcSubnets: this.vpc.selectSubnets({
// subnets: privateSubnets,
// }),
// required, else CDK creates new SecurityGroup
securityGroups: [this.securityGroup],
});
// Use AWS CDK escape hatch to override networkConfiguration without `vpc.selectSubnets` limitations....
// https://docs.aws.amazon.com/cdk/v2/guide/cfn_layer.html#develop-customize-escape-l2
const cfnSvc = svc.node.defaultChild as ecs.CfnService;
cfnSvc.networkConfiguration = {
awsvpcConfiguration: {
subnets: Fn.split(",", privateSubnetsRef),
securityGroups: [Fn.ref("SecurityGroup")],
},
};
Expected Behavior
Providing the ecsFargateService an explicit list of SubnetIds should not require the Availability Zone attribute per Subnet ... (the bug is caused by - L480:
You cannot reference a Subnet's availability zone if it was not supplied. Add the availabilityZone when importing using Subnet.fromSubnetAttributes()
236 | // ecs.FargateService -> launchType == "FARGATE"
237 | // TODO: is ServiceName: &configCode, required?
> 238 | const svc = new ecs.FargateService(this, "FargateService", {
| ^
239 | taskDefinition,
240 | cluster: this.ecsCluster,
241 | desiredCount: this.data.desired,
at ImportedSubnet.get availabilityZone [as availabilityZone] (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.js:1:35138)
at node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.js:1:2861
at Array.map (<anonymous>)
at Object.get availabilityZones [as availabilityZones] (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.js:1:2852)
at ImportedVpc.reifySelectionDefaults (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.js:1:7431)
at ImportedVpc.selectSubnetObjects (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.js:1:4733)
at ImportedVpc.selectSubnets (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ec2/lib/vpc.js:1:2705)
at FargateService.configureAwsVpcNetworkingWithSecurityGroups (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ecs/lib/base/base-service.js:1:25843)
at new FargateService (node_modules/.pnpm/aws-cdk-lib@2.140.0_constructs@10.3.0/node_modules/aws-cdk-lib/aws-ecs/lib/fargate/fargate-service.js:1:3382)
at new EcsService (src/index.ts:238:19)
at Object.<anonymous> (test/ecs-service-stack.test.ts:113:5)
Describe the bug
Creating a new
ecs.FargateService
with specificsubnet-ids
via itsvpcSubnets: ec2.SubnetSelection
property fails even when onlysubnet-id
is required.Due to environment constraints, our AWS CDK Construct must depend on existing CloudFormation Stack Exported Outputs in the AWS Environment.
Sample code:
Work Around:
Expected Behavior
Providing the ecsFargateService an explicit list of SubnetIds should not require the Availability Zone attribute per Subnet ... (the bug is caused by -
L480
:https://github.com/aws/aws-cdk/blob/46168aac07c0d3f50ad10c31801751d083474081/packages/aws-cdk-lib/aws-ec2/lib/vpc.ts#L474-L486
Current Behavior
Error is thrown
Reproduction Steps
Possible Solution
Either:
ecs.BaseService
allows passing in Subnets byISubnet[]
, orvpc.selectSubnets()
should not error if theavailabilityZone
attribute does not exist on the subnet?Additional Information/Context
No response
CDK CLI Version
2.140.0
Framework Version
2.140.0
Node.js Version
20.12.2
OS
WSL
Language
TypeScript
Language Version
5.3.3
Other information
No response