Packer: `user_data` and `user_data_file` with `amazon-ebs` builder

Created on 8 Dec 2014  路  7Comments  路  Source: hashicorp/packer

I have set these in my template which runs just fine to build an EBS backed AMI. However, I do not see the user data set when i try to launch the AMI.

waiting-reply

Most helpful comment

user_data (string) - User data to apply when launching the instance. Note that you need to be careful about escaping characters due to the templates being JSON. It is often more convenient to use user_data_file, instead.

user_data_file (string) - Path to a file that will be used for the user data when launching the instance.

The above is from Packer's documentation; If I read this correctly, a user_data script or file will be applied to the ami and will be used when launching a packer generated ami. If this is the case, there is a problem. Here is my scenario:

  1. I have a base ami-00001 which I use to generate a new ami with packer and I wish to attach a user_data file to it.
  2. The new ami-00002 is generated successfully and placed into my account.
  3. I launch a new instance using the new ami-00002 and creates an instance with id i-00003 but the user_data script never executes.

After looking in /var/log/cloud-init.log I notice that the script cannot be found (return code 126). Here's what I think is happening:
If you look in /var/lib/cloud/instances you will see (in my case) 3 directories named after instance ids.
i-000001
i-000002
i-000003
Also, you will see in /var/lib/cloud a link instance that points to /var/lib/cloud/instances/i-000003

I believe cloud-init looks in /var/lib/cloud/instance (the current instance) looking for scriipts to run. The problem is that the user_data_file from packer is in the i-000002 directory.

Am I missing something here or mis reading the docs? The user_data script will not execute on start up unless it it is in the current instance-id's directory path.

All 7 comments

Hi @lindenle

Are you able to provide any additional debugging information? The contents of the Packer run in debug mode would be very helpful.

Hi @sethvargo, I appear to be facing the same issue. My user_data_file content is also not making it through for a amazon-ebs type build. There is nothing in the debug logs to indicate what could be going wrong. If required I can provide the output in debug mode.

Hi, I had a similar problem, in my case, user_data script can't be run completely because packer's ssh attempts don't wait cloud_init process finished which bootstraps user_data script inside EC2 instance. Packer terminates the EC2 instance while user_data scripts is still running.

I think user_data config won't work correctly, unless fixing packer provisioning process to wait for the cloud_init script to finish.

Packer provisions as soon as SSH is available. If you are waiting for cloud-init to finish, you shiould add this to your own shell scripts and wait in there. I don't want to make Packer that aware of machine internals.

user_data (string) - User data to apply when launching the instance. Note that you need to be careful about escaping characters due to the templates being JSON. It is often more convenient to use user_data_file, instead.

user_data_file (string) - Path to a file that will be used for the user data when launching the instance.

The above is from Packer's documentation; If I read this correctly, a user_data script or file will be applied to the ami and will be used when launching a packer generated ami. If this is the case, there is a problem. Here is my scenario:

  1. I have a base ami-00001 which I use to generate a new ami with packer and I wish to attach a user_data file to it.
  2. The new ami-00002 is generated successfully and placed into my account.
  3. I launch a new instance using the new ami-00002 and creates an instance with id i-00003 but the user_data script never executes.

After looking in /var/log/cloud-init.log I notice that the script cannot be found (return code 126). Here's what I think is happening:
If you look in /var/lib/cloud/instances you will see (in my case) 3 directories named after instance ids.
i-000001
i-000002
i-000003
Also, you will see in /var/lib/cloud a link instance that points to /var/lib/cloud/instances/i-000003

I believe cloud-init looks in /var/lib/cloud/instance (the current instance) looking for scriipts to run. The problem is that the user_data_file from packer is in the i-000002 directory.

Am I missing something here or mis reading the docs? The user_data script will not execute on start up unless it it is in the current instance-id's directory path.

Hello -

This issue is still happening. Any advise?

Thanks!

@goldworth the original report was a misunderstanding of how AWS works. This for sure works as it should. If you want help as on the mailing list. If you are sure that you are experiencing a bug file a new issue.

Was this page helpful?
0 / 5 - 0 ratings