0
0
AWScloud~5 mins

Launch templates in AWS - Commands & Configuration

Choose your learning style9 modes available
Introduction
Launch templates help you save settings for virtual servers in the cloud so you can create new servers quickly and consistently without typing all details every time.
When you want to create many virtual servers with the same settings easily.
When you need to update server settings and apply them to new servers without mistakes.
When you want to keep track of different versions of server configurations.
When you want to launch servers automatically using saved settings in scripts or cloud services.
When you want to avoid typing long commands repeatedly to create servers.
Config File - launch-template.json
launch-template.json
{
  "LaunchTemplateName": "example-launch-template",
  "VersionDescription": "Initial version",
  "LaunchTemplateData": {
    "ImageId": "ami-0abcdef1234567890",
    "InstanceType": "t3.micro",
    "KeyName": "example-keypair",
    "SecurityGroupIds": ["sg-0123456789abcdef0"],
    "TagSpecifications": [
      {
        "ResourceType": "instance",
        "Tags": [
          {"Key": "Name", "Value": "example-instance"}
        ]
      }
    ]
  }
}

This JSON file defines a launch template named example-launch-template.

LaunchTemplateData holds the server settings: the AMI image ID to use, the instance type (size), the SSH key for access, and the security group for network rules.

TagSpecifications add a name tag to the server for easy identification.

Commands
This command creates a launch template in AWS using the settings from the JSON file. It saves the server configuration for reuse.
Terminal
aws ec2 create-launch-template --cli-input-json file://launch-template.json
Expected OutputExpected
{ "LaunchTemplate": { "LaunchTemplateId": "lt-0abcd1234efgh5678", "LaunchTemplateName": "example-launch-template", "CreateTime": "2024-06-01T12:00:00.000Z", "CreatedBy": "123456789012", "LatestVersionNumber": 1, "DefaultVersionNumber": 1 } }
--cli-input-json - Specifies the JSON file with launch template settings
This command checks that the launch template was created and shows its details.
Terminal
aws ec2 describe-launch-templates --launch-template-names example-launch-template
Expected OutputExpected
{ "LaunchTemplates": [ { "LaunchTemplateId": "lt-0abcd1234efgh5678", "LaunchTemplateName": "example-launch-template", "CreateTime": "2024-06-01T12:00:00.000Z", "CreatedBy": "123456789012", "LatestVersionNumber": 1, "DefaultVersionNumber": 1 } ] }
--launch-template-names - Specifies the name of the launch template to describe
This command launches a new virtual server using the saved launch template settings.
Terminal
aws ec2 run-instances --launch-template LaunchTemplateName=example-launch-template
Expected OutputExpected
{ "Instances": [ { "InstanceId": "i-0123456789abcdef0", "ImageId": "ami-0abcdef1234567890", "InstanceType": "t3.micro", "State": {"Name": "pending"}, "LaunchTime": "2024-06-01T12:05:00.000Z" } ] }
--launch-template - Specifies which launch template to use for the new instance
This command checks the status and details of the newly launched server to confirm it is running.
Terminal
aws ec2 describe-instances --instance-ids i-0123456789abcdef0
Expected OutputExpected
{ "Reservations": [ { "Instances": [ { "InstanceId": "i-0123456789abcdef0", "State": {"Name": "running"}, "InstanceType": "t3.micro", "LaunchTime": "2024-06-01T12:05:00.000Z" } ] } ] }
--instance-ids - Specifies the instance ID to describe
Key Concept

If you remember nothing else from this pattern, remember: launch templates save your server settings so you can create new servers quickly and without mistakes.

Common Mistakes
Using incorrect or missing AMI image ID in the launch template.
The server will fail to launch because it cannot find the image to use.
Always verify the AMI ID is valid and available in your AWS region before creating the launch template.
Trying to launch an instance without specifying the launch template name correctly.
AWS will return an error because it does not know which template to use.
Use the exact launch template name with the --launch-template flag when running instances.
Not checking the launch template creation output before launching instances.
You might try to use a template that was not created successfully, causing failures.
Always run describe-launch-templates to confirm the template exists and is ready.
Summary
Create a launch template JSON file with your server settings.
Use aws ec2 create-launch-template to save the template in AWS.
Check the template exists with aws ec2 describe-launch-templates.
Launch new servers using aws ec2 run-instances with the launch template.
Verify the server status with aws ec2 describe-instances.