Cloud init examples

Cloud-init is the industry-standard tool for automating the initial configuration of cloud instances across AWS, Azure, GCP, and other platforms. It enables declarative provisioning of users, groups, packages, files, disks, and more, using simple YAML syntax. This page provides real-world, production-ready cloud-init examples for DevOps engineers.


What is Cloud-init?

Cloud-init is an open-source tool that automates the early initialization of cloud servers. It reads user data (YAML or shell scripts) provided at instance launch and applies configuration such as:

  • Creating users and groups

  • Installing packages

  • Writing files and templates

  • Configuring SSH keys and access

  • Setting up disks, filesystems, and mounts

  • Running custom commands or scripts

Cloud-init is supported by all major cloud providers (AWS EC2, Azure VMs, GCP Compute Engine, OpenStack, etc.) and is essential for repeatable, automated infrastructure provisioning.


Best Practices

  • Always validate your YAML with a linter before deploying.

  • Use version control (Git) for your cloud-init templates.

  • Avoid hardcoding secrets; use cloud provider secrets managers or encrypted values.

  • Test cloud-init scripts in a staging environment before production.

  • Use modules (e.g., users, write_files, runcmd) for clarity and maintainability.


Example: Including Users and Groups


Writing out arbitrary files

Last updated