Skip to content

Commit

Permalink
feat(ec2): add GenericWindowsImage (#3454)
Browse files Browse the repository at this point in the history
* feat(ec2): Use custom Windows AMI (#3400)
* Created new GenericWindowsImaeProps
* Created new Class GenericWindowsImage

* Add tests
  • Loading branch information
slipdexic authored and mergify[bot] committed Aug 7, 2019
1 parent 863f1ff commit f4ca41c
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
40 changes: 40 additions & 0 deletions packages/@aws-cdk/aws-ec2/lib/machine-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ export interface GenericLinuxImageProps {
readonly userData?: UserData;
}

/**
* Configuration options for GenericWindowsImage
*/
export interface GenericWindowsImageProps {
/**
* Initial user data
*
* @default - Empty UserData for Windows machines
*/
readonly userData?: UserData;
}

/**
* Construct a Linux machine image from an AMI map
*
Expand Down Expand Up @@ -264,6 +276,34 @@ export class GenericLinuxImage implements IMachineImage {
}
}

/**
* Construct a Windows machine image from an AMI map
*
* Allows you to create a generic Windows EC2 , manually specify an AMI map.
*/
export class GenericWindowsImage implements IMachineImage {
constructor(private readonly amiMap: {[region: string]: string}, private readonly props: GenericWindowsImageProps = {}) {
}

public getImage(scope: Construct): MachineImageConfig {
const region = Stack.of(scope).region;
if (Token.isUnresolved(region)) {
throw new Error(`Unable to determine AMI from AMI map since stack is region-agnostic`);
}

const ami = region !== 'test-region' ? this.amiMap[region] : 'ami-12345';
if (!ami) {
throw new Error(`Unable to find AMI in AMI map: no AMI specified for region '${region}'`);
}

return {
imageId: ami,
userData: this.props.userData,
osType: OperatingSystemType.WINDOWS,
};
}
}

/**
* The OS type of a particular image
*/
Expand Down
10 changes: 10 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/example.images.lit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,18 @@ const linux = new ec2.GenericLinuxImage({
'eu-west-1': 'ami-12345678',
// ...
});

// For other custom (Windows) images, instantiate a `GenericWindowsImage` with
// a map giving the AMI to in for each region:

const genericWindows = new ec2.GenericWindowsImage({
'us-east-1': 'ami-97785bed',
'eu-west-1': 'ami-12345678',
// ...
});
/// !hide

Array.isArray(windows);
Array.isArray(amznLinux);
Array.isArray(linux);
Array.isArray(genericWindows);
46 changes: 46 additions & 0 deletions packages/@aws-cdk/aws-ec2/test/test.machine-image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { Stack } from '@aws-cdk/core';
import { Test } from 'nodeunit';
import ec2 = require('../lib');

export = {
'can make and use a Windows image'(test: Test) {
// GIVEN
const stack = new Stack(undefined, undefined, {
env: { region: 'testregion' }
});

// WHEN
const image = new ec2.GenericWindowsImage({
testregion: 'ami-1234'
});

// THEN
const details = image.getImage(stack);
test.equals(details.imageId, 'ami-1234');
test.equals(details.osType, ec2.OperatingSystemType.WINDOWS);

test.done();
},

'WindowsImage retains userdata'(test: Test) {
// GIVEN
const stack = new Stack(undefined, undefined, {
env: { region: 'testregion' }
});

// WHEN
const ud = ec2.UserData.forWindows();

const image = new ec2.GenericWindowsImage({
testregion: 'ami-1234',
}, {
userData: ud
});

// THEN
const details = image.getImage(stack);
test.equals(details.userData, ud);

test.done();
},
};

0 comments on commit f4ca41c

Please sign in to comment.