Skip to content

Instantly share code, notes, and snippets.

@alanmur
Last active September 10, 2021 23:42
Show Gist options
  • Save alanmur/5cc219270272cb8b28ebfeb6b611deec to your computer and use it in GitHub Desktop.
Save alanmur/5cc219270272cb8b28ebfeb6b611deec to your computer and use it in GitHub Desktop.
Update an Amazon Linux 2 image with AWS CLI V2 (properly)

Update an Amazon Linux 2 image with AWS CLI V2 (properly)

When an Amazon Linux 2 AMI is started, it may be on an old version of the AWS CLI and Python, such as the following.

2021-09-10_18-14-17

Procedure

Upgrade by pasting the following command block at the EC2 command prompt.

aws --version
sudo rm -rf /usr/lib/python2.7/site-packages/awscli/
sudo rm -rf /usr/lib/python2.7/site-packages/botocore/
sudo rm /usr/bin/aws
sudo rm /usr/bin/aws_completer
sudo rm /usr/share/zsh/site-functions/aws_zsh_completer.sh
sudo rm /etc/bash_completion.d/aws_bash_completer
sudo yum update -y
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -q -o awscliv2.zip
sudo ./aws/install --update
bash
aws --version

You should expect to see the output look a bit like this:

2021-09-10_18-16-30

Troubleshooting

First, make sure you are using an Amazon Linux 2 image!

2021-09-10_18-27-46

If you are the root user, instead of ec2-user (for example if you are using the AL2 container image or unofficial WSL image by yosukes-dev) then sudo will not work as super user escalation is not needed. Use this command block instead, from which the sudo commands have been removed.

aws --version
rm -rf /usr/lib/python2.7/site-packages/awscli/
rm -rf /usr/lib/python2.7/site-packages/botocore/
rm /usr/bin/aws
rm /usr/bin/aws_completer
rm /usr/share/zsh/site-functions/aws_zsh_completer.sh
rm /etc/bash_completion.d/aws_bash_completer
yum update -y
curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -q -o awscliv2.zip
./aws/install --update
bash
aws --version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment