Install Custom Script Extension in Azure Linux VM using the REST API

Figuring out how to install Microsoft Azure VM extensions using the REST API can be a bit of a pain — especially when you don’t know the Publisher name, version, or specific name of the extension. While these values are available for a few of the Windows extensions, the Linux ones are harder to find. Recently I had to figure out the settings for the Linux version of the Custom Script Extension, as well as how to specify the command to be run. After a bit of searching I found this and decided to share as it was certainly not as easy to find as would have been nice.

Note that the bits that you want are in the <ResourceExtensionReference> tag. Also note that the <Value>‘s for both of the <ResourceExtensionParameterValue> tags are base64-encoded strings built from JSON objects of the following form:

{
    "fileUris": ["http://MyAccount.blob.core.windows.net/vhds/MyShellScript.sh"],
    "commandToExecute": "sh MyShellScript.sh"
}

In the public config, fileUris can be a list of one or more blobs to be downloaded from the Azure Storage account whose information is contained in the private config:

{
    "storageAccountName": "MyAccount",
    "storageAccountKey":"Mykey"
}

In C# you can base64 encode these values as such:

// Where "paramString" is the name of the string you want to base64 encode
String encodedValues = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(paramString));

Finally, a full example of the XML and parameters. Note that you’ll replace the <!–Public Config–> and <!–Private Config–> with the base64 encoded versions of your public and private config parameters.

<Deployment xmlns="http://schemas.microsoft.com/windowsazure">
  <Name></Name>
  <DeploymentSlot></DeploymentSlot>
  <Label></Label>
  <Location></Location>
  <RoleList>
    <Role>
      <RoleName></RoleName>
      <RoleType></RoleType>
      <ConfigurationSets>
        ...
      </ConfigurationSets>
      <ResourceExtensionReferences>
        <ResourceExtensionReference>
          <ReferenceName>MyCustomScriptExtension</ReferenceName>
          <Publisher>Microsoft.OSTCExtensions</Publisher>
          <Name>CustomScriptForLinux</Name>
          <Version>1.1</Version>
          <ResourceExtensionParameterValues>
            <ResourceExtensionParameterValue>
              <Key>CustomScriptExtensionPublicConfigParameter</Key>
              <Value><!--Public Config--></Value>
              <Type>Public</Type>
            </ResourceExtensionParameterValue>
            <ResourceExtensionParameterValue>
              <Key>CustomScriptExtensionPrivateConfigParameter</Key>
              <Value><!--Private Config--></Value>
              <Type>Private</Type>
            </ResourceExtensionParameterValue>
          </ResourceExtensionParameterValues>
        </ResourceExtensionReference>
      </ResourceExtensionReferences>
      <OSVirtualHardDisk>
        ...
      </OSVirtualHardDisk>
    </Role>
  </RoleList>
</Deployment>

Demo JSON objects are from this Azure blog post.

No comments yet.

Leave a Reply

Proudly made in Canada