5

I get various and inconsistent errors with apt on the official Ubuntu images (ami-83e769fb). I'm using Packer to build my AMI and it fails about 40% of the time. Rerunning the script succeeds.

My script runs:

sudo apt-get clean all
sudo apt-get update

before install any packages.

Some times I get this error:

amazon-ebs: W: GPG error: http://archive.ubuntu.com/ubuntu artful InRelease: Splitting up /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_artful_InRelease into data and signature failed
amazon-ebs: E: The repository 'http://archive.ubuntu.com/ubuntu artful InRelease' is not signed.

Some times apt-get update hits http://us-west-2.ec2.archive.ubuntu.com/ubuntu ... other times it doesn't.

Other times packages are missing (like apache2 or python3).

I don't understand why this behaviour is inconsistent.

How can I get apt-get update on the official Ubuntu AMI to work consistently?

Mystic
  • 151
  • 4

3 Answers3

5

If you are using cloud-init you can wait for it to complete.

while [ ! -f /var/lib/cloud/instance/boot-finished ]; do
   echo 'Waiting for cloud-init...'
   sleep 1
done

e.g. packer json:

{
  "type": "shell",
  "inline": [
    "while [ ! -f /var/lib/cloud/instance/boot-finished ]; do echo 'Waiting for cloud-init...'; sleep 1; done"
  ]
}

Reference:

dnozay
  • 251
  • 3
  • 7
2

packer.json before provision:

"provisioners": [
    {
       "type": "shell",
       "inline": ["/usr/bin/cloud-init status --wait"]
},
1

I just ran into this issue myself and I believe it happens because cloud-init is still in the process of configuring the EC2 instance when apt-get runs. I solved it by inserting a 30 second delay in my script that runs immediately after the instance boots. I think a better way would be to ask cloud-init to run any scripts through User Data or even letting it handle package installation and updates for you [1]. For my use case, where I don't want to acknowledge cloud-init, adding the delay was an acceptable solution.

  1. https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html
ctrlc-root
  • 181
  • 1
  • 8