我正在尝试在现有VPC上创建Fargate ECS服务,但在运行cdk deploy
后出现以下错误
CREATE_FAILED | AWS::ElasticLoadBalancingV2::LoadBalancer | exms-service/LB (exmsserviceLB259DA1C7) At least two subnets in two different Availability Zones must be specified (Service: AmazonElasticLoadBalancingV2; Status Code: 400; Error Code: ValidationError;
正确导入vpc,并根据cloudformation日志成功创建ecs集群。在创建负载平衡器时发生故障
const vpc = ec2.VpcNetwork.import(this, "TB-DEV", {
vpcId: 'vpc-xxxxxx',
availabilityZones: ['eu-west-1G', 'eu-west-1b', 'eu-west-1c'],
privateSubnetIds: ['subnet-xxxxxxx', 'subnet-xxxxx', 'subnet-xxxx', 'subnet-xxxxx', 'subnet-xxxxx', 'subnet-xxxxx']
//this is a list of 1 private and 1 public subnet on each of the specified availability zones
})
const cluster = new ecs.Cluster(this, "TB-ECS-DEV", {
clusterName: "TB-DEV",
vpc: vpc,
})
const repo = ecr.Repository.import(this, 'EXMS-REPO', {
repositoryName: "expense-type-mapper-dev"
})
new ecs.LoadBalancedFargateService(this, "EXMS", {
cluster: cluster,
image:ecs.ContainerImage.fromEcrRepository(repo),
})
我希望负载均衡器能够利用VPC定义中指定的子网,但似乎并未发生这种情况。我是否需要在LoadBalancedFargateService定义中的某处定义哪些子网?
默认情况下,LoadBalancedFargateService
会创建面向Internet的应用程序负载均衡器,但您不会在导入中指定公共子网。
此外,在导入VPC时,privateSubnetIds
/ publicSubnetIds
必须与可用区域的长度和顺序完全匹配。
const vpc = ec2.VpcNetwork.import(this, "TB-DEV", {
vpcId: 'vpc-xxxxxx',
availabilityZones: ['eu-west-1a', 'eu-west-1b', 'eu-west-1c'],
publicSubnetIds: ['subnet-xxxxx', 'subnet-xxxxx', 'subnet-xxxxx'],
privateSubnetIds: ['subnet-xxxxxxx', 'subnet-xxxxx', 'subnet-xxxx']
});
另一种解决方案是使用importFromContext
进行API调用以收集VPC的正确信息:
const vpc = ec2.VpcNetwork.importFromContext(this, "TB-DEV", {
vpcId: 'vpc-xxxxxx'
});