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.
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:
First, make sure you are using an Amazon Linux 2 image!
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