Bootstrap FreeKB - Amazon Web Services (AWS) - Create a Launch Template using the AWS CLI
Amazon Web Services (AWS) - Create a Launch Template using the AWS CLI

Updated:   |  Amazon Web Services (AWS) articles

This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

An EC2 Launch Template is a way to set certain defaults for launching a new EC2 instance, such as the SSH Key Pairs, Network Subnets, Security Groups, Amazon Machine Image, et cetera that the EC2 Instance will have. An EC2 Launch Template is often used with an EC2 Auto Scaling Group, to auto scale a collection of EC2 instances based on needs, such as when load is high or low is low.

You will need a Launch Template before you can create and setup the EC2 Auto Scaling Group. The launch template will need to use a specific Amazon Machine Image (AMI). The aws ec2 describe-images command can be used to list the available Amazon Machine Images.

~]$ aws ec2 describe-images --filters "Name=name,Values=al2023-ami-2023*x86_64*" | egrep '\"Name\"|\"ImageId\"'
            "ImageId": "ami-0759f51a90924c166",
            "Name": "al2023-ami-2023.3.20231211.4-kernel-6.1-x86_64"
            "ImageId": "ami-0e9107ed11be76fde",
            "Name": "al2023-ami-2023.3.20240117.0-kernel-6.1-x86_64"
            "ImageId": "ami-0277155c3f0ab2930",
            "Name": "al2023-ami-2023.3.20240131.0-kernel-6.1-x86_64"
            "ImageId": "ami-0a3c3a20c09d6f377",
            "Name": "al2023-ami-2023.3.20240122.0-kernel-6.1-x86_64"
            "ImageId": "ami-0230bd60aa48260c6",
            "Name": "al2023-ami-2023.2.20231113.0-kernel-6.1-x86_64"
            "ImageId": "ami-0005e0cfe09cc9050",
            "Name": "al2023-ami-2023.3.20240108.0-kernel-6.1-x86_64"
            "ImageId": "ami-079db87dc4c10ac91",
            "Name": "al2023-ami-2023.3.20231218.0-kernel-6.1-x86_64"

 

Then the aws ec2 create-launch-template command can be used to create the Launch Template.

aws ec2 create-launch-template \
--launch-template-name my-launch-template \
--version-description v1 \
--launch-template-data 
  '{"NetworkInterfaces":
     [
       {
         "AssociatePublicIpAddress":false,
         "DeviceIndex":0
       }
     ],
    "ImageId":"ami-0277155c3f0ab2930",
    "InstanceType":"t2.micro",
    "KeyName":"my-ssh-key",
    "TagSpecifications":
    [
      {
        "ResourceType":"instance",
        "Tags":
        [
          {
             "Key":"Name",
             "Value":"my-instance"
          }
        ]
      }
    ]
  }'

 

And here is an example where the Launch Template will assign a public IPv4 and IPv6 address to the instance in subnet-11111111111111111. The aws ec2 describe-subnets command can be used to determine the ID of the subnet you want to use.

aws ec2 create-launch-template \
--launch-template-name my-launch-template \
--version-description v1 \
--launch-template-data 
  '{"NetworkInterfaces":
     [
       {
         "AssociatePublicIpAddress":true,
         "DeviceIndex":0,
         "Ipv6AddressCount":1,
         "SubnetId":"subnet-11111111111111111"
       }
     ],
    "ImageId":"ami-0277155c3f0ab2930",
    "InstanceType":"t2.small"
   }'

 

The aws ec2 describe-launch-templates command can be used to list your Launch Templates.

~]$ aws ec2 describe-launch-templates
{
    "LaunchTemplates": [
        {
            "LatestVersionNumber": 1,
            "LaunchTemplateId": "lt-0dfee84b8657928cb",
            "LaunchTemplateName": "my-launch-template",
            "DefaultVersionNumber": 1,
            "CreatedBy": "arn:aws:iam::123456789012:user/johndoe",
            "CreateTime": "2024-02-06T02:43:24.000Z"
        }
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8373cc in the box below so that we can be sure you are a human.