Skip to content

Commit

Permalink
add option to disable imdsv1
Browse files Browse the repository at this point in the history
  • Loading branch information
jericht committed Sep 28, 2021
1 parent 239840b commit 17f123b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/@aws-cdk/aws-autoscaling/lib/auto-scaling-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import * as sns from '@aws-cdk/aws-sns';

import {
Annotations,
Aspects,
Aws,
CfnAutoScalingRollingUpdate, CfnCreationPolicy, CfnUpdatePolicy,
Duration, Fn, IResource, Lazy, PhysicalName, Resource, Stack, Tags,
Token,
Tokenization, withResolved,
} from '@aws-cdk/core';
import { Construct } from 'constructs';
import { AutoScalingGroupImdsAspect } from './aspects';
import { CfnAutoScalingGroup, CfnAutoScalingGroupProps, CfnLaunchConfiguration } from './autoscaling.generated';
import { BasicLifecycleHookProps, LifecycleHook } from './lifecycle-hook';
import { BasicScheduledActionProps, ScheduledAction } from './scheduled-action';
Expand Down Expand Up @@ -384,6 +386,13 @@ export interface AutoScalingGroupProps extends CommonAutoScalingGroupProps {
* @default - default options
*/
readonly initOptions?: ApplyCloudFormationInitOptions;

/**
* Whether IMDSv1 should be disabled on launched instances.
*
* @default - false
*/
readonly disableImdsv1?: boolean;
}

/**
Expand Down Expand Up @@ -1065,6 +1074,10 @@ export class AutoScalingGroup extends AutoScalingGroupBase implements
}

this.spotPrice = props.spotPrice;

if (props.disableImdsv1 === true) {
Aspects.of(this).add(new AutoScalingGroupImdsAspect({ enableImdsV1: false }));
}
}

/**
Expand Down
21 changes: 21 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 @@ -1364,6 +1364,27 @@ describe('auto scaling group', () => {


});

test('disables imdsv1', () => {
// GIVEN
const stack = new cdk.Stack();
const vpc = mockVpc(stack);

// WHEN
new autoscaling.AutoScalingGroup(stack, 'MyASG', {
vpc,
instanceType: new ec2.InstanceType('t2.micro'),
machineImage: ec2.MachineImage.latestAmazonLinux(),
disableImdsv1: true,
});

// THEN
expect(stack).toHaveResourceLike('AWS::AutoScaling::LaunchConfiguration', {
MetadataOptions: {
HttpTokens: 'required',
},
});
});
});

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

0 comments on commit 17f123b

Please sign in to comment.