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): adding placementGroup to LaunchTemplateProps and LaunchTemplate #33726

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion packages/aws-cdk-lib/aws-ec2/lib/launch-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { CfnLaunchTemplate } from './ec2.generated';
import { InstanceType } from './instance-types';
import { IKeyPair } from './key-pair';
import { IMachineImage, MachineImageConfig, OperatingSystemType } from './machine-image';
import { IPlacementGroup } from './placement-group';
import { launchTemplateBlockDeviceMappings } from './private/ebs-util';
import { ISecurityGroup } from './security-group';
import { UserData } from './user-data';
Expand Down Expand Up @@ -441,6 +442,13 @@ export interface LaunchTemplateProps {
* @default - No instance profile
*/
readonly instanceProfile?: iam.IInstanceProfile;

/**
* The placement group that you want to launch the instance into.
*
* @default - no placement group will be used for this launch template.
*/
readonly placementGroup?: IPlacementGroup;
}

/**
Expand Down Expand Up @@ -783,6 +791,9 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
userData: userDataToken,
metadataOptions: this.renderMetadataOptions(props),
networkInterfaces,
placement: props.placementGroup ? {
groupName: props.placementGroup.placementGroupName,
} : undefined,

// Fields not yet implemented:
// ==========================
Expand Down Expand Up @@ -811,7 +822,8 @@ export class LaunchTemplate extends Resource implements ILaunchTemplate, iam.IGr
// Should be implemented via the Tagging aspect in CDK core. Complication will be that this tagging interface is very unique to LaunchTemplates.
// tagSpecification: undefined

// CDK has no abstraction for Placement yet.
// CDK only has placement groups, not placement.
// Specifiying options other than placementGroup is not supported yet.
// placement: undefined,

},
Expand Down
47 changes: 47 additions & 0 deletions packages/aws-cdk-lib/aws-ec2/test/launch-template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import {
LaunchTemplate,
LaunchTemplateHttpTokens,
OperatingSystemType,
PlacementGroup,
PlacementGroupStrategy,
SecurityGroup,
SpotInstanceInterruption,
SpotRequestType,
Expand Down Expand Up @@ -889,6 +891,51 @@ describe('LaunchTemplate', () => {
},
});
});

test('Given placementGroup', () => {
// GIVEN
const pg = new PlacementGroup(stack, 'PlacementGroup', {
strategy: PlacementGroupStrategy.CLUSTER,
});

// WHEN
new LaunchTemplate(stack, 'Template', {
placementGroup: pg,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EC2::LaunchTemplate', {
LaunchTemplateData: {
Placement: {
GroupName: {
'Fn::GetAtt': [
'PlacementGroup68E91A0F',
'GroupName',
],
},
},
},
});
});

test('Given imported placementGroup', () => {
// GIVEN
const importedPg = PlacementGroup.fromPlacementGroupName(stack, 'ImportedPlacementGroup', 'my-placement-group');

// WHEN
new LaunchTemplate(stack, 'Template', {
placementGroup: importedPg,
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::EC2::LaunchTemplate', {
LaunchTemplateData: {
Placement: {
GroupName: 'my-placement-group',
},
},
});
});
});

describe('LaunchTemplate marketOptions', () => {
Expand Down
Loading