einfra logoDocumentation
Additional information

Custom cloud-init

Cloud-init is a tool that helps to manage cloud instance initialization. Find out more in Cloud-init documentation.

All cloud images have cloud-init pre-installed and set up with default configuration, which works reasonably well for most use-cases. However, you may find yourself in need of some additional setup.

Applying custom cloud-init configuration

New instance

You can provide your instance with custom configuration in the form of ‘cloud-config’ (also called ‘user data’) at the time of its creation.

  1. Have your cloud-config YAML file ready.

    my-cloud-config.yaml

    #cloud-config
    runcmd:
    - [ls, -l, /]
    - [sh, -xc, 'echo $(date) '': hello world!''']
    - [sh, -c, echo "=========hello world'========="]
    - ls -l /root
  2. Pass the cloud-config to the new instance.

    1. In Compute > Instances, click the Launch Instance button.

      Example

    2. Follow the launch steps as described e.g. in the Create First Instance guide.
    3. In the Configuration step, either click Browse and select your cloud-config YAML file, or paste the config directly into the Customization Script textarea.

      Example

    4. Complete the steps and launch the instance.

Existing instance

  1. Login to the instance (via SSH, web console, …).
    ubuntu@my-instance:~$ ssh ubuntu@147.X.X.X
  2. Become root and add the configuration to the /etc/cloud/ directory.
    ubuntu@my-instance:~$ sudo -i
    root@my-instance:~$ cat <<EOF > /etc/cloud/cloud.cfg.d/99_hello-world.cfg
    runcmd:
    - [ls, -l, /]
    - [sh, -xc, 'echo $(date) '': hello world!''']
    - [sh, -c, echo "=========hello world'========="]
    - ls -l /root
    EOF
  3. (Optional) If you want to apply your changes immediately, force cloud-init to reload the configuration and re-initiate.
    root@my-instance:~$ cloud-init clean && cloud-init init && cloud-init modules --mode=config && cloud-init modules --mode=final
    • This might prove useful in case of changes that affect runtime behavior, such as auto-configuration of network devices (see below).

    Warning

    This set of commands makes cloud-init reinitiate all data generated during the first boot of the instance, including server SSH keys. This means that next time you log in using SSH, you will see a security warning that the new key does not match the one stored in your local trusted keys (stored usually in ~/.ssh/known_hosts).

    Warning: Remote Host Identification has Changed

    If you are sure about the server security, you may want to remove the old key from ~/.ssh/known_hosts:

    user@localhost:~$ ssh-keygen -R <hostname or IP>

Custom use-cases

Here, we discuss situations that require additional configuration of cloud-init.

Automatic setup of network interfaces

By default, cloud-init auto-configures NICs on first boot of an instance.

To make it do the auto-configuration on every boot and on the run (hotplug), use this cloud-config:

#cloud-config
 
updates:
  network:
    when: [boot, hotplug]

See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#install-hotplug.

Last updated on

publicity banner

On this page

einfra banner