-
Notifications
You must be signed in to change notification settings - Fork 4.4k
fix(aws-ecs): make Cluster.addAsgCapacityProvider() not need specify machineImageType. #16360
Description
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
Bottlerocketautoscaling 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:
aws-cdk/packages/@aws-cdk/aws-ecs/lib/cluster.ts
Lines 357 to 388 in 174b066
| 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