Skip to content

Commit 8c6777c

Browse files
authored
feat(ec2): allow imdsv2 usage on bastion host (#18955)
Allow user to use Instance Metadata Service Version 2 (IMDSv2) on Bastion Host. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent f8bb85f commit 8c6777c

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

packages/@aws-cdk/aws-ec2/lib/bastion-host.ts

+11
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,13 @@ export interface BastionHostLinuxProps {
9797
* @default - default options
9898
*/
9999
readonly initOptions?: ApplyCloudFormationInitOptions;
100+
101+
/**
102+
* Whether IMDSv2 should be required on this instance
103+
*
104+
* @default - false
105+
*/
106+
readonly requireImdsv2?: boolean;
100107
}
101108

102109
/**
@@ -147,14 +154,17 @@ export class BastionHostLinux extends Resource implements IInstance {
147154
* @attribute
148155
*/
149156
public readonly instancePrivateDnsName: string;
157+
150158
/**
151159
* @attribute
152160
*/
153161
public readonly instancePrivateIp: string;
162+
154163
/**
155164
* @attribute
156165
*/
157166
public readonly instancePublicDnsName: string;
167+
158168
/**
159169
* @attribute
160170
*/
@@ -178,6 +188,7 @@ export class BastionHostLinux extends Resource implements IInstance {
178188
blockDevices: props.blockDevices ?? undefined,
179189
init: props.init,
180190
initOptions: props.initOptions,
191+
requireImdsv2: props.requireImdsv2 ?? false,
181192
});
182193
this.instance.addToRolePolicy(new PolicyStatement({
183194
actions: [

packages/@aws-cdk/aws-ec2/test/bastion-host.test.ts

+21
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,25 @@ describe('bastion host', () => {
160160
},
161161
});
162162
});
163+
164+
test('imdsv2 is required', () => {
165+
//GIVEN
166+
const stack = new Stack();
167+
const vpc = new Vpc(stack, 'VPC');
168+
169+
//WHEN
170+
new BastionHostLinux(stack, 'Bastion', {
171+
vpc,
172+
requireImdsv2: true,
173+
});
174+
175+
// THEN
176+
Template.fromStack(stack).hasResourceProperties('AWS::EC2::LaunchTemplate', {
177+
LaunchTemplateData: {
178+
MetadataOptions: {
179+
HttpTokens: 'required',
180+
},
181+
},
182+
});
183+
});
163184
});

0 commit comments

Comments
 (0)