AWS::EC2::LaunchTemplate - AWS CloudFormation

AWS::EC2::LaunchTemplate

Specifies the properties for creating a launch template.

The minimum required properties for specifying a launch template are as follows:

  • You must specify at least one property for the launch template data.

  • You do not need to specify a name for the launch template. If you do not specify a name, AWS CloudFormation creates the name for you.

A launch template can contain some or all of the configuration information to launch an instance. When you launch an instance using a launch template, instance properties that are not specified in the launch template use default values, except the ImageId property, which has no default value. If you do not specify an AMI ID for the launch template ImageId property, you must specify an AMI ID for the instance ImageId property.

For more information, see Launch an instance from a launch template in the Amazon EC2 User Guide.

Syntax

To declare this entity in your AWS CloudFormation template, use the following syntax:

JSON

{ "Type" : "AWS::EC2::LaunchTemplate", "Properties" : { "LaunchTemplateData" : LaunchTemplateData, "LaunchTemplateName" : String, "TagSpecifications" : [ LaunchTemplateTagSpecification, ... ], "VersionDescription" : String } }

Properties

LaunchTemplateData

The information for the launch template.

Required: Yes

Type: LaunchTemplateData

Update requires: No interruption

LaunchTemplateName

A name for the launch template.

Required: No

Type: String

Pattern: [a-zA-Z0-9\(\)\.\-/_]+

Minimum: 3

Maximum: 128

Update requires: Replacement

TagSpecifications

The tags to apply to the launch template on creation. To tag the launch template, the resource type must be launch-template.

To specify the tags for the resources that are created when an instance is launched, you must use TagSpecifications.

Required: No

Type: Array of LaunchTemplateTagSpecification

Update requires: No interruption

VersionDescription

A description for the first version of the launch template.

Required: No

Type: String

Minimum: 0

Maximum: 255

Update requires: No interruption

Return values

Ref

When you pass the logical ID of this resource to the intrinsic Ref function, Ref returns the ID of the launch template, for example, lt-01238c059e3466abc.

For more information about using the Ref function, see Ref.

Fn::GetAtt

The Fn::GetAtt intrinsic function returns a value for a specified attribute of this type. The following are the available attributes and sample return values.

For more information about using the Fn::GetAtt intrinsic function, see Fn::GetAtt.

DefaultVersionNumber

The default version of the launch template, such as 2.

The default version of a launch template cannot be specified in AWS CloudFormation. The default version can be set in the Amazon EC2 console or by using the modify-launch-template AWS CLI command.

LatestVersionNumber

The latest version of the launch template, such as 5.

LaunchTemplateId

The ID of the launch template.

Examples

Launch template with an IAM instance profile

The following example creates a launch template and an instance profile. The instance profile contains the IAM role named MyAdminRole and can provide the role's temporary credentials to an application that runs on the instances created by this launch template.

The launch template also prevents accidental instance termination when using the Amazon EC2 console, CLI, or API, by specifying true for the DisableApiTermination property. If the instances created by this launch template are launched in a default VPC, they receive a public IP address by default. If the instances are launched in a nondefault VPC, they do not receive a public IP address by default.

To see another example that uses an IAM role, user data, and tags for the instances and volumes launched by that launch template, see Create launch templates with AWS CloudFormation.

JSON

{ "AWSTemplateFormatVersion":"2010-09-09", "Resources":{ "MyIamInstanceProfile":{ "Type":"AWS::IAM::InstanceProfile", "Properties":{ "InstanceProfileName":"MyIamInstanceProfile", "Path":"/", "Roles":["MyAdminRole"] } }, "MyLaunchTemplate":{ "Type":"AWS::EC2::LaunchTemplate", "Properties":{ "LaunchTemplateName":"MyLaunchTemplate", "LaunchTemplateData":{ "IamInstanceProfile":{ "Arn":{"Fn::GetAtt": ["MyIamInstanceProfile", "Arn"]} }, "DisableApiTermination":"true", "ImageId":"ami-04d5cc9b88example", "InstanceType":"t2.micro", "KeyName":"MyKeyPair", "SecurityGroupIds":[ "sg-083cd3bfb8example" ] } } } } }

YAML

