0

我正在尝试在现有 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 定义的某处定义要使用的子网?

4

1 回答 1

1

默认情况下,它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'
});
于 2019-04-11T15:39:47.883 回答