Chun Young
Chun Young

Reputation: 93

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

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? The reason I am asking because I can add an option to specify the AMI with a name, but did not see any option for launch configuration, I have some customised configure in the launch configuration.

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,
      });

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.

const autoScalingGroup = new autoscaling.CfnAutoScalingGroup(this, 'myASG', {
        instanceType: new ec2.InstanceType("t3.medium"),
        desiredCapacity: 12,
        minCapacity: 1,
        maxCapacity: 12,
        launchConfigurationName: myASGLaunchConfigurationName,
      });

const capacityProvider = new ecs.AsgCapacityProvider(this, 'AsgCapacityProvider', { autoScalingGroup, machineImageType: ecs.MachineImageType.BOTTLEROCKET });

this.cluster.addAsgCapacityProvider(capacityProvider, { machineImageType: ecs.MachineImageType.BOTTLEROCKET });

Upvotes: 2

Views: 1212

Answers (2)

gshpychka
gshpychka

Reputation: 11492

This is currently not supported in CDK. See this issue: https://github.com/aws/aws-cdk/issues/1403

Upvotes: 1

LRutten
LRutten

Reputation: 1902

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

Upvotes: 0

Related Questions