Your submission was sent successfully! Close

Thank you for contacting us. A member of our team will be in touch shortly. Close

You have successfully unsubscribed! Close

Thank you for signing up for our newsletter!
In these regular emails you will find the latest updates about Ubuntu and upcoming events where you can meet our team.Close

Search and launch Ubuntu 22.04 in AWS using CLI

1. Overview

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

2. Search for the right AMI

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.


3. Step 2: Launch it!

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.


4. That’s all Folks!

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

Did you find it useful?

How do you launch your instances on AWS?

Did you know about Ubuntu Pro?