Skip to content

Commit f4ca41c

Browse files
slipdexicmergify[bot]
authored andcommitted
feat(ec2): add GenericWindowsImage (#3454)
* feat(ec2): Use custom Windows AMI (#3400) * Created new GenericWindowsImaeProps * Created new Class GenericWindowsImage * Add tests
1 parent 863f1ff commit f4ca41c

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

packages/@aws-cdk/aws-ec2/lib/machine-image.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ export interface GenericLinuxImageProps {
235235
readonly userData?: UserData;
236236
}
237237

238+
/**
239+
* Configuration options for GenericWindowsImage
240+
*/
241+
export interface GenericWindowsImageProps {
242+
/**
243+
* Initial user data
244+
*
245+
* @default - Empty UserData for Windows machines
246+
*/
247+
readonly userData?: UserData;
248+
}
249+
238250
/**
239251
* Construct a Linux machine image from an AMI map
240252
*
@@ -264,6 +276,34 @@ export class GenericLinuxImage implements IMachineImage {
264276
}
265277
}
266278

279+
/**
280+
* Construct a Windows machine image from an AMI map
281+
*
282+
* Allows you to create a generic Windows EC2 , manually specify an AMI map.
283+
*/
284+
export class GenericWindowsImage implements IMachineImage {
285+
constructor(private readonly amiMap: {[region: string]: string}, private readonly props: GenericWindowsImageProps = {}) {
286+
}
287+
288+
public getImage(scope: Construct): MachineImageConfig {
289+
const region = Stack.of(scope).region;
290+
if (Token.isUnresolved(region)) {
291+
throw new Error(`Unable to determine AMI from AMI map since stack is region-agnostic`);
292+
}
293+
294+
const ami = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
295+
if (!ami) {
296+
throw new Error(`Unable to find AMI in AMI map: no AMI specified for region '${region}'`);
297+
}
298+
299+
return {
300+
imageId: ami,
301+
userData: this.props.userData,
302+
osType: OperatingSystemType.WINDOWS,
303+
};
304+
}
305+
}
306+
267307
/**
268308
* The OS type of a particular image
269309
*/

packages/@aws-cdk/aws-ec2/test/example.images.lit.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,18 @@ const linux = new ec2.GenericLinuxImage({
2121
'eu-west-1': 'ami-12345678',
2222
// ...
2323
});
24+
25+
// For other custom (Windows) images, instantiate a `GenericWindowsImage` with
26+
// a map giving the AMI to in for each region:
27+
28+
const genericWindows = new ec2.GenericWindowsImage({
29+
'us-east-1': 'ami-97785bed',
30+
'eu-west-1': 'ami-12345678',
31+
// ...
32+
});
2433
/// !hide
2534

2635
Array.isArray(windows);
2736
Array.isArray(amznLinux);
2837
Array.isArray(linux);
38+
Array.isArray(genericWindows);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Stack } from '@aws-cdk/core';
2+
import { Test } from 'nodeunit';
3+
import ec2 = require('../lib');
4+
5+
export = {
6+
'can make and use a Windows image'(test: Test) {
7+
// GIVEN
8+
const stack = new Stack(undefined, undefined, {
9+
env: { region: 'testregion' }
10+
});
11+
12+
// WHEN
13+
const image = new ec2.GenericWindowsImage({
14+
testregion: 'ami-1234'
15+
});
16+
17+
// THEN
18+
const details = image.getImage(stack);
19+
test.equals(details.imageId, 'ami-1234');
20+
test.equals(details.osType, ec2.OperatingSystemType.WINDOWS);
21+
22+
test.done();
23+
},
24+
25+
'WindowsImage retains userdata'(test: Test) {
26+
// GIVEN
27+
const stack = new Stack(undefined, undefined, {
28+
env: { region: 'testregion' }
29+
});
30+
31+
// WHEN
32+
const ud = ec2.UserData.forWindows();
33+
34+
const image = new ec2.GenericWindowsImage({
35+
testregion: 'ami-1234',
36+
}, {
37+
userData: ud
38+
});
39+
40+
// THEN
41+
const details = image.getImage(stack);
42+
test.equals(details.userData, ud);
43+
44+
test.done();
45+
},
46+
};

0 commit comments

Comments
 (0)