Problem:
If an Ubuntu image is used as a baseline for a new EC2 instance, and the EC2 build scripts attempt to update the packages during the build process, apt-get will sometimes fail updating, causing the build script to crash and fail.
Symptoms:
During the build process, you may see the following messages:
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
Cause:
There are maintenance scripts within the default Ubuntu Server installation that occasionally update the apt package database on their own, to inform system administrators of recommended and necessary updates. When a new instance is launched, this process sometimes starts before you expect it to, and only one copy of apt can modify the package database at a time. This causes your apt-get update process to exit, since there is no retry option and apt does not know how to handle this error beyond exiting.
Solution:
Use the flock command to wait for the background update job to finish before your own operation is executed.
# flock "$(apt-config shell StateDir Dir::State/d | perl -ne "print /'(.*)\/'/")"/daily_lock apt-get update Hit:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease Hit:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease Hit:3 http://us-east-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease Reading package lists... # flock "$(apt-config shell StateDir Dir::State/d | perl -ne "print /'(.*)\/'/")"/daily_lock apt-get upgrade -y Reading package lists... Building dependency tree... Reading state information... Calculating upgrade... The following packages have been kept back: base-files ubuntu-server The following packages will be upgraded: apport bcache-tools cloud-init cryptsetup cryptsetup-bin grub-common grub-pc grub-pc-bin grub2-common initramfs-tools initramfs-tools-bin ...