0

[Solved] Launch EC2 instance from AMI – Cloud-init doesn’t run correctly at first boot

I want to share my case here. (already solved: Cloud-init EC2 error)

The Case

I have an EC2 instance (Ubuntu 18.04, i called original instance) and i want cloning this instance as AMI (Amazon Machine Images), then launch a new instance by using this AMI.

But, after launched new EC2 instance from AMI, the EC2 status is not passed (1/2 checks passed), like below:

After getting this issue, i check the log, on Monitoring and troubleshoot > Get system log and i found some errors:

Cloud-init EC2 error

As you can see, cloud-init was running some tasks using python3. And all error above related to python script (see the extension .py). And look at the last error is ModuleNotFoundError: No module named 'requests'.

I assume if module ‘requests’ is not installed on python3 yet. So i need to install it on the original instance, then re-create AMI.

sudo apt-get install python3-requests

after running that command, the response turns out: python3-requests is already the newest version. That means module ‘requests’ is already installed, so i don’t need to install again.

And the question is, if module ‘requests’ is already, why it doesn’t work during cloud-init initiation?


The cause and solution

After some searches on the internet and some attempts by myself. Finnaly i got the cause and a way to fix it.

Cause:

  • you run python3 with version >= 3.8. (i just recalled, i have ever upgraded python3 from 3.6.9 to be 3.8, so it makes conflict in cloud-init)

Solution:

  • Downgrade back python3 to 3.6.9 (because cloud-init in ubuntu 18.04 AFAIK, need python3 version 3.6.9 as default)


Downgrade python3 to version 3.6.9 with the following command:

sudo update-alternatives --config python3
Cloud-init EC2 error

Choose number 1 / python3.6, then press Enter.

After that, check python3 version:

python3 --version

you will see python3 version is now 3.6.9

Ok now, let’s install module ‘requests’ again:

sudo apt-get install python3-requests


Then Re-create AMI from the instance, right click the instance > Images and templates > Create image > give a name > click Create Image

Now, you will have new AMI. Launch new instance by clicking Launch Instance from AMI > give a instance name > i choose t2.medium for instance type (smaller than the original instance (t3.medium)) > click Launch Instance

Wait a moment until the instance status: 2/2 passed

Yeah, cloud-init now works perfectly!

Ambar Hasbiyatmoko

Hello, I'm web developer. Passionate about programming, web server, and networking.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload the CAPTCHA.