LaunchTemplate

class aws_cdk.aws_ec2.LaunchTemplate(scope, id, *, block_devices=None, cpu_credits=None, detailed_monitoring=None, disable_api_termination=None, ebs_optimized=None, hibernation_configured=None, instance_initiated_shutdown_behavior=None, instance_type=None, key_name=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)

Bases: Resource

This represents an EC2 LaunchTemplate.

See:

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

ExampleMetadata:

infused

Example:

boot_hook_conf = ec2.UserData.for_linux()
boot_hook_conf.add_commands("cloud-init-per once docker_options echo 'OPTIONS="${OPTIONS} --storage-opt dm.basesize=40G"' >> /etc/sysconfig/docker")

setup_commands = ec2.UserData.for_linux()
setup_commands.add_commands("sudo yum install awscli && echo Packages installed らと > /var/tmp/setup")

multipart_user_data = ec2.MultipartUserData()
# The docker has to be configured at early stage, so content type is overridden to boothook
multipart_user_data.add_part(ec2.MultipartBody.from_user_data(boot_hook_conf, "text/cloud-boothook; charset="us-ascii""))
# Execute the rest of setup
multipart_user_data.add_part(ec2.MultipartBody.from_user_data(setup_commands))

ec2.LaunchTemplate(self, "",
    user_data=multipart_user_data,
    block_devices=[]
)
Parameters:
  • scope (Construct) –

  • id (str) –

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

  • 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_type (Optional[InstanceType]) – Type of instance to launch. Default: - This Launch Template does not specify a default Instance Type.

  • key_name (Optional[str]) – Name of SSH keypair to grant access to 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: 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

Methods

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 construct tree node associated with this construct.

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)

Return whether the given object is a Construct.

Parameters:

x (Any) –

Return type:

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters:

construct (IConstruct) –

Return type:

bool