(aws-ecs): adding same launch template to multiple AutoScalingGroup breaks cluster
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 74
Description
### Describe the bug
Ran into a quite bizarre issue today which needed AWS Support to figure out.
We deployed a change to add another autoscaling group to an existing cluster and the cluster wouldn't get any of the instances to register.
We eventually tracked it down to User Data looking like:
```
#!/bin/bash
[settings.ecs]
cluster = ""
[settings.ecs]
cluster = ""
```
Note how it's doubled. The cluster was identical in both cases.
This seems to be caused by:
https://github.com/aws/aws-cdk/blob/994e95289b589596179553a5b9d7201155bd9ed1/packages/aws-cdk-lib/aws-ecs/lib/cluster.ts#L617-L622
I'm not sure what the correct solution would be.
### Regression Issue
- [ ] Select this option if this issue appears to be a regression.
### Last Known Working CDK Version
_No response_
### Expected Behavior
I was expecting the launch template to not be broken.
Ideally either by throwing an error that the launch template shouldn't be reused, or the userdata section to not be invalid.
### Current Behavior
See description
### Reproduction Steps
```ts
import * as cdk from "aws-cdk-lib";
import * as ec2 from "aws-cdk-lib/aws-ec2";
import * as ecs from "aws-cdk-lib/aws-ecs";
import * as autoscaling from "aws-cdk-lib/aws-autoscaling";
import * as iam from "aws-cdk-lib/aws-iam";
export class MinimalEcsClusterStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// Create a VPC and an ECS Cluster.
const vpc = new ec2.Vpc(this, "VPC");
const cluster = new ecs.Cluster(this, "Cluster", { vpc });
// Create a launch template with Linux user data.
const lt = new ec2.LaunchTemplate(this, "LaunchTemplate", {
machineImage: new ecs.BottleRocketImage({
variant: ecs.BottlerocketEcsVariant.AWS_ECS_2,
architecture: ec2.InstanceArchitecture.X86_64,
}),
userData: ec2.UserData.forLinux(),
role: new iam.Role(this, "LTRole", {
assumedBy: new iam.ServicePrincipal("ec2.amazonaws.com"),
}),
});
// Create two autoscaling groups using the same launch template.
const asg1 = new autoscaling.AutoScalingGroup(this, "ASG1", {
vpc,
launchTemplate: lt,
minCapacity: 1,
maxCapacity: 2,
});
const asg2 = new autoscaling.AutoScalingGroup(this, "ASG2", {
vpc,
launchTemplate: lt,
minCapacity: 1,
maxCapacity: 2,
});
// Create capacity providers from the autoscaling groups.
const cp1 = new ecs.AsgCapacityProvider(this, "CP1", {
autoScalingGroup: asg1,
enableManagedDraining: true,
});
const cp2 = new ecs.AsgCapacityProvider(this, "CP2", {
autoScalingGroup: asg2,
enableManagedDraining: true,
});
// Add capacity providers to the cluster.
cluster.addAsgCapacityProvider(cp1);
cluster.addAsgCapacityProvider(cp2);
}
}
```
### Possible Solution
_No response_
### Additional Information/Context
_No response_
### CDK CLI Version
2.152.0 (build faa7d79)
### Framework Version
_No response_
### Node.js Version
20
### OS
macOS
### Language
TypeScript
### Language Version
_No response_
### Other information
Possibly more context in AWS case id 174376078300155 if needed.
Contributor guide
Research direction
Start in packages/aws-cdk-lib/aws-ecs/lib/cluster.ts around lines 617-622 and synthesize the provided TypeScript reproduction to inspect the launch template user data when two AutoScalingGroups reuse it. Compare the generated settings with a single-capacity-provider case and check existing ECS cluster tests; done means the user data remains valid or reuse is rejected clearly.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- aws, typescript
- Domain
- cloud, infrastructure
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100