Skip to content

Commit da61adc

Browse files
fix(autoscaling): error not thrown when associatePublicIpAddress is set to false when specifying launchTemplate (#21714)
Setting `associatePublicIpAddress` prop when also setting `launchTemplate` prop on a new ASG is supposed to throw an error, but doesn't due to `associatePublicIpAddress` being a boolean, and the error message not triggering if a falsy value is used for the prop. This PR will ensure an error is thrown when `associatePublicIpAddress` is set, even when the value passed is falsy. It also updates the documentation on all props which conflict with `launchTemplate` or `mixedInstancesPolicy` fixes: #21576 ---- ### All Submissions: * [ ] 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*
1 parent 5e7662c commit da61adc

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts

+16-6
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export interface CommonAutoScalingGroupProps {
8080
/**
8181
* Name of SSH keypair to grant access to instances
8282
*
83+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
84+
*
8385
* @default - No SSH access will be possible.
8486
*/
8587
readonly keyName?: string;
@@ -190,6 +192,8 @@ export interface CommonAutoScalingGroupProps {
190192
* Whether instances in the Auto Scaling Group should have public
191193
* IP addresses associated with them.
192194
*
195+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
196+
*
193197
* @default - Use subnet setting.
194198
*/
195199
readonly associatePublicIpAddress?: boolean;
@@ -198,6 +202,8 @@ export interface CommonAutoScalingGroupProps {
198202
* The maximum hourly price (in USD) to be paid for any Spot Instance launched to fulfill the request. Spot Instances are
199203
* launched when the price you specify exceeds the current Spot market price.
200204
*
205+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
206+
*
201207
* @default none
202208
*/
203209
readonly spotPrice?: string;
@@ -217,6 +223,8 @@ export interface CommonAutoScalingGroupProps {
217223
* You can use block device mappings to specify additional EBS volumes or
218224
* instance store volumes to attach to an instance when it is launched.
219225
*
226+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
227+
*
220228
* @see https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/block-device-mapping-concepts.html
221229
*
222230
* @default - Uses the block device mapping of the AMI
@@ -243,6 +251,8 @@ export interface CommonAutoScalingGroupProps {
243251
* When detailed monitoring is enabled, Amazon CloudWatch generates metrics every minute and your account
244252
* is charged a fee. When you disable detailed monitoring, CloudWatch generates metrics every 5 minutes.
245253
*
254+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
255+
*
246256
* @see https://docs.aws.amazon.com/autoscaling/latest/userguide/as-instance-monitoring.html#enable-as-instance-metrics
247257
*
248258
* @default - Monitoring.DETAILED
@@ -543,7 +553,7 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
543553
/**
544554
* Type of instance to launch
545555
*
546-
* `launchTemplate` must not be specified when this property is specified.
556+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
547557
*
548558
* @default - Do not provide any instance type
549559
*/
@@ -552,7 +562,7 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
552562
/**
553563
* AMI to launch
554564
*
555-
* `launchTemplate` must not be specified when this property is specified.
565+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
556566
*
557567
* @default - Do not provide any machine image
558568
*/
@@ -561,7 +571,7 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
561571
/**
562572
* Security group to launch the instances in.
563573
*
564-
* `launchTemplate` must not be specified when this property is specified.
574+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
565575
*
566576
* @default - A SecurityGroup will be created if none is specified.
567577
*/
@@ -572,7 +582,7 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
572582
*
573583
* The UserData may still be mutated after creation.
574584
*
575-
* `launchTemplate` must not be specified when this property is specified.
585+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
576586
*
577587
* @default - A UserData object appropriate for the MachineImage's
578588
* Operating System is created.
@@ -584,7 +594,7 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
584594
*
585595
* The role must be assumable by the service principal `ec2.amazonaws.com`:
586596
*
587-
* `launchTemplate` must not be specified when this property is specified.
597+
* `launchTemplate` and `mixedInstancesPolicy` must not be specified when this property is specified
588598
*
589599
* @example
590600
*
@@ -1523,7 +1533,7 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
15231533
if (props.instanceMonitoring) {
15241534
throw new Error('Setting \'instanceMonitoring\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set');
15251535
}
1526-
if (props.associatePublicIpAddress) {
1536+
if (props.associatePublicIpAddress !== undefined) {
15271537
throw new Error('Setting \'associatePublicIpAddress\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set');
15281538
}
15291539
if (props.spotPrice) {

packages/@aws-cdk/aws-autoscaling/test/auto-scaling-group.test.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,7 @@ describe('auto scaling group', () => {
15251525
launchTemplateId: 'test-lt-id',
15261526
versionNumber: '0',
15271527
});
1528+
const vpc = mockVpc(stack);
15281529

15291530
// THEN
15301531
expect(() => {
@@ -1535,9 +1536,16 @@ describe('auto scaling group', () => {
15351536
generation: AmazonLinuxGeneration.AMAZON_LINUX_2,
15361537
cpuType: AmazonLinuxCpuType.X86_64,
15371538
}),
1538-
vpc: mockVpc(stack),
1539+
vpc,
15391540
});
15401541
}).toThrow('Setting \'machineImage\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set');
1542+
expect(() => {
1543+
new autoscaling.AutoScalingGroup(stack, 'imported-lt-asg-2', {
1544+
launchTemplate: lt,
1545+
associatePublicIpAddress: true,
1546+
vpc,
1547+
});
1548+
}).toThrow('Setting \'associatePublicIpAddress\' must not be set when \'launchTemplate\' or \'mixedInstancesPolicy\' is set');
15411549
});
15421550

15431551
test('Cannot specify Launch Template without instance type', () => {

0 commit comments

Comments
 (0)