Launch instances from a launch template - Amazon Elastic Compute Cloud

Launch instances from a launch template

Launch templates are supported by several instance launch services. This topic describes how to use a launch template when launching an instance using the EC2 launch instance wizard, Amazon EC2 Auto Scaling, EC2 Fleet, and Spot Fleet.

Launch an instance from a launch template

You can use the parameters contained in a launch template to launch an instance. You have the option to override or add launch parameters before you launch the instance.

Instances that are launched using a launch template are automatically assigned two tags with the keys aws:ec2launchtemplate:id and aws:ec2launchtemplate:version. You cannot remove or edit these tags.

Console
To launch an instance from a launch template using the console
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Launch Templates.

  3. Select the launch template and choose Actions, Launch instance from template.

  4. For Source template version, select the launch template version to use.

  5. For Number of instances, specify the number of instances to launch.

  6. (Optional) You can override or add launch template parameters by changing and adding parameters in the Instance details section.

  7. Choose Launch instance from template.

AWS CLI
To launch an instance from a launch template using the AWS CLI
  • Use the run-instances command and specify the --launch-template parameter. Optionally specify the launch template version to use. If you don't specify the version, the default version is used.

    aws ec2 run-instances \ --launch-template LaunchTemplateId=lt-0abcd290751193123,Version=1
  • To override a launch template parameter, specify the parameter in the run-instances command. The following example overrides the instance type that's specified in the launch template (if any).

    aws ec2 run-instances \ --launch-template LaunchTemplateId=lt-0abcd290751193123 \ --instance-type t2.small
  • If you specify a nested parameter that's part of a complex structure, the instance is launched using the complex structure as specified in the launch template plus any additional nested parameters that you specify.

    In the following example, the instance is launched with the tag Owner=TeamA as well as any other tags that are specified in the launch template. If the launch template has an existing tag with a key of Owner, the value is replaced with TeamA.

    aws ec2 run-instances \ --launch-template LaunchTemplateId=lt-0abcd290751193123 \ --tag-specifications "ResourceType=instance,Tags=[{Key=Owner,Value=TeamA}]"

    In the following example, the instance is launched with a volume with the device name /dev/xvdb as well as any other block device mappings that are specified in the launch template. If the launch template has an existing volume defined for /dev/xvdb, its values are replaced with the specified values.

    aws ec2 run-instances \ --launch-template LaunchTemplateId=lt-0abcd290751193123 \ --block-device-mappings "DeviceName=/dev/xvdb,Ebs={VolumeSize=20,VolumeType=gp2}"

If the instance fails to launch or the state immediately goes to terminated instead of running, see Troubleshoot instance launch issues.

