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-serve-7f441158…
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:

Product/images/disk type/product-codename-version-architecture-product_type-metadata

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.21/...

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 Focal 20.04 LTS:

aws ec2 describe-images --output json --region us-east-1 \
--filters "Name=name,Values=ubuntu/images/hvm-ssd/ubuntu-focal-20.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-serve-7f441158-49f7-496a-a358-7418a2164218-ami-085fc875b90696f81.4",
"ImageId": "ami-037211dc049411d37",
"CreationDate": "2022-04-21T16:28:51.000Z",
"Owner": "679593333241"
}

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-037211dc049411d37 (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-037211dc049411d37 \ 
--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