aws-ecs-patterns错误:此服务的群集需要Ec2容量。在集群上调用addXxxCapacity()

时间:2020-05-05 07:19:12

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

根据AWS CDK文档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()。

这是我的代码... (我希望内森·佩克能看到这一点)

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 }

1 个答案:

答案 0 :(得分: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

时将该群集作为属性传递

希望这会有所帮助!