Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(autoscaling): osType is wrong when using CloudformationInit with launchTemplate #20759

Merged
merged 2 commits into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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