PowerShell
To launch an instance from a launch template using the AWS Tools for PowerShell
  • Use the New-EC2Instance command and specify the -LaunchTemplate parameter. Optionally specify the launch template version to use. If you don't specify the version, the default version is used.

    Import-Module AWS.Tools.EC2 New-EC2Instance ` -LaunchTemplate ( New-Object -TypeName Amazon.EC2.Model.LaunchTemplateSpecification -Property @{ LaunchTemplateId = 'lt-0abcd290751193123'; Version = '4' } )
  • To override a launch template parameter, specify the parameter in the New-EC2Instance command. The following example overrides the instance type that's specified in the launch template (if any).

    Import-Module AWS.Tools.EC2 New-EC2Instance ` -InstanceType t4g.small ` -LaunchTemplate ( New-Object -TypeName Amazon.EC2.Model.LaunchTemplateSpecification -Property @{ LaunchTemplateId = 'lt-0abcd290751193123'; Version = '4' } )
  • If you specify a nested parameter that's part of a complex structure, the instance is launched using the complex structure as specified in the launch template plus any additional nested parameters that you specify.

    In the following example, the instance is launched with the tag Owner=TeamA as well as any other tags that are specified in the launch template. If the launch template has an existing tag with a key of Owner, the value is replaced with TeamA.

    Import-Module AWS.Tools.EC2 New-EC2Instance ` -InstanceType t4g.small ` -LaunchTemplate ( New-Object -TypeName Amazon.EC2.Model.LaunchTemplateSpecification -Property @{ LaunchTemplateId = 'lt-0abcd290751193123'; Version = '4' } ) ` -TagSpecification ( New-Object -TypeName Amazon.EC2.Model.TagSpecification -Property @{ ResourceType = 'instance'; Tags = @( @{key = "Owner"; value = "TeamA" }, @{key = "Department"; value = "Operations" } ) } )

    In the following example, the instance is launched with a volume with the device name /dev/xvdb as well as any other block device mappings that are specified in the launch template. If the launch template has an existing volume defined for /dev/xvdb, its values are replaced with the specified values.

    Import-Module AWS.Tools.EC2 New-EC2Instance ` -InstanceType t4g.small ` -LaunchTemplate ( New-Object -TypeName Amazon.EC2.Model.LaunchTemplateSpecification -Property @{ LaunchTemplateId = 'lt-0abcd290751193123'; Version = '4' } ) ` -BlockDeviceMapping ( New-Object -TypeName Amazon.EC2.Model.BlockDeviceMapping -Property @{ DeviceName = '/dev/xvdb'; EBS = ( New-Object -TypeName Amazon.EC2.Model.EbsBlockDevice -Property @{ VolumeSize = 25; VolumeType = 'gp3' } ) } )

If the instance fails to launch or the state immediately goes to terminated instead of running, see Troubleshoot instance launch issues.

Use launch templates with Amazon EC2 Auto Scaling

You can create an Auto Scaling group and specify a launch template to use for the group. When Amazon EC2 Auto Scaling launches instances in the Auto Scaling group, it uses the launch parameters defined in the associated launch template. For more information, see Create a launch template for an Auto Scaling group and Create a launch template using advanced settings in the Amazon EC2 Auto Scaling User Guide.

Before you can create an Auto Scaling group using a launch template, you must create a launch template that includes the parameters required to launch an instance in an Auto Scaling group, such as the ID of the AMI. The console provides guidance to help you create a template that you can use with Amazon EC2 Auto Scaling.

To create a launch template to use with Auto Scaling using the console
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Launch Templates, and then choose Create launch template.

  3. For Launch template name, enter a descriptive name for the launch template.

  4. For Template version description, provide a brief description of this version of the launch template.

  5. Under Auto Scaling guidance, select the check box to have Amazon EC2 provide guidance to help create a template to use with Auto Scaling.

  6. Modify the launch parameters as required. Because you selected Auto Scaling guidance, some fields are required and some fields are not available. For information about how to configure the launch parameters for Amazon EC2 Auto Scaling, see Create a launch template for an Auto Scaling group and Create a launch template using advanced settings in the Amazon EC2 Auto Scaling User Guide.

  7. Choose Create launch template.

  8. (Optional) To create an Auto Scaling group using this launch template, on the Next steps page, choose Create Auto Scaling group.

For examples that show how to use the AWS CLI to create launch templates with various parameter combinations, see Examples for creating and managing launch templates with the AWS Command Line Interface (AWS CLI) in the Amazon EC2 Auto Scaling User Guide.

To create or update an Auto Scaling group with a launch template using the AWS CLI

For more information about creating or updating an Auto Scaling group using a launch template, see the following topics in the Amazon EC2 Auto Scaling User Guide.

Use launch templates with EC2 Fleet

You can create an EC2 Fleet request and specify a launch template in the instance configuration. When Amazon EC2 fulfills the EC2 Fleet request, it uses the launch parameters defined in the associated launch template. You can override some of the parameters that are specified in the launch template.

For more information, see Create an EC2 Fleet.

To create an EC2 Fleet with a launch template using the AWS CLI
  • Use the create-fleet command. Use the --launch-template-configs parameter to specify the launch template and any overrides for the launch template.

Use launch templates with Spot Fleet

You can create a Spot Fleet request and specify a launch template in the instance configuration. When Amazon EC2 fulfills the Spot Fleet request, it uses the launch parameters defined in the associated launch template. You can override some of the parameters that are specified in the launch template.

For more information, see Create a Spot Fleet request.

To create a Spot Fleet request with a launch template using the console
  1. Open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. In the navigation pane, choose Spot Requests.

  3. Choose Request Spot Instances.

  4. Under Launch parameters, choose Use a launch template.

  5. For Launch template, choose a launch template, and then, from the field to the right, choose the launch template version.

  6. Configure your Spot Fleet by selecting different options on this screen. For more information about the options, see Create a Spot Fleet request using defined parameters (console).

  7. When you're ready to create your Spot Fleet, choose Launch.

To create a Spot Fleet request with a launch template using the AWS CLI
  • Use the request-spot-fleet command. Use the LaunchTemplateConfigs parameter to specify the launch template and any overrides for the launch template.