Skip to content

Commit c62377e

Browse files
authoredNov 30, 2021
feat(ec2): extend BastionHostLinux to support CloudFormationInit (#17507)
Implements #17161 Extends the `BastionHostLinux` constructor to accept optional `CloudFormationInit` and `ApplyCloudFormationInitOptions` arguments to be passed to the underlying instance. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 719f33e commit c62377e

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed
 

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

+20-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { IPrincipal, IRole, PolicyStatement } from '@aws-cdk/aws-iam';
22
import { CfnOutput, Resource, Stack } from '@aws-cdk/core';
33
import { Construct } from 'constructs';
44
import { AmazonLinuxGeneration, InstanceArchitecture, InstanceClass, InstanceSize, InstanceType } from '.';
5+
import { CloudFormationInit } from './cfn-init';
56
import { Connections } from './connections';
6-
import { IInstance, Instance } from './instance';
7+
import { ApplyCloudFormationInitOptions, IInstance, Instance } from './instance';
78
import { AmazonLinuxCpuType, IMachineImage, MachineImage } from './machine-image';
89
import { IPeer } from './peer';
910
import { Port } from './port';
@@ -80,6 +81,22 @@ export interface BastionHostLinuxProps {
8081
* @default - Uses the block device mapping of the AMI
8182
*/
8283
readonly blockDevices?: BlockDevice[];
84+
85+
/**
86+
* Apply the given CloudFormation Init configuration to the instance at startup
87+
*
88+
* @default - no CloudFormation init
89+
*/
90+
readonly init?: CloudFormationInit;
91+
92+
/**
93+
* Use the given options for applying CloudFormation Init
94+
*
95+
* Describes the configsets to use and the timeout to wait
96+
*
97+
* @default - default options
98+
*/
99+
readonly initOptions?: ApplyCloudFormationInitOptions;
83100
}
84101

85102
/**
@@ -159,6 +176,8 @@ export class BastionHostLinux extends Resource implements IInstance {
159176
}),
160177
vpcSubnets: props.subnetSelection ?? {},
161178
blockDevices: props.blockDevices ?? undefined,
179+
init: props.init,
180+
initOptions: props.initOptions,
162181
});
163182
this.instance.addToRolePolicy(new PolicyStatement({
164183
actions: [

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

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import '@aws-cdk/assert-internal/jest';
2-
import { Stack } from '@aws-cdk/core';
3-
import { BastionHostLinux, BlockDeviceVolume, InstanceClass, InstanceSize, InstanceType, SubnetType, Vpc } from '../lib';
2+
import { ResourcePart } from '@aws-cdk/assert-internal';
3+
import { Duration, Stack } from '@aws-cdk/core';
4+
import { BastionHostLinux, BlockDeviceVolume, CloudFormationInit, InitCommand, InstanceClass, InstanceSize, InstanceType, SubnetType, Vpc } from '../lib';
45

56
describe('bastion host', () => {
67
test('default instance is created in basic', () => {
@@ -123,4 +124,41 @@ describe('bastion host', () => {
123124

124125

125126
});
127+
128+
test('add CloudFormation Init to instance', () => {
129+
// GIVEN
130+
const stack = new Stack();
131+
const vpc = new Vpc(stack, 'VPC');
132+
133+
// WHEN
134+
new BastionHostLinux(stack, 'Bastion', {
135+
vpc,
136+
initOptions: {
137+
timeout: Duration.minutes(30),
138+
},
139+
init: CloudFormationInit.fromElements(
140+
InitCommand.shellCommand('echo hello'),
141+
),
142+
});
143+
144+
// THEN
145+
expect(stack).toHaveResourceLike('AWS::EC2::Instance', {
146+
CreationPolicy: {
147+
ResourceSignal: {
148+
Timeout: 'PT30M',
149+
},
150+
},
151+
Metadata: {
152+
'AWS::CloudFormation::Init': {
153+
config: {
154+
commands: {
155+
'000': {
156+
command: 'echo hello',
157+
},
158+
},
159+
},
160+
},
161+
},
162+
}, ResourcePart.CompleteDefinition);
163+
});
126164
});

0 commit comments

Comments
 (0)
Please sign in to comment.