LaunchTemplate

class aws_cdk.aws_ec2.LaunchTemplate(scope, id, *, associate_public_ip_address=None, block_devices=None, cpu_credits=None, detailed_monitoring=None, disable_api_termination=None, ebs_optimized=None, hibernation_configured=None, http_endpoint=None, http_protocol_ipv6=None, http_put_response_hop_limit=None, http_tokens=None, instance_initiated_shutdown_behavior=None, instance_metadata_tags=None, instance_profile=None, instance_type=None, key_name=None, key_pair=None, launch_template_name=None, machine_image=None, nitro_enclave_enabled=None, require_imdsv2=None, role=None, security_group=None, spot_options=None, user_data=None, version_description=None)

Bases: Resource

This represents an EC2 LaunchTemplate.

See:

https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-launch-templates.html

ExampleMetadata:

infused

Example:

# vpc: ec2.Vpc


sg1 = ec2.SecurityGroup(self, "sg1",
    vpc=vpc
)
sg2 = ec2.SecurityGroup(self, "sg2",
    vpc=vpc
)

launch_template = ec2.LaunchTemplate(self, "LaunchTemplate",
    machine_image=ec2.MachineImage.latest_amazon_linux2023(),
    security_group=sg1
)

launch_template.add_security_group(sg2)
Parameters:
  • scope (Construct) –

  • id (str) –

  • associate_public_ip_address (Optional[bool]) – Whether instances should have a public IP addresses associated with them. Default: - Use subnet settings

  • block_devices (Optional[Sequence[Union[BlockDevice, Dict[str, Any]]]]) – Specifies how block devices are exposed to the instance. You can specify virtual devices and EBS volumes. 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. Default: - Uses the block device mapping of the AMI

  • cpu_credits (Optional[CpuCredits]) – CPU credit type for burstable EC2 instance types. Default: - No credit type is specified in the Launch Template.

  • detailed_monitoring (Optional[bool]) – If set to true, then detailed monitoring will be enabled on instances created with this launch template. Default: False - Detailed monitoring is disabled.

  • disable_api_termination (Optional[bool]) – 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; otherwise, you can. Default: - The API termination setting is not specified in the Launch Template.

  • ebs_optimized (Optional[bool]) – Indicates whether the instances are optimized for Amazon EBS I/O. 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. Default: - EBS optimization is not specified in the launch template.

  • hibernation_configured (Optional[bool]) – 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.

  • http_endpoint (Optional[bool]) – Enables or disables the HTTP metadata endpoint on your instances. Default: true

  • http_protocol_ipv6 (Optional[bool]) – Enables or disables the IPv6 endpoint for the instance metadata service. Default: true

  • http_put_response_hop_limit (Union[int, float, None]) – The desired HTTP PUT response hop limit for instance metadata requests. The larger the number, the further instance metadata requests can travel. Default: 1

  • http_tokens (Optional[LaunchTemplateHttpTokens]) – The state of token usage for your instance metadata requests. The default state is optional if not specified. However, if requireImdsv2 is true, the state must be required. Default: LaunchTemplateHttpTokens.OPTIONAL

  • instance_initiated_shutdown_behavior (Optional[InstanceInitiatedShutdownBehavior]) – 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.

  • instance_metadata_tags (Optional[bool]) – 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. Default: false

  • instance_profile (Optional[IInstanceProfile]) – The instance profile used to pass role information to EC2 instances. Note: You can provide an instanceProfile or a role, but not both. Default: - No instance profile

  • instance_type (Optional[InstanceType]) – Type of instance to launch. Default: - This Launch Template does not specify a default Instance Type.

  • key_name (Optional[str]) – (deprecated) Name of SSH keypair to grant access to instance. Default: - No SSH access will be possible.

  • key_pair (Optional[IKeyPair]) – The SSH keypair to grant access to the instance. Default: - No SSH access will be possible.

  • launch_template_name (Optional[str]) – Name for this launch template. Default: Automatically generated name

  • machine_image (Optional[IMachineImage]) – The AMI that will be used by instances. Default: - This Launch Template does not specify a default AMI.

  • nitro_enclave_enabled (Optional[bool]) – If this parameter is set to true, the instance is enabled for AWS Nitro Enclaves; otherwise, it is not enabled for AWS Nitro Enclaves. Default: - Enablement of Nitro enclaves is not specified in the launch template; defaulting to false.

  • require_imdsv2 (Optional[bool]) – Whether IMDSv2 should be required on launched instances. Default: - false

  • role (Optional[IRole]) – An IAM role to associate with the instance profile that is used by instances. The role must be assumable by the service principal ec2.amazonaws.com. Note: You can provide an instanceProfile or a role, but not both. Default: - No new role is created.

  • security_group (Optional[ISecurityGroup]) – Security group to assign to instances created with the launch template. Default: No security group is assigned.

  • spot_options (Union[LaunchTemplateSpotOptions, Dict[str, Any], None]) – 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.

  • user_data (Optional[UserData]) – The AMI that will be used by instances. Default: - This Launch Template creates a UserData based on the type of provided machineImage; no UserData is created if a machineImage is not provided

  • version_description (Optional[str]) – A description for the first version of the launch template. The version description must be maximum 255 characters long. Default: - No description

Methods

add_security_group(security_group)

Add the security group to the instance.

Parameters:

security_group (ISecurityGroup) – : The security group to add.

Return type:

None

apply_removal_policy(policy)

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).

Parameters:

policy (RemovalPolicy) –

Return type:

None

to_string()

Returns a string representation of this construct.

Return type:

str

Attributes

connections

Allows specifying security group connections for the instance.

Note:

Only available if you provide a securityGroup when constructing the LaunchTemplate.

default_version_number

The default version for the launch template.

Attribute:

true

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

grant_principal

Principal to grant permissions to.

Note:

Only available if you provide a role when constructing the LaunchTemplate.

image_id

The AMI ID of the image to use.

Attribute:

true

instance_type

Type of instance to launch.

Attribute:

true

latest_version_number

The latest version of the launch template.

Attribute:

true

launch_template_id

The identifier of the Launch Template.

Exactly one of launchTemplateId and launchTemplateName will be set.

launch_template_name

The name of the Launch Template.

Exactly one of launchTemplateId and launchTemplateName will be set.

node

The tree node.

os_type

The type of OS the instance is running.

Attribute:

true

role

IAM Role assumed by instances that are launched from this template.

Attribute:

true

stack

The stack in which this resource is defined.

user_data

UserData executed by instances that are launched from this template.

Attribute:

true

version_number

The version number of this launch template to use.

Static Methods

classmethod from_launch_template_attributes(scope, id, *, launch_template_id=None, launch_template_name=None, version_number=None)

Import an existing LaunchTemplate.

Parameters:
  • scope (Construct) –

  • id (str) –

  • launch_template_id (Optional[str]) – The identifier of the Launch Template. Exactly one of launchTemplateId and launchTemplateName may be set. Default: None

  • launch_template_name (Optional[str]) – The name of the Launch Template. Exactly one of launchTemplateId and launchTemplateName may be set. Default: None

  • version_number (Optional[str]) – The version number of this launch template to use. Default: Version: “$Default”

Return type:

ILaunchTemplate

classmethod is_construct(x)

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) – Any object.

Return type:

bool

Returns:

true if x is an object created from a class which extends Construct.

classmethod is_owned_resource(construct)

Returns true if the construct was created by CDK, and false otherwise.

Parameters:

construct (IConstruct) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool