Skip to main content

Upgrade to Amazon Linux 2023

Published

2026-12-01

WarningCaution

This chapter is under review. Thank you for your patience.

The instance I launched in Stand up an EC2 instance runs Amazon Linux 2 (AL2). This worked fine when do4ds was written, but it started breaking down once I started installing language runtimes.

This new chapter replaces the AL2 with an Amazon Linux 2023 (AL2023) instance. I’ve opted to keep both versions because it’s likely you’ll have to perform an upgrade (i.e., we need to before any of the installs in App Dependencies).

Why upgrade?

The login banner already hints at the problem:

  ~~     \###|       AL2 End of Life is 2026-06-30.
  ~~       \#/ ___
   ~~       V~' '->
    ~~~         /    A newer version of Amazon Linux is available!

AL2 stopped receiving updates on that date, so upgrading before the ‘End of Life’ is obviously idea. The issue for the examples in the book is the C library it uses.

AL2 ships glibc 2.26, and modern prebuilt binaries expect something newer. I discovered this when I tried to install R with rig:

sudo /usr/local/bin/rig add 4.6.1
✗ Cannot download JSON: HTTP status client error (404 Not Found) for url (https://api.r-hub.io/rversions/resolve/4.6.1/linux-amzn-2/x86_64)
✗ Failed to resolve R version: Cannot download JSON: HTTP status client error (404 Not Found) for url (https://api.r-hub.io/rversions/resolve/4.6.1/linux-amzn-2/x86_64)
✗ Unsupported glibc version 2.26: user-mode R installation requires glibc 2.34 or later

The output reports two failures. First, there is no R build for linux-amzn-2. Second, the fallback build needs glibc 2.34. AL2023 ships glibc 2.34, and R builds exist for it.

When should I upgrade?

You don’t have to upgrade an instance that already does its job, but the timing matters. Upgrade before you install anything you’d have to rebuild. For this book, that point is the start of Lab 10.

Labs 7 through 9 only need SSH, users, and the shell, and those work the same on both versions. Everything after Lab 10 depends on the operating system: language runtimes, systemd services, nginx, and TLS certificates. Each command tested on AL2 would need retesting on AL2023.

Outside this book, the same rule applies. Three signals say it’s time:

  1. The server/OS you’re using reaches end of life
  2. A package you need drops support for the version you’re using, or
  3. You’re about to build something new on the instance

Each one makes the move cheaper now than later.

How the upgrade works

There’s no in-place upgrade from AL2 to AL2023. AWS treats AL2023 as a new operating system, so the upgrade means launching a new instance and retiring the old one. The diagram below shows the order.

An in-place upgrade updates the operating system on the running instance instead of replacing it. It fits when the upgrade stays within one major version:

  • On AL2, sudo yum update installs security patches and package updates.
  • On AL2023, sudo dnf upgrade --releasever=latest moves the instance to the newest AL2023 release.

Some other distributions also support a major version upgrade in place (Ubuntu has do-release-upgrade). That’s reasonable for an instance that’s hard to rebuild, as long as we take an EBS snapshot first so we can roll back.

Moving between major versions in place is riskier: every package, library, and service changes at once, and a failure can leave the instance unable to boot. AL2 to AL2023 isn’t offered at all, so we replace the instance. A replacement also gives us a clean system and keeps the old instance as a fallback until the new one works.

%%{init: {'theme': 'base', 'look': 'classic', 'themeVariables': {'fontFamily': 'monospace', "fontSize":"12px"}}}%%

flowchart TD
    Start(["AL2 instance<br>running"])
    Ami("Find the newest<br>AL2023 AMI")
    Launch("Launch the new instance<br>with the same key, group, and role")
    Login("Log in and confirm<br>the OS and glibc")
    Copy("Copy files from<br>the old instance")
    Check{"Everything<br>needed copied?"}
    Term(["Terminate the<br>AL2 instance"])

    Start -->|"i."| Ami
    Ami -->|"ii."| Launch
    Launch -->|"iii."| Login
    Login -->|"iv."| Copy
    Copy --> Check
    Check -->|"yes"| Term
    Check -->|"no"| Copy

    style Start fill:#d2562b,stroke:#fff,stroke-width:2px,color:#fff
    style Ami fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
    style Launch fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
    style Login fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
    style Copy fill:#2a6f77,stroke:#fff,stroke-width:1px,color:#fff
    style Check fill:#e8a33d,stroke:#000,stroke-width:2px,color:#000
    style Term fill:#5b8c5a,stroke:#fff,stroke-width:2px,color:#fff

Replacing an AL2 instance with AL2023

The old instance keeps running until the last step. If anything goes wrong on the new one, I can still log into the old one and get what I need.

Finding the AL2023 AMI

An Amazon Machine Image (AMI) is the template a new instance boots from. The launch command in Stand up an EC2 instance used a fixed AMI ID. That ID pins the instance to whatever image was current when I wrote it. AWS publishes new AL2023 images every few weeks, so I look up the newest one instead.

First we need to log into the aws cli:

aws login

This will take you to the AWS sign in page in the browser. The page asks whether to sign in as the root user or as an IAM user: use the IAM user (the account ID or alias, IAM username, and password).

The root user is the email address that created the account. It has unrestricted access that no policy can limit. I only sign in as root for the few tasks that require it: billing and payment settings, changing the root email or password, closing the account, or restoring access if every IAM admin is locked out. Launching instances and looking up AMIs are everyday tasks, and an IAM user with the right policy handles them.

Console Home after signing in as the mjfrigaard-cli IAM user (top right)

Console Home after signing in as the mjfrigaard-cli IAM user (top right)

With the CLI signed in, I look up the newest AL2023 image:

# these are lookup/filter commands (think 'search and find')
aws ec2 describe-images \
  --owners amazon \
  --filters "Name=name,Values=al2023-ami-2023.*-x86_64" \
            "Name=state,Values=available" \
  --query "sort_by(Images, &CreationDate)[-1].[ImageId,Name]" \
  --output text \
  --profile dev-deploy
1
Lists Amazon Machine Images (AMIs). An AMI is the disk template a new instance boots from.
2
Keeps only images Amazon publishes, which rules out look-alike community images.
3
Matches names starting with al2023-ami-2023. and ending in -x86_64. The * covers the version and kernel.
4
Drops images that are still being built or have been retired. Several filters must all match.
5
A JMESPath expression run on the result. sort_by(..., &CreationDate) orders the images oldest to newest, [-1] takes the last (newest) one, and [ImageId,Name] keeps just those two fields.
6
Prints plain tab-separated text instead of JSON.
7
Uses the dev-deploy credentials from ~/.aws/credentials.

The filter matches the standard AL2023 images (not the minimal ones), and the query sorts them by date and keeps the last:

ami-0d00ab3a044f7f788   al2023-ami-2023.12.20261120.0-kernel-6.12-x86_64

Your AMI ID will differ, because each region and each release has its own. On a Graviton (arm64) instance type, change x86_64 to arm64 in the filter.

AWS also publishes the newest AMI ID as a public SSM parameter (/aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64), and run-instances can read it directly with --image-id resolve:ssm:.... Both need the ssm:GetParameters permission. The dev-deploy user doesn’t have it, so this chapter uses describe-images, which only needs the EC2 read access we already granted.

Launching the new instance

The new instance reuses everything from Stand up an EC2 instance: the key pair, the security group, and the instance profile. Only the AMI changes. I store the AMI ID in a shell variable and add a Name tag, so the two instances are easy to tell apart in the console:

# store shell variable
AMI_ID=$(aws ec2 describe-images \
  --owners amazon \
  --filters "Name=name,Values=al2023-ami-2023.*-x86_64" \
            "Name=state,Values=available" \
  --query "sort_by(Images, &CreationDate)[-1].ImageId" \
  --output text \
  --profile dev-deploy)
# use stored variable
NEW_INSTANCE_ID=$(aws ec2 run-instances \
  --image-id "$AMI_ID" \
  --instance-type t3.micro \
  --key-name aws-dev \
  --security-groups dev-sg \
  --iam-instance-profile Name=ec2-s3-instance-profile \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=dev-al2023}]' \
  --query "Instances[0].InstanceId" \
  --output text \
  --profile dev-deploy)

echo "$NEW_INSTANCE_ID"
1
Runs the AMI lookup from the previous section and stores its output in the shell variable AMI_ID.
2
Keeps only images Amazon publishes.
3
Matches the standard AL2023 x86_64 images.
4
Drops images that are still being built or have been retired.
5
Sorts by date and keeps only the newest image’s ID (no name), so AMI_ID holds just the ID.
6
Prints plain text, so AMI_ID holds ami-... without JSON quotes.
7
Uses the dev-deploy credentials. The ) closes the command substitution.
8
Launches one new instance and stores its ID in the shell variable NEW_INSTANCE_ID.
9
Boots it from the AMI stored in AMI_ID.
10
Sets the instance size: 2 vCPUs and 1 GiB of memory.
11
Installs the public half of our key pair, so our private key can log in.
12
Attaches the same security group as the old instance (SSH on port 22).
13
Attaches the role that lets the instance read the S3 bucket without access keys.
14
Names the instance dev-al2023, so it’s easy to spot in the console.
15
Keeps only the new instance ID from the long JSON response.
16
Prints plain text, so NEW_INSTANCE_ID holds i-... without JSON quotes.
17
Uses the dev-deploy credentials. The ) closes the command substitution.
18
Prints the new instance ID, so we can note it.

Both variables last until we close the terminal. If we open a new one, we set NEW_INSTANCE_ID again with the ID that echo printed (for example, NEW_INSTANCE_ID=i-0123456789abcdef0).

The instance starts in the pending state. Instead of checking again by hand, aws ec2 wait blocks until it’s running, and then I look up its public IP address:

aws ec2 wait instance-running \
  --instance-ids "$NEW_INSTANCE_ID" \
  --profile dev-deploy

aws ec2 describe-instances \
  --instance-ids "$NEW_INSTANCE_ID" \
  --query "Reservations[*].Instances[*].PublicIpAddress" \
  --output text \
  --profile dev-deploy
1
Checks the instance every 15 seconds and returns once it’s running (it gives up after 40 checks).
2
Reads the instance ID stored in NEW_INSTANCE_ID.
3
Uses the dev-deploy credentials.
4
Returns the details for one or more instances.
5
Limits the details to the new instance.
6
Keeps only the public IP address. The response groups instances inside reservations (one per launch request), so the query steps through both.
7
Prints the IP address as plain text.
8
Uses the dev-deploy credentials.

Logging into the new instance

The login is the same as before: same private key, same ec2-user account, new IP address.

ssh -i ~/.ssh/aws-dev.pem ec2-user@NEW_PUBLIC_IP_ADDRESS

The first connection asks me to confirm the host’s fingerprint, because my machine has never seen this server. Type yes. The AL2023 banner confirms I’m on the new instance:

Warning: Permanently added 'NEW_PUBLIC_IP_ADDRESS' (ED25519) to the list of known hosts.
   ,     #_
   ~\_  ####_        Amazon Linux 2023
  ~~  \_#####\
  ~~     \###|
  ~~       \#/ ___   https://aws.amazon.com/linux/amazon-linux-2023
   ~~       V~' '->
    ~~~         /
      ~~._.   _/
         _/ _/
       _/m/'
[ec2-user@ip-xxx-xx-x-xxx ~]$

If ssh times out instead, the port 22 rule in dev-sg probably doesn’t match my current IP address. This fix is covered in the next chapter (Logging into the instance).

I can confirm the operating system and the glibc version that caused the original failure:

grep PRETTY_NAME /etc/os-release
PRETTY_NAME="Amazon Linux 2023.12.20261120"
ldd --version | head -1
ldd (GNU libc) 2.34

Copying files from the old instance

Most of what this book puts on the instance comes from somewhere else: the data comes from S3, the code comes from GitHub, and all the software comes from installers. Anything I’ve changed by hand on the old instance is the exception. For me, that’s my ~/.bashrc and a few scratch files.

I copy them through my local machine with scp, which works over the same SSH connection.

scp (secure copy) needs nothing new: it uses the key we already log in with, the port 22 rule already in the security group, and it comes installed on macOS, Linux, Windows, and both instances. The file is encrypted on the way, just like an SSH session. Copying through the local machine also means the private key never has to go onto a server, which it would if one instance copied straight to the other.

Both commands need two things: the private key and each instance’s public IP address.

The key is the .pem file that create-key-pair saved in Stand up an EC2 instance: ~/.ssh/aws-dev.pem. We launched the new instance with the same key pair, so one file opens both instances.

For the IP addresses, I list the running instances from the local machine:

# don't run this on either instances
aws ec2 describe-instances \
  --filters "Name=instance-state-name,Values=running" \
  --query "Reservations[].Instances[].[InstanceId,PublicIpAddress,Tags[?Key=='Name']|[0].Value]" \
  --output text \
  --profile dev-deploy
1
Returns the details for every instance in the region.
2
Keeps only instances that are running.
3
Keeps the instance ID, the public IP address, and the Name tag of each one.
4
Prints one instance per line as plain text.
5
Uses the dev-deploy credentials.
i-0069201f3688cec43 OLD_PUBLIC_IP_ADDRESS   None
i-0514a57a209efcf56 NEW_PUBLIC_IP_ADDRESS   dev-al2023

The new instance has the dev-al2023 tag we added at launch. The old one has no Name tag (None), and its ID matches the one from Stand up an EC2 instance. I store both addresses in shell variables, so the scp commands don’t need editing:

OLD_IP=OLD_PUBLIC_IP_ADDRESS
NEW_IP=NEW_PUBLIC_IP_ADDRESS

Run these from the local machine, not from either instance. The first downloads ~/.bashrc from the old instance and saves it locally as bashrc-al2:

scp -i ~/.ssh/aws-dev.pem \
  ec2-user@"$OLD_IP":~/.bashrc ./bashrc-al2
.bashrc                         100%  274     1.9KB/s   00:00

The second uploads bashrc-al2 to the home directory on the new instance:

scp -i ~/.ssh/aws-dev.pem \
  ./bashrc-al2 ec2-user@"$NEW_IP":~/bashrc-al2
bashrc-al2                      100%  274     3.8KB/s   00:00

Each scp prints the file name, the percent copied, the size in bytes, the transfer speed, and the time it took.

If the first scp hangs and then fails like this, the old instance’s security group doesn’t allow my current IP address:

ssh: connect to host OLD_PUBLIC_IP_ADDRESS port 22: Connection timed out
scp: Connection closed

My home IP changes now and then, and each security group keeps its own port 22 rule. The new group has the current address because I just created it, but the old one still has the address from when I launched the old instance. The fix is the same as for ssh: replace the old address in the old security group (see Logging into the instance).

I copy the old .bashrc under a different name on purpose. AL2023 ships its own .bashrc, so I compare the two and move over only the lines I added (like PATH changes), instead of overwriting the new file. The comparison runs on the new instance, so I log in first:

ssh -i ~/.ssh/aws-dev.pem ec2-user@"$NEW_IP"
diff ~/bashrc-al2 ~/.bashrc
7a8,14
> # User specific environment
> if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
> then
>     PATH="$HOME/.local/bin:$HOME/bin:$PATH"
> fi
> export PATH
>
12c19,27
< export PATH="/opt/python/3.12.1/bin:$PATH"
---
> if [ -d ~/.bashrc.d ]; then
>     for rc in ~/.bashrc.d/*; do
>         if [ -f "$rc" ]; then
>             . "$rc"
>         fi
>     done
> fi
>
> unset rc

Lines starting with < are only in the old file, and lines starting with > are only in the new one. The > lines are AL2023’s defaults: it adds ~/.local/bin and ~/bin to PATH, and it loads every file in ~/.bashrc.d. The only line I added on AL2 is the /opt/python PATH entry, and it points to software that isn’t on the new instance (see below). So there’s nothing to move over, and I delete the copy:

rm ~/bashrc-al2

If I do have lines to keep, I put them in a file under ~/.bashrc.d (for example, ~/.bashrc.d/paths.sh) instead of editing ~/.bashrc. The new .bashrc loads them, and my changes stay separate from AL2023’s defaults.

Don’t copy software installed under /opt or /usr/local. It was built against AL2’s libraries and may not run on AL2023. App Dependencies reinstalls it on the new instance.

What changes on AL2023

Most of what I do on the command line works the same, but a few differences show up right away. The table below lists the ones this book runs into.

Area Amazon Linux 2 Amazon Linux 2023
Package manager yum dnf (yum still works as an alias)
Extra repositories amazon-linux-extras Removed; packages are in the main repository
glibc 2.26 2.34
System Python 3.7 3.9
curl Full curl curl-minimal (fewer protocols)
cron Installed Not installed; use systemd timers or dnf install cronie
Default user ec2-user ec2-user

The first two rows matter most. Commands later in the book use dnf, and none of them need amazon-linux-extras.

Terminating the old instance

Once the new instance has everything I need, the old one only costs money. Before I remove it, I check for anything left behind. I log into the old instance one more time and look at the home directory and /opt:

ls -la ~
ls /opt
ImportantWarning

Terminating an instance can’t be undone. Its root volume is deleted with it by default, so anything you didn’t copy is gone. If you’re unsure, stop the instance with aws ec2 stop-instances instead and terminate it later. A stopped instance doesn’t bill for compute, but its volume still costs a small amount each month.

When I’m sure, I terminate the old instance from my local machine:

aws ec2 terminate-instances \
  --instance-ids i-0069201f3688cec43 \
  --profile dev-deploy
{
    "TerminatingInstances": [
        {
            "InstanceId": "i-0069201f3688cec43",
            "CurrentState": {
                "Code": 32,
                "Name": "shutting-down"
            },
            "PreviousState": {
                "Code": 16,
                "Name": "running"
            }
        }
    ]
}

The response shows the state changing from running (code 16) to shutting-down (code 32). To wait until it’s gone, aws ec2 wait works here too. It returns with no output once the instance is terminated:

aws ec2 wait instance-terminated \
  --instance-ids i-0069201f3688cec43 \
  --profile dev-deploy

The remaining chapters use the new instance ID wherever an instance ID appears. If you followed along, replace it with your own.

Recap

AL2 reached end of life on 2026-06-30, and its glibc 2.26 is too old for modern prebuilt binaries such as R from rig. In this chapter we:

  1. Found the newest AL2023 AMI with describe-images, instead of hard coding an AMI ID.
  2. Launched a new instance with the same key pair, security group, and instance profile.
  3. Logged in and confirmed AL2023 and glibc 2.34.
  4. Copied hand-edited files through the local machine, and left installed software behind.
  5. Terminated the old instance once nothing on it was needed.

In the next chapter, App Dependencies, we install Python and R on the new instance.