Using Cloud-Init
This guide explains how to use cloud-init to automate the configuration of virtual machines in Thalassa Cloud. Cloud-init allows you to customize VMs at boot time, including user creation, package installation, file creation, and running custom scripts.
Note
Cloud-init executes during the first boot of a VM. If you recreate a VM from an image or snapshot, cloud-init will run again on the first boot of the new instance.
What is Cloud-Init?
Cloud-init is a standard method for customizing cloud instances during their initial boot. It runs in the boot process and can; create users and configure SSH keys, Install packages, create files, and configure system services by running custom scripts.
Overview
When creating a VM in Thalassa Cloud, you can provide cloud-init user data that will be executed on first boot. The user data is injected into the VM during provisioning and processed by cloud-init when the instance boots for the first time.
The user data can be provided as:
- Cloud-config: YAML-formatted configuration (recommended)
- Shell script: Bash script that runs on first boot
- Include file: Reference to external configuration
Cloud-Init Execution Behavior
Cloud-init runs automatically on the first boot after VM creation, If you create a new VM from an image or snapshot, cloud-init will execute again on the first boot of the new instance. It’s therefor recommended to remove the cloud init file after the initial boot. Cloud-init does not run on normal reboots of an existing VM
Step 1: Create a Cloud-Init Configuration
Cloud-init configurations are typically written in YAML format using the #cloud-config header.
Basic Cloud-Config Template
Create a cloud-init configuration file:
#cloud-config
users:
- name: admin
groups: sudo
shell: /bin/bash
sudo: ALL=(ALL) NOPASSWD:ALL
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC... your-key-here
package_update: true
package_upgrade: true
packages:
- curl
- git
- htop
- vim
write_files:
- path: /etc/motd
content: |
Welcome to your Thalassa Cloud VM
This system was configured with cloud-init
owner: root:root
permissions: '0644'
runcmd:
- echo "Cloud-init completed successfully" >> /var/log/cloud-init.logCommon Use Cases
Creating Users and SSH Keys
When you create a new VM, you often need to add users and set up SSH access. Cloud-init can create users automatically during the first boot. You can add multiple users and give them different permissions. Each user can have one or more SSH public keys added to their account. This lets you log in without passwords using SSH key authentication. You can also add users to specific groups like sudo or docker. This is useful for setting up deployment users or application users with the right access levels.
#cloud-config
users:
- name: deploy
groups: sudo, docker
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC... key1
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC... key2
sudo: ALL=(ALL) NOPASSWD:ALL
- name: appuser
groups: www-data
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC... key3Installing Packages
You can tell cloud-init to install software packages when the VM first boots. This saves you from manually installing packages after the VM starts. Cloud-init will update the package list and install all the packages you specify. You can also tell it to upgrade existing packages to their latest versions. This is useful for installing web servers, databases, development tools, or any other software your application needs. The packages are installed automatically before your applications start running.
#cloud-config
package_update: true
package_upgrade: true
packages:
- nginx
- postgresql-client
- python3-pip
- docker.io
- docker-composeCreating Files
Cloud-init can create files on your VM during the first boot. You can create configuration files, scripts, or any other files you need. For each file, you specify the full path where it should be created, the content of the file, who owns it, and what permissions it should have. This is useful for creating application configuration files, deployment scripts, or customizing system settings. The files are created before your applications start, so they are ready when you need them. You can create multiple files in a single cloud-init configuration.
#cloud-config
write_files:
- path: /etc/nginx/sites-available/default
content: |
server {
listen 80;
server_name _;
root /var/www/html;
index index.html;
}
owner: root:root
permissions: '0644'
- path: /opt/deploy.sh
content: |
#!/bin/bash
echo "Deployment script"
# Your deployment logic here
owner: root:root
permissions: '0755'
- path: /home/admin/.bashrc
content: |
export EDITOR=vim
alias ll='ls -lah'
owner: admin:admin
permissions: '0644'Running Commands
You can tell cloud-init to run commands or scripts during the first boot. These commands run after packages are installed and files are created. This is useful for starting services, pulling Docker images, setting up systemd services, or running any setup tasks your application needs. Commands run in order, one after another. If one command fails, the remaining commands will still run. You can use this to enable and start services, configure system settings, or run custom setup scripts. All commands run as root, so you have full system access.
#cloud-config
runcmd:
- systemctl enable nginx
- systemctl start nginx
- docker pull nginx:latest
- echo "export APP_ENV=production" >> /etc/environment
- |
cat > /etc/systemd/system/myapp.service <<EOF
[Unit]
Description=My Application
After=network.target
[Service]
ExecStart=/opt/myapp/start.sh
Restart=always
[Install]
WantedBy=multi-user.target
EOF
- systemctl daemon-reload
- systemctl enable myappSetting Environment Variables
You can set environment variables that your applications can use. Environment variables are useful for storing configuration values like database URLs, API keys, or application settings. Cloud-init can create files like /etc/environment that set system-wide environment variables. These variables are available to all users and applications on the system. You can also set environment variables in user-specific files like .bashrc for individual users. This is a common way to configure applications without hardcoding values in your code.
#cloud-config
write_files:
- path: /etc/environment
content: |
APP_ENV=production
DATABASE_URL=postgresql://user:pass@localhost/db
REDIS_URL=redis://localhost:6379
owner: root:root
permissions: '0644'Step 2: Create a VM with Cloud-Init
Option A: Using Thalassa Cloud CLI
Create a VM with cloud-init user data from a file:
tcloud compute machines create \
--name my-vm \
--image ubuntu-24.04 \
--machine-type dgp-large \
--user-data-file cloud-init.yaml \
--subnet subnet-12345Or provide the user data directly:
tcloud compute machines create \
--name my-vm \
--image ubuntu-24.04 \
--machine-type dgp-large \
--user-data "$(cat cloud-init.yaml)" \
--subnet subnet-12345Step 1: Prepare Your Cloud-Init Configuration
Save your cloud-init configuration to a file (e.g., cloud-init.yaml):
#cloud-config
users:
- name: admin
groups: sudo
shell: /bin/bash
ssh_authorized_keys:
- ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC... your-key
sudo: ALL=(ALL) NOPASSWD:ALL
package_update: true
packages:
- curl
- gitStep 2: Create the VM
Use the CLI to create the VM with your cloud-init configuration:
tcloud compute machines create \
--name web-server \
--image ubuntu-22.04 \
--machine-type dgp-large \
--user-data-file cloud-init.yaml \
--subnet subnet-12345Verifying Cloud-Init Execution
After the VM boots, verify that cloud-init ran successfully:
Check Cloud-Init Status
# Check cloud-init status
cloud-init status
# View cloud-init logs
sudo cat /var/log/cloud-init.log
# View all cloud-init logs
sudo journalctl -u cloud-initSyntax Errors
Cloud-config YAML must be valid. Common issues:
- Missing
#cloud-configheader - Incorrect indentation (use spaces, not tabs)
- Invalid YAML syntax
References
- Virtual Machines Documentation — Overview of VM capabilities
- Machine Images Documentation — Understanding machine images
- tcloud CLI Reference — CLI commands for VM management
- Thalassa Cloud API Reference — API documentation for compute resources
- Cloud-Init Documentation — Official cloud-init documentation
- Cloud-Config Examples — Cloud-config examples and reference