Search and launch Ubuntu 22.04 in AWS using CLI

Key Value
Summary Search and launch Ubuntu 22.04 in AWS using CLI
Categories cloud, server, aws
Difficulty 2
Author Carlos Bravo carlos.bravo@canonical.com

Overview

Duration: 1:00

We are super happy to announce that we have launched our new LTS version of Ubuntu, codenamed “Jammy Jellyfish”, just days ago. Follow this link if you want to learn more about Ubuntu 22.04

The good news is that it is already available on AWS and here we will show you how to launch it like a Pro.

This quick tutorial will guide you through the process of searching and launching an AMI using AWS CLI

What you’ll learn:

  • How Ubuntu AMI names are structured on AWS
  • How to search and get the latest AMI from the CLI
  • How to launch an AMI from the CLI

What you’ll need:

  • AWS CLI installed and configured
  • Basic understanding of AWS CLI

Search for the right AMI

Duration 5:00

Getting the latest AMIs with AWS CLI can get complicated if you haven’t found a personal preference to re-utilize, since there are several ways to search and get a list of AMIs. Here I will show you mine, which I have found out to be the fastest for me.

First, let’s learn how we structure AMIs names:

ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server-20220420
ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-arm64-server-20220420
ubuntu-minimal/images/hvm-ssd/ubuntu-jammy-22.04-amd64-minimal-20220420
ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-amd64-pro-server-20230420
ubuntu-pro-fips-server/images/hvm-ssd/ubuntu-focal-20.04-amd64-pro-fips-server
ubuntu-eks/k8s_1.21/images/hvm-ssd/ubuntu-focal-20.04-arm64-server-20220506

If you read carefully the pattern, you will see that there is a defined structure that goes as follows:

${FAMILY}/${RELEASE_STREAM}/${VIRT}-${STORAGE}/ubuntu-${SUITE}-${VERSION}-${ARCH}-server-${SERIAL}

FAMILY = ubuntu || ubuntu-minimal || ubuntu-pro-server || ubuntu-eks
RELEASE_STREAM = images (aka release) || images-testing (aka daily)
VIRT = hvm (there are no others anymore. pv was formerly available)
STORAGE = ssd || instance (instance only available on 18.04, and deprecated by AWS)
SUITE = lunar, kinetic, jammy, focal, bionic, xenial (written name of Ubuntu version)
VERSION = 23.04, 22.10, 22.04, 20.04, 18.04, 16.04 (numeric name of Ubuntu version
ARCH = amd64 || arm64
SERIAL = date of creation. This is a constantly moving value

Note:
For EKS images, the structure is similar but the product definition includes one extra level for the K8s version, e.g. ubuntu-eks/k8s_1.25/...

Note 2:
EKS images are currently also being published on AWS Marketplace, so searching ubuntu-eks images produce both results. The ones listed on marketplace are owned by AWS, with 679593333241 as Owner ID. They are the same except that the Marketplace AMIs need a subscription first before launch.

So, if you want to get the free version of Focal (20.04) for ARM, the AMI name would be:

ubuntu/images/hvm-ssd/ubuntu-focal-20.04-arm64-server-*

The corresponding version for Ubuntu Pro:

ubuntu-pro-server/images/hvm-ssd/ubuntu-focal-20.04-arm64-pro*

Note:
Always check for the AMI owner (publisher), as you may end up with an unsupported 3rd party AMI.

Official Ubuntu images on AWS are either published under AWS’ OwnerID 679593333241 or Canonical’s id 099720109477

Having that in mind, now we can search using the command: aws cli describe-images. Let’s search for Ubuntu Jammy 22.04 LTS:

aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server*"

But this will give you a long list of all the available releases for this version, so let’s add a way to get only the latest (Canonical releases periodically the latest images, so always search for the latest AMI)

We will use the built-in query mechanism, which also includes a sorting function.

The search for Ubuntu 22.04 LTS would be like this:

aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-jammy-22.04-amd64-server*" \
--query 'sort_by(Images, &CreationDate)[-1].{Name: Name, ImageId: ImageId, CreationDate: CreationDate, Owner:OwnerId}'

If we want to get the Pro version:

aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-amd64-pro*" \
--query 'sort_by(Images, &CreationDate)[-1].{Name: Name, ImageId: ImageId, CreationDate: CreationDate, Owner:OwnerId}'

You will get a single json object with the latest AMI published.

{
    "Name": "ubuntu-pro-server/images/hvm-ssd/ubuntu-jammy-22.04-amd64-pro-server-20230420",
    "ImageId": "ami-028f6d7ddc3079baf",
    "CreationDate": "2023-04-20T02:32:06.000Z",
    "Owner": "099720109477"
}

Note:
AMI IDs are unique per region. So if you need to use another region, you will get different AMI IDs. This is important to remember when automating pipelines, as you may get a lot of “AMI not found” errors.

Step 2: Launch it!

Duration: 5:00

We are going to launch it with the minimal information just for avoid complexity in this tutorial.

We will need to know: AMI ID (which we know now), the instance type (e.g. t2.micro), our key-pair filename, a security group id and a subnet id.

For this demo, we are going to use:

  • Ami: ami-028f6d7ddc3079baf (Ubuntu Pro Server, AMI for us-east-1 region)
  • Instance type: t2.micro
  • Key pair: my_key_pair (created before on EC2 web console)
  • Security group: sg-XXXXXXXX (created before on EC2 web console)
  • Subnet id: subnet-YYYYYYYY (created before on EC2 web console)

If you want to get security groups and subnets ids from the cli, these are the two instructions you need to know:

  • Subnets can be queried with aws ec2 describe-subnets
aws ec2 describe-subnets --query "Subnets[].[SubnetId, VpcId, AvailabilityZone]"
  • Security groups can be queried with aws ec2 describe-security-groups
aws ec2 describe-security-groups --query "SecurityGroups[].[Description, GroupId]"

Once you have all the information, you can launch instances with just one CLI

aws ec2 run-instances --image-id ami-028f6d7ddc3079baf \ 
--count 1 --instance-type t2.micro --key-name my_key_pair \
--security-group-ids sg-XXXXXXXX --subnet-id subnet-YYYYYYYY

Note:
Marketplace images require a subscription, so if you launch an AMI that is listed only on AWS Marketplace, it will raise you an error message with a link to the product page. Subscription requires you to login and click to subscribe, then the process will be the same as described above.

That’s all Folks!

Duration: 02:00

I hope you have enjoyed this tutorial. Please take a minute to let us learn more from you. Thanks!

Did you find it useful?

  • Yes, absolutely
  • Some parts only, as I knew the most parts of it
  • Not much really
0 voters

How do you launch your instances on AWS?

  • Only using CLI
  • Only using the web console
  • Using both
  • Using only automation mechanism
  • Somebody does it for me
0 voters

Did you know about Ubuntu Pro?

  • Yes, I currently run workloads on it
  • Yes, although I am not (yet) a user of Ubuntu Pro
  • Nope, I prefer the free version only
  • Nope, I use another distros
0 voters
2 Likes