TIL: Packer stuff

Today I spent a large part of my time fighting with Packer.The task at hand was relative simple. There are two configurations file I needed copied to a specific directory inside the AMI created.

First attempt

Packer has a file provisioner just for that purporse. So, without actually reading all the docs, I just dove in. It looks really straight forward. source, destination. How hard can it be. Its just a copy. Well, I got permission denied. It turns out, unlike, for instance Docker, while the image is building your provisioning are not running as priveleged and you cannot just copy files where you want to.

TIL file provisioners should copy files to tmp directory and then use shell provisioner to copy / move them inside the image

Second attempt

So I copied the files into the /tmp/confg directory then tried to move them to the correct location inside the image using the shell provisioner. I kept getting stats errors. Doing listing the files in /tmp showed config was there. After so more searching I tried to look inside /tmp/config and got a stats error again. Diving deeper into the docs revealed that directories need to exist. Packer will not create them while provisioning.

TIL file provisioner will not raise errors when trying to copy to non existing directories bu will fail silently

Third attempt

Ok. So I just put the two files in /tmp and then mv them from there. Fail again. This time permission denied because the source directory is for a specific user. No problem. I will just chown them. Which didn’t work because permission denied. Searching some more I found that indeed normally this is not allowed. However by changing the way Packer executes the shell provisioning, it is possible to run commands under sudo

TIL Its possible to run the provisioner as sudo by changing the execute_command

And so, after some hours of work and frustations, there is an AMI with the files as needed. this feels harder than it should be.