How to get started with cloud-init

Last updated on February 25, 2023 by Dan Nanni

Cloud-init is an open-source tool for configuring and customizing cloud instances when they first start up. It is used by many cloud providers, including Amazon Web Services (AWS), Google Cloud Platform (GCP), Microsoft Azure, and DigitalOcean. Cloud-init is designed to simplify the process of configuring and customizing cloud instances. It allows you to specify a wide range of configuration options via a cloud-init configuration file. This includes creating a user account, installing software packages and a specific kernel version, configuring networking, setting up SSH keys, etc.

When a cloud instance is started with cloud-init, the configuration specified in a cloud-init configuration file is applied automatically. This can help streamline the process of setting up new cloud instances when there are many, and make it easier to ensure that all instances are configured correctly and securely.

In this tutorial, I will show you how to use cloud-init to set up basic configuration of a Linux-based cloud instance.

How to Create a Cloud-Init File

Cloud-init uses a YAML-formatted configuration file to configure a cloud instance upon its boot. To create a cloud-init file, you can use any text editor on your local computer. Once a cloud-init file is created, the location where you should upload it will vary depending on which cloud provider you are using, and how you are launching an instance. For example:

In the rest of the tutorial, let's find out how to use cloud-init to configure different settings of your cloud instance. I will use Ubuntu-based cloud instance as an example.

Create a User Account with Cloud-init

To use cloud-init to set up a user account on a cloud instance, you can include the following in your cloud-init configuration file:

# cloud-config
users:
  - name: yourusername
    ssh-authorized-keys:
      - ssh-rsa YOUR_PUBLIC_KEY_HERE
    sudo: ALL=(ALL) NOPASSWD:ALL

In this example, I am going to use SSH key authentication, and hence specify the SSH public key. Just copy and paste your public key. Also, I enable the sudo privilege for the user, and allow the user to run any command as sudo without being prompted for a password.

Set the Default Timezone with Cloud-init

You want to change the default timezone? No worries. It is as easy as:

# cloud-config
timezone: America/New_York

You can find your timezone string with the tzselect command.

Configure Networking with Cloud-init

Cloud-init supports a wide range of networking including DHCP-based and static networking. I assume that eth0 is the name of the primary network interface of the instance.

To set up DHCP-based networking:

# cloud-config
network:
  version: 2
  ethernets:
    eth0:
      dhcp4: true

To manually set up a static IP address and DNS servers:

# cloud-config
network:
  version: 2
  ethernets:
    eth0:
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses:
          - 8.8.8.8
          - 8.8.4.4

Install Packages with Cloud-init

Often times you want to install some packages on your VPS. In this case, you can use runcmd

# cloud-config
runcmd:
  - apt update
  - apt install -y nginx
  - echo "Hello, world!" > /var/www/html/index.html

Under runcmd, you are supposed to include a list of commands that are executed when the instance starts up. The commands listed under runcmd are only executed the first time ever you start an instance.

Install and Boot with a Specific Kernel Version with Cloud-init

If you require a specific kernel version for your cloud instance, cloud-init can get the job done easily. In this example, I choose 5.4.0-87-generic as a preferred kernel version.

# cloud-config
packages:
  - linux-image-5.4.0-87-generic
bootcmd:
  - sed -i 's/^GRUB_DEFAULT=.*/GRUB_DEFAULT="Advanced options for Ubuntu>Ubuntu, with Linux 5.4.0-87-generic"/g' /etc/default/grub
  - update-grub

Under packages, you include a list of packages to install on the instance. In this case, the package we need is linux-image-5.4.0-97-generic. Under bootcmd, you specify a list of commands that are executed at boot time. In this case, I use the sed command to modify the GRUB_DEFAULT setting in the /etc/default/grub file, such that it will boot the specific kernel version that we specify. We also need to run update-grub to modify the GRUB configuration file reflect the new default kernel.

The difference between bootcmd and runcmd is that the the bootcmd section is invoked early during initialization, whereas those commands in the runcmd section are invoked near the end of the init process.

Enable Time Synchronization with Cloud-init

If your instance require accurate clock all the time, you want to enable time synchronization via NTP. With systemd, this can be done easily with timedatectl command.

# cloud-config
runcmd:
  - timedatectl set-ntp true

Create a File with Cloud-init

If you want to create a file (e.g., configuration file) with predefined content, you can also do it with cloud-init. For example, you can create a custom sshd_config and place it /etc/ssh by using the following:

# cloud-config
write_files:
- content: |
       # content of sshd_config
       Port 17600

       HostKey / etc / ssh / ssh_host_rsa_key
       HostKey / etc / ssh / ssh_host_ecdsa_key

       SyslogFacility AUTHPRIV

       PasswordAuthentication no

       PermitRootLogin No

       AuthorizedKeysFile .ssh / authorized_keys

       ChallengeResponseAuthentication no

       GSSAPIAuthentication yes
       GSSAPICleanupCredentials no

       UsePAM yes

       X11 Forwarding no
  path: /etc/ssh/sshd_config
  permissions: 0600
  owner: root: root

As you can see, cloud-init is quite flexible and extensible, allowing you to customize the configuration of your instances to meet your specific requirements. Cloud-init includes a wide range of built-in directives for configuring user accounts, network settings, package installation, file management, system configuration, and more!

Got any specific question with cloud-init? Post your question via comment!

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2023 ‒ AboutWrite for Us ‒ Feed ‒ Powered by DigitalOcean