AWS user_data with wrapper

So I am trying to use Packer to build an AWS image and specify some custom data via user_data_file. The content of this file should run when the instance is loaded as it will be unique every time. I can't bake this in OMI.

Using a 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 content of my shell user_data script is just a few basic configuration lines for the package, which was installed via irreplaceable scripts that were executed during the build phase. By watching the Packer release, I can confirm that all running scripts are being executed.

The packer completes and creates the AMI, but the user data piece is never executed. There is no record in the resulting image. There is no /userdata.log file and is /var/lib/cloud/instance/user-data.txt

empty. I feel like I am missing something basic as this should be a very simple task for Packer.

+7


source to share


3 answers


As Ricard von Essen pointed out, the answer was to copy my script into /var/lib/cloud/scripts/per-instance

, which would execute my script on every instance started from that OIM.

Alternatively, you can put your script in /var/lib/cloud/scripts/per-boot

if you need this to happen every time the instance is loaded.



In my case, since I wanted to register an instance with a third party service, I only executed it once to create the instance.

+3


source


By rereading this, I think you may have misunderstood how user data scripts work with Packer.

user_data

provided when the EC2 instance is started by Packer. This instance is, after all, after taking snapshots 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 that you specify when you start this new instance.

The effect of the original (defined in your template) user data may or may not be present in the new instance, depending on whether the change persists in the AMI.

+8


source


Loading in /var/lib/cloud/scripts/*

will work, but it depends on how you want your images to be built. Need to spin up a copy quickly?

The best solution for us would be packer suppliers . Providers are used to install and configure a post-boot machine image using ansible / salt / puppet / cheff / shell scripts, etc. You can supply your look with whatever you need. This way you will not be tied to the need to prepare deps every time you start the instance, which can cause some problems (think about intermittent network problems / outages that can cause some deps not to be installed)

Suppliers for the packer are third party

0


source







All Articles