AWSTemplateFormatVersion: '2010-09-09' Resources: MyIamInstanceProfile: Type: AWS::IAM::InstanceProfile Properties: InstanceProfileName: MyIamInstanceProfile Path: "/" Roles: - MyAdminRole MyLaunchTemplate: Type: AWS::EC2::LaunchTemplate Properties: LaunchTemplateName: MyLaunchTemplate LaunchTemplateData: IamInstanceProfile: Arn: !GetAtt - MyIamInstanceProfile - Arn DisableApiTermination: true ImageId: ami-04d5cc9b88example InstanceType: t2.micro KeyName: MyKeyPair SecurityGroupIds: - sg-083cd3bfb8example

Launch template with defined block device mapping

The following example creates a launch template with a block device mapping: an encrypted 22 gigabyte EBS volume mapped to /dev/xvdcz. The /dev/xvdcz volume uses the General Purpose SSD (gp2) volume type and is deleted when terminating the instance it is attached to. This example uses the Fn::Sub function to customize the name of the launch template to include the stack name.

The launch template also provisions T2 instances in unlimited mode by specifying a value of unlimited for the CPUCredits property. Because Monitoring is enabled, EC2 metric data will be available at 1-minute intervals (known as detailed monitoring) through CloudWatch.

JSON

{ "AWSTemplateFormatVersion": "2010-09-09", "Resources":{ "myLaunchTemplate":{ "Type":"AWS::EC2::LaunchTemplate", "Properties":{ "LaunchTemplateName":{"Fn::Sub":"${AWS::StackName}-launch-template"}, "LaunchTemplateData":{ "BlockDeviceMappings":[{ "Ebs":{ "VolumeSize":"22", "VolumeType":"gp2", "DeleteOnTermination": true, "Encrypted": true }, "DeviceName":"/dev/xvdcz" }], "CreditSpecification":{ "CpuCredits":"unlimited" }, "Monitoring":{"Enabled":true}, "ImageId":"ami-04d5cc9b88example", "InstanceType":"t2.micro", "KeyName":"MyKeyPair", "SecurityGroupIds":["sg-7c2270198example", "sg-903004f88example"] } } } } }

YAML

AWSTemplateFormatVersion: '2010-09-09' Resources: myLaunchTemplate: Type: AWS::EC2::LaunchTemplate Properties: LaunchTemplateName: !Sub ${AWS::StackName}-launch-template LaunchTemplateData: BlockDeviceMappings: - Ebs: VolumeSize: 22 VolumeType: gp2 DeleteOnTermination: true Encrypted: true DeviceName: /dev/xvdcz CreditSpecification: CpuCredits: Unlimited Monitoring: Enabled: true ImageId: ami-04d5cc9b88example InstanceType: t2.micro KeyName: MyKeyPair SecurityGroupIds: - sg-7c2270198example - sg-903004f88example

Launch template with public IP addresses for Amazon EC2 Auto Scaling

The following example creates and configures a launch template to assign public IP addresses to instances launched in a nondefault VPC. Note that when you specify a network interface for Amazon EC2 Auto Scaling, specify the VPC subnets as properties of the Auto Scaling group, and not in the launch template (because they will be ignored).

This example launch template also sets the instance placement tenancy to dedicated.

For more information about creating launch templates for Amazon EC2 Auto Scaling, see Creating a launch template for an Auto Scaling group in the Amazon EC2 Auto Scaling User Guide.

JSON

{ "AWSTemplateFormatVersion":"2010-09-09", "Resources":{ "myLaunchTemplate":{ "Type":"AWS::EC2::LaunchTemplate", "Properties":{ "LaunchTemplateName":{ "Fn::Sub":"${AWS::StackName}-launch-template-for-auto-scaling" }, "LaunchTemplateData":{ "NetworkInterfaces":[ { "DeviceIndex":0, "AssociatePublicIpAddress":true, "Groups":[ "sg-7c2270198example", "sg-903004f88example" ], "DeleteOnTermination":true } ], "Placement":{ "Tenancy": "dedicated" }, "ImageId":"ami-04d5cc9b88example", "InstanceType":"t2.micro", "KeyName":"MyKeyPair" } } } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Resources: myLaunchTemplate: Type: 'AWS::EC2::LaunchTemplate' Properties: LaunchTemplateName: !Sub '${AWS::StackName}-launch-template-for-auto-scaling' LaunchTemplateData: NetworkInterfaces: - DeviceIndex: 0 AssociatePublicIpAddress: true Groups: - sg-7c2270198example - sg-903004f88example DeleteOnTermination: true Placement: Tenancy: dedicated ImageId: ami-04d5cc9b88example InstanceType: t2.micro KeyName: MyKeyPair

See also