Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ec2): add GenericWindowsImage #3454

Merged
merged 4 commits into from
Aug 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
},
};