Skip to main content

Automating VM setup with Cloud-init

Cloud-init is an industry standard that enables you to automate the initialisation of your Linux instances. This means you can use Cloud-init to inject a file when provisioning a ccloud³ VM, which automatically sets up things such as new users, firewall rules, app installations and SSH keys. The User Data feature of centron utilises Cloud-init, allowing you to provision and automatically configure multiple ccloud³ VMs simultaneously. Learning how to use Cloud-init can save you a great deal of set-up time when provisioning new ccloud³ VMs.

In this tutorial, you will learn how to use Cloud-init to provision two ccloud³ VMs that automatically:

  1. Set up a new user on the ccloud³ VM.

  2. Link a public SSH key (from GitHub) to the new user.

  3. Remove root user access.

  4. Configure and start nginx on each ccloud³ VM.

By the end of this tutorial, you will be able to log in to any ccloud³ VM using your new username and SSH key, and view a static website hosted on any ccloud³ VM.

This example application makes it possible to set up user accounts on ccloud³ VMs for the entire Teams system without first having to log in to the ccloud³ VM.

Prerequisites

To complete this tutorial, you will need:

  • A GitHub account to store the new user’s SSH key.

  • Installed doctl, the official CLI tool from centron. You use doctl to add an SSH key to your account and to provision the ccloud³ VMs using the cloud-init file.

  • An API token to authenticate your commands with doctl.

Step 1: Create your SSH keys

To complete this tutorial, you will need an SSH key to set up the new user account on the ccloud³ VM. Once you have generated the key, upload the public key to both your centron and GitHub accounts.

  • Command: ssh-keygen

Step 2: Upload public keys

In order to provision the ccloud³ VMs with their automated configuration, the public SSH key must be accessible when the ccloud³ VMs are created.

Instructions for uploading the public key to your centron account:

compute ssh-key import git-user --public-key-file /Users/example-user/.ssh/git-user.pub

Step 3: Configure the cloud-init file

Once you have uploaded the public SSH key to your centron and GitHub accounts, you can start configuring the cloud-init.yaml file, which the ccloud³ VM uses to configure itself.

Example configuration:

#cloud-config
users:
- name: example-user
shell: /bin/bash
sudo: ['ALL=(ALL) NOPASSWD:ALL']
ssh_import_id:
- gh:<your-GitHub-username>
disable_root: true
packages:
- nginx
runcmd:
- 'export PUBLIC_IPV4=$(curl -s http://169.254.169.254/metadata/v1/interfaces/public/0/ipv4/address)'
- 'echo ccloud: $(hostname), IP Address: $PUBLIC_IPV4 > /var/www/html/index.html'

Step 4: Deploying the ccloud³ VMs

Once you have configured the cloud-config.yaml file, you can provision the ccloud³ VMs on your centron account.

Step 5: Check the setup

Once you have successfully deployed the ccloud³ VMs, you can check whether the Cloud-init configuration was successful by logging in to one of the ccloud³ VMs using the username from the Cloud-config.yaml file.

Summary

In this tutorial, you will:

  1. Create an SSH key pair and add the public key to your centron and GitHub accounts

  2. A Cloud-init YAML file has been configured.

  3. Deployed two ccloud³ VMs that use a cloud-init file to automatically set up a new user and an nginx website.

You can now use these ccloud³ VMs as normal or delete them if necessary.