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.
-
Have your
cloud-config
YAML file ready.my-cloud-config.yaml
- Make sure to include the
#cloud-config
annotation on the first line of the file! - See more examples at Cloud config examples library.
- Make sure to include the
-
Pass the
cloud-config
to the new instance.- In Compute > Instances, click the Launch Instance button.
Example
- Follow the launch steps as described e.g. in the Create First Instance guide.
- 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
- Complete the steps and launch the instance.
- In Compute > Instances, click the Launch Instance button.
Existing instance
- Login to the instance (via SSH, web console, …).
- Become
root
and add the configuration to the/etc/cloud/
directory. - (Optional) If you want to apply your changes immediately, force
cloud-init
to reload the configuration and re-initiate.- 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
).If you are sure about the server security, you may want to remove the old key from
~/.ssh/known_hosts
:
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
:
See https://cloudinit.readthedocs.io/en/latest/reference/modules.html#install-hotplug.
Last updated on