Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS user_data with Packer

So I'm trying to use Packer to create an AWS image and specify some user data via user_data_file. The contents of this file needs to be run when the instance boots as it will be unique each time. I can't bake this into the AMI.

Using packer I have the following:

{
  "variables": {
  "ami_name": ""
  },
  "builders": [
  {
    "type": "amazon-ebs",
    "region": "us-east-1",
    "source_ami": "ami-c8580bdf",
    "instance_type": "t2.micro",
    "ssh_username": "ubuntu",
    "ami_name": "{{ user `ami_name` }}-{{ isotime | clean_ami_name }}",
    "user_data_file": "user_data.sh",
    "tags": {
      "os_version": "ubuntu",
      "built_by": "packer",
      "build_on": "{{ isotime | clean_ami_name }}",
      "Name": "{{ user `ami_name` }}"
    }
  }],
  "provisioners": [
  {
    "type": "ansible",
    "playbook_file": "playbook.yml",
    "user": "ubuntu"
  }]
}

The contents of my user_data shell script are just a few basic config lines for a package that was installed via the ansible scripts that were run in the provisioners step. Watching the output of Packer I can confirm that the ansible scripts all run.

Packer completes and creates the AMI, but the user data piece is never executed. No record of it exists in resulting image. There is no /userdata.log file and /var/lib/cloud/instance/user-data.txt is empty I feel like I missing something basic as this should be a very simple thing to do with Packer.

like image 906
David Ficociello Avatar asked Jul 14 '17 20:07

David Ficociello


People also ask

How do I make Windows AMI with Packer?

In brief, Packer will spin up the source AMI, connect to it and then run whatever commands or scripts we've configured in our build template to customize the image. Finally, when all is done, Packer will wrap the whole customized package up into a brand new AMI that will be available from the AWS AMI management page.

What is AWS Packer?

Packer is an open source tool for creating identical machine images for multiple platforms from a single source configuration. Packer is lightweight and many customers run it as part of a CI/CD pipeline. It is very easy to set up its execution with AWS CodeBuild, and make it part of your deployment process.

How can I utilize user data to automatically run a script with every restart of my Amazon EC2 Windows instance?

By default, the user data scripts are run one time when you launch the instance. To run the user data scripts every time you reboot or start the instance, add <persist>true</persist> to the user data.


2 Answers

Rereading this I think maybe you misunderstood how user-data scripts work with Packer.

user_data is provided when the EC2 instance is launched by Packer. This instance is in the end, after provisioning snapshoted and saved as an AMI.

When you launch new instances from the created AMI it doesn't have the same user-data, it gets the user-data you specify when launching this new instance.

The effect of the initial (defined in your template) user-data might or might not be present in the new instance depending if the change was persisted in the AMI.

like image 190
Rickard von Essen Avatar answered Sep 17 '22 18:09

Rickard von Essen


As pointed out by Rickard von Essen the answer was to copy my script to /var/lib/cloud/scripts/per-instance which would execute my script on every instance launched from this AMI.

Alternately you can put your script in /var/lib/cloud/scripts/per-boot if you needed this to happen each time the instance boots.

In my case since I wanted to register the instance with a 3rd party service I only had it execute once per instance creation.

like image 26
David Ficociello Avatar answered Sep 20 '22 18:09

David Ficociello