# LaunchTemplate

This represents an EC2 LaunchTemplate.

Extends: Construct › Resource › LaunchTemplate
Implements: IResource, IConnectable, ILaunchTemplate, IGrantable, IEnvironmentAware, ILaunchTemplateRef, IConstruct, IDependable

- CloudFormation: [`AWS::EC2::LaunchTemplate`](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-ec2-launchtemplate.html)
- L1 type: [`CfnLaunchTemplate`](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnLaunchTemplate.html)
- Source: [`aws-ec2/lib/launch-template.ts`](https://github.com/aws/aws-cdk/blob/v2.275.0/packages/aws-cdk-lib/aws-ec2/lib/launch-template.ts#L565)
- Type last changed: v2.224.0 · 13 Nov 2025

## Example

```ts
declare const vpc: ec2.Vpc;

const role = new iam.Role(this, 'Role', {
  assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});
const instanceProfile = new iam.InstanceProfile(this, 'InstanceProfile', {
  role,
});

const template = new ec2.LaunchTemplate(this, 'LaunchTemplate', {
  launchTemplateName: 'MyTemplateV1',
  versionDescription: 'This is my v1 template',
  machineImage: ec2.MachineImage.latestAmazonLinux2023(),
  securityGroup: new ec2.SecurityGroup(this, 'LaunchTemplateSG', {
    vpc: vpc,
  }),
  instanceProfile,
});
```

## Initializer

```ts
new LaunchTemplate(scope: Construct, id: string, props?: LaunchTemplateProps)
```

## Construct Props

- `associatePublicIpAddress`: boolean — Whether instances should have a public IP addresses associated with them.

  **Default:** Use subnet settings

- `blockDevices`: [BlockDevice](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.BlockDevice.html)[] — Specifies how block devices are exposed to the instance. You can specify virtual devices and EBS volumes.

  **Default:** Uses the block device mapping of the AMI

  Each instance that is launched has an associated root device volume,
  either an Amazon EBS volume or an instance store volume.
  You can use block device mappings to specify additional EBS volumes or
  instance store volumes to attach to an instance when it is launched.

- `cpuCredits`: [CpuCredits](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CpuCredits.html) — CPU credit type for burstable EC2 instance types.

  **Default:** No credit type is specified in the Launch Template.

- `cpuOptions`: [LaunchTemplateCpuOptions](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.LaunchTemplateCpuOptions.html) — The CPU options for the instance.

  **Default:** The CPU options are not specified in the launch template.

- `detailedMonitoring`: boolean — If set to true, then detailed monitoring will be enabled on instances created with this launch template.

  **Default:** False - Detailed monitoring is disabled.

- `disableApiTermination`: boolean — If you set this parameter to true, you cannot terminate the instances launched with this launch template using the Amazon EC2 console, CLI, or API;

  **Default:** The API termination setting is not specified in the Launch Template.

  otherwise, you can.

- `ebsOptimized`: boolean — Indicates whether the instances are optimized for Amazon EBS I/O.

  **Default:** EBS optimization is not specified in the launch template.

  This optimization provides dedicated throughput
  to Amazon EBS and an optimized configuration stack to provide optimal Amazon EBS I/O performance. This optimization
  isn't available with all instance types. Additional usage charges apply when using an EBS-optimized instance.

- `hibernationConfigured`: boolean — If you set this parameter to true, the instance is enabled for hibernation.

  **Default:** Hibernation configuration is not specified in the launch template; defaulting to false.

- `httpEndpoint`: boolean = true — Enables or disables the HTTP metadata endpoint on your instances.

- `httpProtocolIpv6`: boolean = true — Enables or disables the IPv6 endpoint for the instance metadata service.

- `httpPutResponseHopLimit`: number = 1 — The desired HTTP PUT response hop limit for instance metadata requests.

  The larger the number, the further instance metadata requests can travel.

- `httpTokens`: [LaunchTemplateHttpTokens](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.LaunchTemplateHttpTokens.html) — The state of token usage for your instance metadata requests.

  **Default:** LaunchTemplateHttpTokens.OPTIONAL

  The default state is `optional` if not specified. However,
  if requireImdsv2 is true, the state must be `required`.

- `instanceInitiatedShutdownBehavior`: [InstanceInitiatedShutdownBehavior](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.InstanceInitiatedShutdownBehavior.html) — Indicates whether an instance stops or terminates when you initiate shutdown from the instance (using the operating system command for system shutdown).

  **Default:** Shutdown behavior is not specified in the launch template; defaults to STOP.

- `instanceMetadataTags`: boolean = false — Set to enabled to allow access to instance tags from the instance metadata.

  Set to disabled to turn off access to instance tags from the instance metadata.

- `instanceProfile`: [IInstanceProfile](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IInstanceProfile.html) — The instance profile used to pass role information to EC2 instances.

  **Default:** No instance profile

  Note: You can provide an instanceProfile or a role, but not both.

- `instanceType`: [InstanceType](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.InstanceType.html) — Type of instance to launch.

  **Default:** This Launch Template does not specify a default Instance Type.

- `keyName`: string — - Use `keyPair` instead - https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2-readme.html#using-an-existing-ec2-key-pair

  **Default:** No SSH access will be possible.

- `keyPair`: [IKeyPair](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IKeyPair.html) — The SSH keypair to grant access to the instance.

  **Default:** No SSH access will be possible.

- `launchTemplateName`: string — Name for this launch template.

  **Default:** Automatically generated name

- `machineImage`: [IMachineImage](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.IMachineImage.html) — The AMI that will be used by instances.

  **Default:** This Launch Template does not specify a default AMI.

- `nitroEnclaveEnabled`: boolean — If this parameter is set to true, the instance is enabled for AWS Nitro Enclaves;

  **Default:** Enablement of Nitro enclaves is not specified in the launch template; defaulting to false.

  otherwise, it is not enabled for AWS Nitro Enclaves.

- `placementGroup`: [IPlacementGroupRef](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ec2.IPlacementGroupRef.html) — The placement group that you want to launch the instance into.

  **Default:** no placement group will be used for this launch template.

- `requireImdsv2`: boolean = false — Whether IMDSv2 should be required on launched instances.

- `role`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — An IAM role to associate with the instance profile that is used by instances.

  **Default:** No new role is created.

  The role must be assumable by the service principal `ec2.amazonaws.com`.
  Note: You can provide an instanceProfile or a role, but not both.

  ```ts
  const role = new iam.Role(this, 'MyRole', {
    assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com')
  });
  ```

- `securityGroup`: [ISecurityGroup](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.ISecurityGroup.html) — Security group to assign to instances created with the launch template.

  **Default:** No security group is assigned.

- `spotOptions`: [LaunchTemplateSpotOptions](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.LaunchTemplateSpotOptions.html) — If this property is defined, then the Launch Template's InstanceMarketOptions will be set to use Spot instances, and the options for the Spot instances will be as defined.

  **Default:** Instance launched with this template will not be spot instances.

- `userData`: [UserData](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.UserData.html) — The user data to make available to the instance.

  **Default:** This Launch Template creates a UserData based on the type of provided machineImage; no UserData is created if a machineImage is not provided

- `versionDescription`: string — A description for the first version of the launch template.

  **Default:** No description

  The version description must be maximum 255 characters long.

## Properties

- `connections`: [Connections](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Connections.html) — Allows specifying security group connections for the instance.

- `defaultVersionNumber`: string — The default version for the launch template.

- `env`: [ResourceEnvironment](/cdk/api/v2/docs/aws-cdk-lib.interfaces.ResourceEnvironment.html) — The environment this resource belongs to.

  For resources that are created and managed in a Stack (those created by
  creating new class instances like `new Role()`, `new Bucket()`, etc.), this
  is always the same as the environment of the stack they belong to.

  For referenced resources (those obtained from referencing methods like
  `Role.fromRoleArn()`, `Bucket.fromBucketName()`, etc.), they might be
  different than the stack they were imported into.

- `grantPrincipal`: [IPrincipal](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IPrincipal.html) — Principal to grant permissions to.

- `imageId`: string — The AMI ID of the image to use.

- `instanceType`: [InstanceType](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.InstanceType.html) — Type of instance to launch.

- `latestVersionNumber`: string — The latest version of the launch template.

- `launchTemplateId`: string — The identifier of the Launch Template.

  Exactly one of `launchTemplateId` and `launchTemplateName` will be set.

- `launchTemplateName`: string — The name of the Launch Template.

  Exactly one of `launchTemplateId` and `launchTemplateName` will be set.

- `launchTemplateRef`: [LaunchTemplateReference](/cdk/api/v2/docs/aws-cdk-lib.interfaces.aws_ec2.LaunchTemplateReference.html) — A reference to a LaunchTemplate resource.

- `node`: [Node](/cdk/api/v2/docs/constructs.Node.html) — The tree node.

- `osType`: [OperatingSystemType](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.OperatingSystemType.html) — The type of OS the instance is running.

- `PROPERTY_INJECTION_ID`: string (static) — Uniquely identifies this class.

- `role`: [IRole](/cdk/api/v2/docs/aws-cdk-lib.aws_iam.IRole.html) — IAM Role assumed by instances that are launched from this template.

- `stack`: [Stack](/cdk/api/v2/docs/aws-cdk-lib.Stack.html) — The stack in which this resource is defined.

- `userData`: [UserData](/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.UserData.html) — UserData executed by instances that are launched from this template.

- `versionNumber`: string — The version number of this launch template to use.

## Methods

- `addSecurityGroup(securityGroup: ISecurityGroup): void` — Add the security group to the instance.

  **Parameters**

  - `securityGroup` — : The security group to add.

- `applyCrossStackReferenceStrength(strength: ReferenceStrength): void` — Override the cross-stack reference strength for this resource.

  When set, any cross-stack reference to this resource will use the specified
  mechanism instead of the global default determined by the
  `@aws-cdk/core:defaultCrossStackReferences` context key. This is useful for
  selectively weakening specific references to avoid the "deadly embrace" problem
  without changing the app-wide default.

  **Parameters**

  - `strength` — - The reference strength to use for this resource.

- `applyRemovalPolicy(policy: RemovalPolicy): void` — Apply the given removal policy to this resource.

  The Removal Policy controls what happens to this resource when it stops
  being managed by CloudFormation, either because you've removed it from the
  CDK application or because you've made a change that requires the resource
  to be replaced.

  The resource can be deleted (`RemovalPolicy.DESTROY`), or left in your AWS
  account for data recovery and cleanup later (`RemovalPolicy.RETAIN`).

- `fromLaunchTemplateAttributes(scope: Construct, id: string, attrs: LaunchTemplateAttributes): ILaunchTemplate` (static) — Import an existing LaunchTemplate.

- `isConstruct(x: any): boolean` (static) — Checks if `x` is a construct.

  Use this method instead of `instanceof` to properly detect `Construct`
  instances, even when the construct library is symlinked.

  Explanation: in JavaScript, multiple copies of the `constructs` library on
  disk are seen as independent, completely different libraries. As a
  consequence, the class `Construct` in each copy of the `constructs` library
  is seen as a different class, and an instance of one class will not test as
  `instanceof` the other class. `npm install` will not create installations
  like this, but users may manually symlink construct libraries together or
  use a monorepo tool: in those cases, multiple copies of the `constructs`
  library can be accidentally installed, and `instanceof` will behave
  unpredictably. It is safest to avoid using `instanceof`, and using
  this type-testing method instead.

  **Parameters**

  - `x` — Any object.

  **Returns:** true if `x` is an object created from a class which extends `Construct`.

- `isOwnedResource(construct: IConstruct): boolean` (static) — Returns true if the construct was created by CDK, and false otherwise.

- `isResource(construct: IConstruct): boolean` (static) — Check whether the given construct is a Resource.

- `toString(): string` — Returns a string representation of this construct.

- `with(...mixins: IMixin[]): IConstruct` — Applies one or more mixins to this construct.

  Mixins are applied in order. The list of constructs is captured at the
  start of the call, so constructs added by a mixin will not be visited.
  Use multiple `with()` calls if subsequent mixins should apply to added
  constructs.
