aws / aws/aws-cdk

aws-autoscaling: `LaunchTemplate` resource should be created for `AutoScalingGroup` instead of `LaunchConfiguration` when running unit tests

Open
#28,750 3 comments 0 reactions 0 assignees View on GitHub
@aws-cdk/aws-autoscaling bug effort/medium p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

Hi, template generated from stack in unit tests creates a `LaunchConfiguration` resource instead of [the recommended `LaunchTemplate` resource](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-autoscaling-launchconfiguration.html) when an `AutoScalingGroup` resource is created. So when a test like this is added:
```typescript
test('Auto Scaling Group Created', () => {
const app = new App();
const stack = new DummyStack(app, 'DummyStack');
const template = Template.fromStack(stack);
console.log(template);
template.hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
AutoScalingGroupName: 'MyAutoScalingGroup',
LaunchTemplate: Match.anyValue(),
MaxSize: '1',
MinSize: '0'
});
});
```
It will fail with:
```
FAIL test/dummy.test.ts
✕ Auto Scaling Group Created (213 ms)

● Auto Scaling Group Created

Template has 1 resources with type AWS::AutoScaling::AutoScalingGroup, but none match as expected.
The 1 closest matches:
MyAutoScalingGroupASG1686ADA7 :: {
"Properties": {
"LaunchConfigurationName": { "Ref": "MyAutoScalingGroupLaunchConfig15D8856B" },
!! Missing key 'LaunchTemplate'
"LaunchTemplate": undefined,
"MaxSize": "1",
"MinSize": "0",
"Tags": [ { ... } ],
"VPCZoneIdentifier": [ ... ]
},
"Type": "AWS::AutoScaling::AutoScalingGroup",
"UpdatePolicy": { "AutoScalingScheduledAction": { "IgnoreUnmodifiedGroupSizeProperties": true } }
}
```

Please note that this only happens for unit tests, when `cdk synth` is executed `AutoScalingGroup` is correctly created with a `LaunchTemplate`:
```json
"MyAutoScalingGroupASG1686ADA7": {
"Type": "AWS::AutoScaling::AutoScalingGroup",
"Properties": {
"AutoScalingGroupName": "MyAutoScalingGroup",
"LaunchTemplate": {
"LaunchTemplateId": {
"Ref": "MyAutoScalingGroupLaunchTemplate9D9A040D"
},
"Version": {
"Fn::GetAtt": [
"MyAutoScalingGroupLaunchTemplate9D9A040D",
"LatestVersionNumber"
]
}
},
...
}
```

### Expected Behavior

`LaunchTemplate` should be created instead of `LaunchConfiguration` for `AutoScalingGroup` when template is created during unit test execution. This as per recommendation of [*Template reference docs*](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-autoscaling-launchconfiguration.html).

### Current Behavior

`LaunchConfiguration` is created for `AutoScalingGroup` instead of `LaunchTemplate` when template is created during unit test execution. This is against recommendation of [*Template reference docs*](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-autoscaling-launchconfiguration.html).

### Reproduction Steps

1. Create a new CDK app with `cdk init app --language typescript` using latest CDK CLI (version `2.121.1 (build d86bb1a)`).
2. Update stack in `lib/` with this code:
```typescript
export class DummyStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const vpc = new ec2.Vpc(this, 'CoreVpc', {
availabilityZones: ['us-east-1a', 'us-east-1b'],
enableDnsHostnames: false,
enableDnsSupport: false,
ipAddresses: ec2.IpAddresses.cidr('10.0.0.0/16'),
subnetConfiguration: [
{
cidrMask: 20,
name: 'private',
subnetType: ec2.SubnetType.PRIVATE_ISOLATED
}
],
vpcName: 'MyVpc'
});
const autoScalingGroup = new autoscaling.AutoScalingGroup(this, 'MyAutoScalingGroup', {
autoScalingGroupName: 'MyAutoScalingGroup',
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ecs.EcsOptimizedImage.amazonLinux2(),
maxCapacity: 1,
minCapacity: 0,
vpc: vpc,
});
}
}
```
3. Update test file in `test/` with this code:
```typescript
test('Auto Scaling Group Created', () => {
const app = new App();
const stack = new DummyStack(app, 'DummyStack');
const template = Template.fromStack(stack);
console.log(template);
template.hasResourceProperties('AWS::AutoScaling::AutoScalingGroup', {
LaunchTemplate: Match.anyValue(),
MaxSize: '1',
MinSize: '0'
});
});
```
4. Run `npm test`.

### Possible Solution

Possibly solution from #23165 could be extended.

### Additional Information/Context

Contents from `package.json`:
```
{
"name": "dummy",
"version": "0.1.0",
"bin": {
"dummy": "bin/dummy.js"
},
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk"
},
"devDependencies": {
"@types/jest": "^29.5.11",
"@types/node": "20.10.8",
"jest": "^29.7.0",
"ts-jest": "^29.1.1",
"aws-cdk": "2.121.1",
"ts-node": "^10.9.2",
"typescript": "~5.3.3"
},
"dependencies": {
"aws-cdk-lib": "2.121.1",
"constructs": "^10.0.0",
"source-map-support": "^0.5.21"
}
}
```

### CDK CLI Version

2.121.1 (build d86bb1a)

### Framework Version

2.121.1

### Node.js Version

v18.18.0

### OS

macOS Sonoma 14.2.1

### Language

TypeScript

### Language Version

Version 5.3.3

### Other information

+ https://github.com/aws/aws-cdk/issues/23165

Contributor guide

Open the contributing guide

Research direction

Start with the reproduced test in test/dummy.test.ts and run npm test, then compare its Template.fromStack output with the cdk synth output from the stack in lib/. Read the related issue #23165 for context. Done means unit-test synthesis produces the expected AWS::AutoScaling::AutoScalingGroup LaunchTemplate while preserving the existing assertions.

Written by the indexing model from the issue text.

Assessment

Tech stack
aws, typescript
Domain
cloud, infrastructure, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.