45

I'm writing a CloudFormation template and I'm trying to debug the user-data script I provide in the template. How can I run the cloud-init manually and make it perform the same actions it does when starting a new instance?

Fluffy
  • 27,504
  • 41
  • 151
  • 234

6 Answers6

56

You can just run it like this:

/usr/bin/cloud-init -d init

This runs the cloud init setup with the initial modules. (The -d option is for debug) If want to run all the modules you have to run:

/usr/bin/cloud-init -d modules

Keep in mind that the second time you run these it doesn't do much since it has already run at boot time. To force to run after boot time you can run from the command line:

( cd /var/lib/cloud/ && sudo rm -rf * )

In older versions the equivalent of cloud-init init is:

/usr/bin/cloud-init start

You may also find this question useful although it applies to the older versions of cloud-init: How do I make cloud-init startup scripts run every time my EC2 instance boots?

The documentation for cloud init here just gives you examples. But it doesn't explain the command line options or each one of the modules, so you have to play around with different values in the config to get your desired results. Of course you can also look at the code.

Community
  • 1
  • 1
Rico
  • 58,485
  • 12
  • 111
  • 141
  • @Fluffy. Yeah I did, why ? – Rico Apr 18 '14 at 12:22
  • Well I'm getting `cloud-init: error: invalid choice: 'start' (choose from 'query', 'init', 'modules', 'single')` – Fluffy Apr 18 '14 at 12:59
  • Looks like you have a different version. Possibly the latest Ubuntu. Run: `cloud-init init` – Rico Apr 18 '14 at 13:22
  • Wouldn't `cloud-init init` just run just the modules from `cloud_init_modules`, skipping the rest of the run? – Fluffy Apr 18 '14 at 13:31
  • Well it should run and initial setup and everything in cloud_init_modules but what happens is that when you run `cloud-init init` the second time it doesn't run like it first booted the machine (because it has already run) To force it to run everything the workaround is to do something like this: `( cd /var/lib/cloud/ && sudo rm -rf * )` from the command line. If you want to run the other modules in your config you can also run `cloud-init -d modules` – Rico Apr 18 '14 at 14:52
  • @Fluffy added more details to the answer. I hope it helps. – Rico Apr 18 '14 at 15:01
  • You can also remove the semaphores in `/var/lib/cloud/instances/i-xxxxxxx/sem`, then edit `/var/lib/cloud/instances/i-xxxxx/scripts/part-001` to your liking and execute it via `sudo /usr/bin/cloud-init -d single -n cc_scripts_user`. – kadrach Jan 31 '17 at 04:13
  • None of your examples re-ran the /etc/cloud/cloud.cfg.d folder scripts for me. However, clearing out the /var/lib/cloud/ folder, and rebooting the instance, worked! – vcardillo Mar 16 '18 at 01:51
10
rm -f /var/log/cloud-init.log \
&& rm -Rf /var/lib/cloud/* \
&& cloud-init -d init \
&& cloud-init -d modules --mode final
Sergey Safarov
  • 436
  • 4
  • 11
  • This might be "better" that what the OP asked for? That is, cloud-init doesn't do the deletes you have, thus your solution does not "perform the same actions" does it? If you added a few words of commentary then all would be clear! – JoeG Nov 26 '18 at 12:31
  • While the above solutions did not rerun failed package downloads and install of packages (salt-minion) this solution did. – Fabian Feb 11 '20 at 12:36
9

Kudus to @Rico, and also, if you want to run a single module - either for testing or because your distro doesn't enable a module by default (hi Precise!), you can

/usr/bin/cloud-init -d single -n <module-name>

For example when my distro doesn't run write_files by default (like a lot of old distros), I use this at the top of runcmd:

runcmd:
 - /usr/bin/cloud-init -d single -n write-files

[I know its not really an answer to the OP, but when looking to solve my problem this question was one of the top results, so I figure other people might find this useful]

Guss
  • 30,470
  • 17
  • 104
  • 128
8

As documentation, you can simply run

sudo cloud-init clean

and add --logs to clean all log file. It will redo everything when you reboot

Lisbeth
  • 161
  • 1
  • 8
4

On most Linux distros (including CentOS and Ubuntu), you can restart the cloud-init service using systemctl:

systemctl restart cloud-init

And then check the output of the journal to see the results:

journalctl -f -u cloud-init
mikemaccana
  • 110,530
  • 99
  • 389
  • 494
Andrew
  • 580
  • 5
  • 13
0

On Amazon Linux 2, we figured out that cloud-init is run after initial launch and then removed. This caused a problem when we built custom AMIs with Packer and then wanted to launch them with user-data scripts. Here is the Packer shell provisioner (HCL2 format) we use at the end of a build to reset cloud-init:

  provisioner "shell" {
    inline = [
      "echo 'Waiting for cloud-init'; while [ ! -f /var/lib/cloud/instance/boot-finished ]; do sleep 1; done; echo 'Done'",
      "sudo yum install cloud-init -y",
      "sudo cloud-init clean",
    ]
  }

AMIs built with templates that have this will launch with cloud-init support.

aharden
  • 101
  • 2