Skip to content

Commit

Permalink
fix(autoscaling): osType is wrong when using CloudformationInit with …
Browse files Browse the repository at this point in the history
…launchTemplate (#20759)

Fix #20304

Like #20454 but add a test

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
BDeus committed Jun 16, 2022
1 parent 50c8f80 commit 610b7b5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1206,7 +1206,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
this._role = this.launchTemplate?.role;
this.grantPrincipal = this._role || new iam.UnknownPrincipal({ resource: this });

this.osType = this.launchTemplate?.osType || ec2.OperatingSystemType.UNKNOWN;
this.osType = this.launchTemplate?.osType ?? ec2.OperatingSystemType.UNKNOWN;
} else {
if (!props.machineImage) {
throw new Error('Setting \'machineImage\' is required when \'launchTemplate\' and \'mixedInstancesPolicy\' is not set');
Expand Down
26 changes: 26 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1732,6 +1732,32 @@ describe('auto scaling group', () => {
asg.addSecurityGroup(mockSecurityGroup(stack));
}).toThrow('You cannot add security groups when the Auto Scaling Group is created from a Launch Template.');
});

test('Should not throw when LaunchTemplate is used with CloudformationInit', () => {
const stack = new cdk.Stack();

// WHEN
const lt = new LaunchTemplate(stack, 'LaunchTemplate', {
machineImage: new AmazonLinuxImage(),
instanceType: new ec2.InstanceType('t3.micro'),
userData: ec2.UserData.forLinux(),
securityGroup: ec2.SecurityGroup.fromSecurityGroupId(stack, 'ImportedSg', 'securityGroupId'),
role: iam.Role.fromRoleArn(stack, 'ImportedRole', 'arn:aws:iam::123456789012:role/MockRole'),
});

const cfInit = ec2.CloudFormationInit.fromElements(
ec2.InitCommand.shellCommand('/bash'),
);

// THEN
expect(() => new autoscaling.AutoScalingGroup(stack, 'Asg', {
launchTemplate: lt,
init: cfInit,
vpc: mockVpc(stack),
signals: autoscaling.Signals.waitForAll(),
})).not.toThrow();

});
});

function mockVpc(stack: cdk.Stack) {
Expand Down

0 comments on commit 610b7b5

Please sign in to comment.