Happy Employees == Happy ClientsCAREERS AT DEPT®
DEPT® Engineering BlogDevOps

DevOps Quick Fix: intermittent yum and apt errors on new EC2

DevOps Quick Fix are solutions to common DevOps problems, usually encountered during client engagements

Problem

When creating a new AWS EC2 (either via Packer for AMI or via Terraform for EC2), the usual next immediate step is to install Unix packages via yum or apt. You may get intermittent errors like:

"Package has no installation candidate"
"Unable to locate package"
"Error: Unable to find a match"
"No package available"

Solution

Running yum or apt before EC2 finishes initialization is the cause of the problem.

You can check when cloud-init finishes EC2 initialization by looking for the file /var/lib/cloud/instance/boot-finished .

The Bash solution is:

while [ ! -f  /var/lib/cloud/instance/boot-finished ];
do
    echo "Waiting for cloud-init to finish ..."
    sleep 2
done

apt-get update -y
apt-get install -y package

The Ansible solution is:

- name: Wait until cloud-init finishes
  wait_for:
    path: /var/lib/cloud/instance/boot-finished
    sleep: 2
    timeout: 300
    
    
- name: Update all existing yum packages
  yum:
    name: '*'
    state: latest