Skip to content

fix(aws-ecs): make Cluster.addAsgCapacityProvider() not need specify machineImageType. #16360

@neilkuan

Description

@neilkuan

Now Cluster.addAsgCapacityProvider() need to be specified machineImageType to determine whether it is MachineImageType.AMAZON_LINUX_2 or MachineImageType.BOTTLEROCKET.
But this approach is not very intuitive.

Because when you create AsgCapacityProvider, you already can specify the machineImageType.

example create Bottlerocket autoscaling provider:

const capacityProviderBottlerocket = new ecs.AsgCapacityProvider(stack, 'providerBottlerocket', {
      autoScalingGroup: autoScalingGroupBottlerocket,
      enableManagedTerminationProtection: false,
      machineImageType: ecs.MachineImageType.BOTTLEROCKET, // <- machineImageType
    });

But now you have to specify machineImageType once at Cluster.addAsgCapacityProvider(capacityProviderBottlerocket, { machineImageType: ecs.MachineImageType.BOTTLEROCKET, }) again, which is very unintuitive.

const cluster = new ecs.Cluster(this, 'cluster', { vpc, });

const capacityProviderBottlerocket = new ecs.AsgCapacityProvider(stack, 'providerBottlerocket', {
      autoScalingGroup: autoScalingGroupBottlerocket,
      enableManagedTerminationProtection: false,
      machineImageType: ecs.MachineImageType.BOTTLEROCKET, // <- machineImageType
    });

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

And if you create Bottlerocket autoscaling provider, but forgot to specify machineImageType at Cluster.addAsgCapacityProvider(), Bottlerocket Node will failed to register to ecs cluster.
root case is:

private configureAutoScalingGroup(autoScalingGroup: autoscaling.AutoScalingGroup, options: AddAutoScalingGroupCapacityOptions = {}) {
if (autoScalingGroup.osType === ec2.OperatingSystemType.WINDOWS) {
this.configureWindowsAutoScalingGroup(autoScalingGroup, options);
} else {
// Tie instances to cluster
switch (options.machineImageType) {
// Bottlerocket AMI
case MachineImageType.BOTTLEROCKET: {
autoScalingGroup.addUserData(
// Connect to the cluster
// Source: https://github.com/bottlerocket-os/bottlerocket/blob/develop/QUICKSTART-ECS.md#connecting-to-your-cluster
'[settings.ecs]',
`cluster = "${this.clusterName}"`,
);
// Enabling SSM
// Source: https://github.com/bottlerocket-os/bottlerocket/blob/develop/QUICKSTART-ECS.md#enabling-ssm
autoScalingGroup.role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('AmazonSSMManagedInstanceCore'));
// required managed policy
autoScalingGroup.role.addManagedPolicy(iam.ManagedPolicy.fromAwsManagedPolicyName('service-role/AmazonEC2ContainerServiceforEC2Role'));
break;
}
default:
// Amazon ECS-optimized AMI for Amazon Linux 2
autoScalingGroup.addUserData(`echo ECS_CLUSTER=${this.clusterName} >> /etc/ecs/ecs.config`);
if (!options.canContainersAccessInstanceRole) {
// Deny containers access to instance metadata service
// Source: https://docs.aws.amazon.com/AmazonECS/latest/developerguide/instance_IAM_role.html
autoScalingGroup.addUserData('sudo iptables --insert FORWARD 1 --in-interface docker+ --destination 169.254.169.254/32 --jump DROP');
autoScalingGroup.addUserData('sudo service iptables save');
// The following is only for AwsVpc networking mode, but doesn't hurt for the other modes.
autoScalingGroup.addUserData('echo ECS_AWSVPC_BLOCK_IMDS=true >> /etc/ecs/ecs.config');
}

Proposed Solution

Let Cluster.addAsgCapacityProvider() not need to specify machineImageType.

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

Other

  • 👋 I may be able to implement this feature request
  • ⚠️ This feature might incur a breaking change

This is a 🚀 Feature Request

Metadata

Metadata

Labels

@aws-cdk/aws-ecsRelated to Amazon Elastic Containerfeature-requestA feature should be added or improved.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions