|
| 1 | +--- |
| 2 | + title: Custom data and Azure Virtual Machines |
| 3 | + description: Details on using Custom data and Cloud-Init on Azure Virtual Machines |
| 4 | + services: virtual-machines |
| 5 | + author: mimckitt |
| 6 | + ms.service: virtual-machines |
| 7 | + ms.topic: article |
| 8 | + ms.date: 03/06/2020 |
| 9 | + ms.author: mimckitt |
| 10 | +--- |
| 11 | + |
| 12 | +# Custom data and Cloud-Init on Azure Virtual Machines |
| 13 | + |
| 14 | +## What is Custom data? |
| 15 | + |
| 16 | +Customers often ask how they can inject a script or other metadata into a Microsoft Azure virtual machine at provision time. In other clouds, this concept is often referred to as user data. In Microsoft Azure, we have a similar feature called custom data. |
| 17 | + |
| 18 | +Custom data is only made available to the VM during first boot/initial setup, we call this 'provisioning'. Provisioning is the process where VM Create parameters (for example, hostname, username, password, certificates, custom data, keys etc.) are made available to the VM and a provisioning agent processes them, such as the [Linux Agent](https://docs.microsoft.com/azure/virtual-machines/extensions/agent-linux) and [cloud-init](https://docs.microsoft.com/azure/virtual-machines/linux/using-cloud-init#troubleshooting-cloud-init). |
| 19 | + |
| 20 | + |
| 21 | +## Passing custom data to the VM |
| 22 | +To use custom data, you must base64 encode the contents first before passing it to the API, unless you are using a CLI tool that does the conversion for you, such as AZ CLI. The size cannot exceed 64 KB. |
| 23 | + |
| 24 | +In CLI, you can pass your custom data as a file, and it will be converted to base64. |
| 25 | +```bash |
| 26 | +az vm create \ |
| 27 | + --resource-group myResourceGroup \ |
| 28 | + --name centos74 \ |
| 29 | + --image OpenLogic:CentOS-CI:7-CI:latest \ |
| 30 | + --custom-data cloud-init.txt \ |
| 31 | + --generate-ssh-keys |
| 32 | +``` |
| 33 | + |
| 34 | +In Azure Resource Manager (ARM), there is a [base64 function](https://docs.microsoft.com/azure/azure-resource-manager/templates/template-functions-string#base64). |
| 35 | + |
| 36 | +```json |
| 37 | +"name": "[parameters('virtualMachineName')]", |
| 38 | +"type": "Microsoft.Compute/virtualMachines", |
| 39 | +"apiVersion": "2019-07-01", |
| 40 | +"location": "[parameters('location')]", |
| 41 | +"dependsOn": [ |
| 42 | +..], |
| 43 | +"variables": { |
| 44 | + "customDataBase64": "[base64(parameters('stringData'))]" |
| 45 | + }, |
| 46 | +"properties": { |
| 47 | +.. |
| 48 | + "osProfile": { |
| 49 | + "computerName": "[parameters('virtualMachineName')]", |
| 50 | + "adminUsername": "[parameters('adminUsername')]", |
| 51 | + "adminPassword": "[parameters('adminPassword')]", |
| 52 | + "customDataBase64": "[variables('customData')]" |
| 53 | + }, |
| 54 | +``` |
| 55 | + |
| 56 | +## Processing custom data |
| 57 | +The provisioning agents installed on the VMs handle interfacing with the platform and placing it on the file system. |
| 58 | + |
| 59 | +### Windows |
| 60 | +Custom data is placed in *%SYSTEMDRIVE%\AzureData\CustomData.bin* as a binary file, but it is not processed. If you wish to process this file, you will need to build a custom image, and write code to process the CustomData.bin. |
| 61 | + |
| 62 | +### Linux |
| 63 | +On Linux OS's, custom data is passed to the VM via the ovf-env.xml file, which is copied to the */var/lib/waagent* directory during provisioning. Newer versions of the Microsoft Azure Linux Agent will also copy the base64-encoded data to */var/lib/waagent/CustomData* as well for convenience. |
| 64 | + |
| 65 | +Azure currently supports two provisioning agents: |
| 66 | +* Linux Agent - By default the agent will not process custom data, you will need to build a custom image with it enabled. The relevant settings, as per the [documentation](https://github.com/Azure/WALinuxAgent#configuration) are: |
| 67 | + * Provisioning.DecodeCustomData |
| 68 | + * Provisioning.ExecuteCustomData |
| 69 | + |
| 70 | +When you enable custom data, and execute a script, it will delay the VM reporting that is it ready or that provisioning has succeeded until the script has completed. If the script exceeds the total VM provisioning time allowance of 40 mins, the VM Create will fail. Note, if the script fails to execute, or errors during executing, it is not deemed a fatal provisioning failure, you will need to create a notification path to alert you for the completion state of the script. |
| 71 | + |
| 72 | +To troubleshoot custom data execution, review */var/log/waagent.log* |
| 73 | + |
| 74 | +* cloud-init - By default will process custom data by default, cloud-init accepts [multiple formats](https://cloudinit.readthedocs.io/en/latest/topics/format.html) of custom data, such as cloud-init configuration, scripts etc. Similar to the Linux Agent, when cloud-init processes the custom data. If there are errors during execution of the configuration processing or scripts, it is not deemed a fatal provisioning failure, and you will need to create a notification path to alert you for the completion state of the script. However, different to the Linux Agent, cloud-init does not wait on user custom data configurations to complete before reporting to the platform that the VM is ready. For more information on cloud-init on azure, review the [documentation](https://docs.microsoft.com/azure/virtual-machines/linux/using-cloud-init). |
| 75 | + |
| 76 | + |
| 77 | +To troubleshoot custom data execution, review the troubleshooting [documentation](https://docs.microsoft.com/azure/virtual-machines/linux/using-cloud-init#troubleshooting-cloud-init). |
| 78 | + |
| 79 | + |
| 80 | +## FAQ |
| 81 | +### Can I update custom data after the VM has been created? |
| 82 | +For single VMs, custom data in the VM model cannot be updated, but for VMSS, you can update VMSS custom data via REST API (not applicable for PS or AZ CLI clients). When you update custom data in the VMSS model: |
| 83 | +* Existing instances in the VMSS will not get the updated custom data, only until they are reimaged. |
| 84 | +* Existing instances in the VMSS that are upgraded will not get the updated custom data. |
| 85 | +* New instances will receive the new custom data. |
| 86 | + |
| 87 | +### Can I place sensitive values in custom data? |
| 88 | +We advise **not** to store sensitive data in custom data. For more information, see [Azure Security and encryption best practices](https://docs.microsoft.com/azure/security/fundamentals/data-encryption-best-practices). |
| 89 | + |
| 90 | + |
| 91 | +### Is custom data made available in IMDS? |
| 92 | +No, this feature is not currently available. |
0 commit comments