Skip to content

Commit

Permalink
feat(ec2): can define Launch Templates (not use them yet) (aws#12385)
Browse files Browse the repository at this point in the history
This provides an initial implementation of a level 2 construct for EC2 Launch Templates. It is a start that is intended to help get the ball rolling on implementation of Launch Template support within the CDK. It is a step towards resolving aws#6734

Launch Templates have value even without the integrations into Instance and AutoScalingGroup being implemented yet. Thus, the intention with this PR is to solely implement the L2 for LaunchTemplate. Future work in a separate PR would be required to implement its integration into Instance, AutoScalingGroup, and others.

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
ddneilson authored and TLadd committed Feb 9, 2021
1 parent 0b22636 commit c20fd03
Show file tree
Hide file tree
Showing 7 changed files with 1,419 additions and 35 deletions.
21 changes: 21 additions & 0 deletions packages/@aws-cdk/aws-ec2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -999,3 +999,24 @@ const subnet = Subnet.fromSubnetAttributes(this, 'SubnetFromAttributes', {
// Supply only subnet id
const subnet = Subnet.fromSubnetId(this, 'SubnetFromId', 's-1234');
```

## Launch Templates

A Launch Template is a standardized template that contains the configuration information to launch an instance.
They can be used when launching instances on their own, through Amazon EC2 Auto Scaling, EC2 Fleet, and Spot Fleet.
Launch templates enable you to store launch parameters so that you do not have to specify them every time you launch
an instance. For information on Launch Templates please see the
[official documentation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html).

The following demonstrates how to create a launch template with an Amazon Machine Image, and security group.

```ts
const vpc = new ec2.Vpc(...);
// ...
const template = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
machineImage: new ec2.AmazonMachineImage(),
securityGroup: new ec2.SecurityGroup(this, 'LaunchTemplateSG', {
vpc: vpc,
}),
});
```
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-ec2/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './cfn-init';
export * from './cfn-init-elements';
export * from './instance-types';
export * from './instance';
export * from './launch-template';
export * from './machine-image';
export * from './nat';
export * from './network-acl';
Expand Down
5 changes: 3 additions & 2 deletions packages/@aws-cdk/aws-ec2/lib/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import { Connections, IConnectable } from './connections';
import { CfnInstance } from './ec2.generated';
import { InstanceType } from './instance-types';
import { IMachineImage, OperatingSystemType } from './machine-image';
import { instanceBlockDeviceMappings } from './private/ebs-util';
import { ISecurityGroup, SecurityGroup } from './security-group';
import { UserData } from './user-data';
import { BlockDevice, synthesizeBlockDeviceMappings } from './volume';
import { BlockDevice } from './volume';
import { IVpc, Subnet, SubnetSelection } from './vpc';

/**
Expand Down Expand Up @@ -362,7 +363,7 @@ export class Instance extends Resource implements IInstance {
subnetId: subnet.subnetId,
availabilityZone: subnet.availabilityZone,
sourceDestCheck: props.sourceDestCheck,
blockDeviceMappings: props.blockDevices !== undefined ? synthesizeBlockDeviceMappings(this, props.blockDevices) : undefined,
blockDeviceMappings: props.blockDevices !== undefined ? instanceBlockDeviceMappings(this, props.blockDevices) : undefined,
privateIpAddress: props.privateIpAddress,
});
this.instance.node.addDependency(this.role);
Expand Down
Loading

0 comments on commit c20fd03

Please sign in to comment.