Launched EC2 instance UNREACHABLE for Ubuntu 20.04 AMI with python 3.9 upgrade

0

I am using EC2 Ubuntu 20.04 VM.

Due to CVE-2021-3177, Python needs to be upgraded to the latest version of Python3.9 which would be 3.9.5 currently.

I did that using the apt install option as per the steps mentioned below:

sudo apt update
sudo apt upgrade -y
sudo apt install python3.9

The above ensures that Python3.9.5 is now available. But now python3.8 & python3.9 is available. So next we will use the update-alternatives command to make python3.9 as the default version.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

Now that alternatives are defined, we will switch to Option 2 as the default option i.e. Python3.9

sudo update-alternatives --config python3

Once done, the following command would point to the latest version.

sudo python3 -V

However, if you use the sudo apt update command, you will see an error stating that

Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

To fix this we will have to add a link using the following command

cd /usr/lib/python3/dist-packages/
sudo ln -s apt-pkg.cpython-{38m,39m}-x86_64-linux-gnu.so

Next, I tried used the following commands

apt purge python3-apt
apt install python3-apt
sudo apt install python3.9-distutils python3.9-dev

Once done following command will now not result in any errors

sudo apt update

This means that the issue is fixed.

**I can use this machine & use it after reboot too. **

But for some reason, If I create an AMI and launch an instance then that instance is unreachable.

Appreciate your help.

1 Risposta
0

If you can't reach an instance after launch I'd suggest using the EC2 Serial Console to access the instance and see what the issue is.

profile pictureAWS
ESPERTO
con risposta 2 anni fa

Accesso non effettuato. Accedi per postare una risposta.

Una buona risposta soddisfa chiaramente la domanda, fornisce un feedback costruttivo e incoraggia la crescita professionale del richiedente.

Linee guida per rispondere alle domande