AWS::EC2::LaunchTemplate - AWS CloudFormation

AWS::EC2::LaunchTemplate

Specifies a launch template for an Amazon EC2 instance. A launch template contains the parameters to launch an instance. 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, ... ] } }

Properties

LaunchTemplateData

The information for the launch template.

Required: No

Type: LaunchTemplateData

Update requires: No interruption

LaunchTemplateName

A name for the launch template.

Required: No

Type: String

Minimum: 3

Maximum: 128

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

Update requires: Replacement

TagSpecifications

The tags to apply to the launch template during creation.

Required: No

Type: List of LaunchTemplateTagSpecification

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.

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.

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