The following packages were automatically installed and are no longer required:

I ssh into aws ec2. After that I root:

sudo -i

Okay so I want to update ubuntu vm:

apt update
apt upgrade
Calculating upgrade... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.15.0-45 linux-headers-4.15.0-45-generic linux-image-4.15.0-45-generic linux-modules-4.15.0-45-generic linux-modules-extra-4.15.0-45-generic
Use 'apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

So what do you mean “automatically installed and are no longer required:”? Is it safe to remove those kernel and if so how do we do it on Ubuntu Linux?

Yes, you can delete those Ubuntu Linux kernel stately. If I were you I would run:

sudo apt --purge autoremove 


See apt command info: