Home > Net >  How can I create a AWS cluster by specifying the launch configuration using CDK
How can I create a AWS cluster by specifying the launch configuration using CDK

Time:02-17

I am using aws cdk to create my cluster. From the console I can define the autoscaling group relying on a launch template/configuration which points to a AMI.

How can I do that with AWS CDK?

this.cluster = new ecs.Cluster(this, "myCluster", {
      vpc: this.vpc,
    });

this.cluster.addCapacity("myASG", {
        instanceType: new ec2.InstanceType("t3.medium"),
        desiredCapacity: 8,
        minCapacity: 1,
        maxCapacity: 8,
      });

CodePudding user response:

I am wondering if this is a good way?

create an autoScallingGroup by specifying the launch configuration name, and create a capacity provider by using the autoScalingGroup, add the capcity provider to the cluster.

CodePudding user response:

If you want full control over the autoscaling group configuration, create the ASG and add it to the cluster.

// Or add customized capacity. Be sure to start the Amazon ECS-optimized AMI.
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'ASG', {
  vpc,
  instanceType: new ec2.InstanceType('t2.xlarge'),
  machineImage: ecs.EcsOptimizedImage.amazonLinux(),
  // Or use Amazon ECS-Optimized Amazon Linux 2 AMI
  // machineImage: EcsOptimizedImage.amazonLinux2(),
  desiredCapacity: 3,
  // ... other options here ...
});

cluster.addAutoScalingGroup(autoScalingGroup);

Example taken from; https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ecs.AddCapacityOptions.html

  • Related