【问题标题】:aws-ecs-patterns error: Cluster for this service needs Ec2 capacity. Call addXxxCapacity() on the clusteraws-ecs-patterns 错误:此服务的集群需要 Ec2 容量。在集群上调用 addXxxCapacity()
【发布时间】:2020-08-19 19:07:30
【问题描述】:

希望有人可以在这里帮助我,根据 AWS CDK 文档 if I declare my VPC then I shouldn't declare 'capacity',但是当我运行 cdk synth 时出现以下错误...

抛出新错误(Validation failed with the following errors:\n ${errorList});

错误:验证失败,出现以下错误: [PrerenderInfrasctutureStack/preRenderApp/Service] 此服务的集群需要 Ec2 容量。在集群上调用 addXxxCapacity()。

这是我的代码... (我希望 Nathan Peck 看到这一点)

const ec2 = require('@aws-cdk/aws-ec2');
const ecsPattern = require('@aws-cdk/aws-ecs-patterns');
const ecs = require('@aws-cdk/aws-ecs');
class PrerenderInfrasctutureStack extends cdk.Stack {
  /**
   *
   * @param {cdk.Construct} scope
   * @param {string} id
   * @param {cdk.StackProps=} props
   */


  constructor(scope, id, props) {
    super(scope, id, props);

    const myVPC = ec2.Vpc.fromLookup(this, 'publicVpc', {
      vpcId:'vpc-xxx'
    });


    const preRenderApp = new ecsPattern.ApplicationLoadBalancedEc2Service(this, 'preRenderApp', {
      vpcId: myVPC,
      certificate: 'arn:aws:acm:ap-southeast-2:xxx:certificate/xxx', //becuase this is spcified, then the LB will automatically use HTTPS
      domainName: 'my-dev.com.au.',
      domainZone:'my-dev.com.au',
      listenerPort: 443,
      publicLoadBalancer: true,
      memoryReservationMiB: 8,
      cpu: 4096,
      desiredCount: 1,
      taskImageOptions:{
        image: ecs.ContainerImage.fromRegistry('xxx.dkr.ecr.region.amazonaws.com/express-prerender-server'), 
        containerPort: 3000
      },
    });


  }

}


module.exports = { PrerenderInfrasctutureStack }

【问题讨论】:

    标签: amazon-ecs aws-cdk infrastructure-as-code


    【解决方案1】:

    这是因为如果您没有明确传递集群,那么它会使用您帐户中存在的默认集群。然而,默认集群一开始没有 EC2 容量,因为 EC2 实例在运行时需要花钱。您可以在 Fargate 模式下使用空的默认集群,因为 Fargate 不需要 EC2 容量,它只是在 Fargate 内运行您的容器,但在您将 EC2 实例添加到集群之前,默认集群将无法使用 EC2 模式。

    这里的简单解决方案是改用ApplicationLoadBalancedFargateService,因为 Fargate 服务使用 Fargate 容量运行,因此它们不需要集群中的 EC2 实例。或者,您应该使用以下内容定义自己的集群:

    // Create an ECS cluster
    const cluster = new ecs.Cluster(this, 'Cluster', {
      vpc,
    });
    
    // Add capacity to it
    cluster.addCapacity('DefaultAutoScalingGroupCapacity', {
      instanceType: new ec2.InstanceType("t2.xlarge"),
      desiredCapacity: 3,
    });
    

    然后在创建 ApplicationLoadBalancedEc2Service 时将该集群作为属性传递

    希望这会有所帮助!

    【讨论】:

    • 嗨,Nathan,这解决了我的问题,谢谢。你说的其实很有道理。 reading the doco again 我明白我为什么感到困惑了,我想我必须指定一个或另一个(集群或 vpc),所以我假设会创建容量。也许需要审查的东西,或者它可能只是我。再次感谢您的帮助!
    猜你喜欢
    • 2020-12-11
    • 1970-01-01
    • 2021-12-02
    • 2022-09-30
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2020-03-23
    • 2023-03-30
    相关资源
    最近更新 更多