Problem


Azure document for "Create an Ubuntu Linux virtual machine using a Bicep file" did not show how to use custom data to run cloud init yaml file when creating VM.

After work 996, I finally figured out the undocumented method to run a cloud init yaml file with Azure Bicep. Let's see how to do it.

Solution


Create a YAML file for cloud init

Create a YAML file in the location where your bicep file can access. For example, I created 251.yml in the same directory with 251.bicep.

Write your cloud init content in the YAML file. For example, install docker

#cloud-config
runcmd:
  - curl -fsSL https://get.docker.com | sh

Load YAML file in Bicep

Define a new variable in your bicep file.

Use loadTextContent() to read YAML file content, and then encode it in base64()

var cloudInit = base64(loadTextContent('251.yml'))

Modify osProfile node

Find osProfile node in your bicep file, add customData into it.

osProfile: {
  computerName: vmName
  adminUsername: adminUsername
  adminPassword: adminPassword
  linuxConfiguration: null
  customData: cloudInit // <-- add this line
}

Run Bicep file

Run your bicep file to test it out. Now you can see the cloud init file is working, docker is automatically installed.

Reference: