Azure, DevOps

Migrating your applications to Azure using Virtual Machine Scale Sets, Packer and Virtual Machine extensions – Part 3

4 min read

This is a continuation of the previous post about migrating your not ready cloud application to the Azure cloud. The last post discussed about creating a managed image to be able to be used by a virtual machine scale set for provisioning.

What will we do in this series

I decided to do a series of posts about this topic as it touches a variety of aspects. I will use a concrete example that may or may not have happened to you and I plan to cover

Assigning the RBAC for the VMSS

Now that our image is built and the VMSS template is ready, we are ready to add the extension.

The tricky part here is that we want to use the managed identity of the VMSS to go and read data in a blob storage we have somewhere. To do that, I will modify the ARM template I used in part 2.

The first thing I need to do is grant the Storage Blob Data Reader role from the VMSS managed identity to the storage account blob container. The following ARM snippet allows me to do so. Add it to the existing ARM template.

The vmssStorageRoleAssignmentName needs to be a unique guid. We will handle that in our variables declaration.

Variables and parameters

You will need to add the following variables in your main ARM template:

Adding the extension

If you add the extension directly into the ARM template, with dependencies (dependsOn), you may get a 403 from the extension when it’s trying to go and fetch the provisioning script. RBAC role assignments may take up to five minutes to propagate1. The role assignment has to be assigned and propagated before anything can happen. This is because the extension will use the managed identity to go and fetch the files listed in the fileUris array.

As such, I will move the extension to it’s own template. You need to wait about 5 minutes before provisioning it so that the RBAC propagation has time to occur.

If you are writing a PowerShell script to automate all of that, use the PowerShell command Start-Sleep -Seconds 300.

Create a new ARM template file. Add the following into the resources array:

Once the extension has successfully been added to the VMSS, it will run the command you specified in the commandToExecute under the protectedSettings property.

Tip: If you want to debug/troubleshoot, you can always go in the waagent folder where the file was downloaded ( /var/lib/waagent/custom-script/download/*) to see what is going on. The folder is located at. See the troubleshooting procedure for more information.

You will also need to supplement your template with the following parameters and variables:

Necessity in your script

In order to download the file from the blob storage, we need to generate an access token and then query the storage account provider to go and grab the file. The following is an example of how to do this

Since we are passing our file to our script as a parameter, we can get it by using the $1 variable. To grab the access token, we query the identity metadata endpoint and ask it to generate a token for the storage provider. The response from the endpoint is a JSON object containing the JWT information. As we only need the access token, we use the jq utility to extract it. Once that is done, we use that access token to download the file and the save it to the appropriate location, in my case /var/www/html .

Tip: Install jq in the image you build with Packer so that it is available right way in your post provisioning script.

Verification

We can now see that our extension ran properly if we navigate to our web application. In my example, I downloaded a text file from the blob storage and added it to the serving directory.

To remember

If you delete everything related to the VMSS, your role assignment will not be deleted along with all the resources. For that you will need to delete it manually:

Updating your VMSS image and extension

If you want to update your VMSS image, as mentioned in the documentation, you can just run the following PowerShell cmdlet:

If you want to update your extension for it to go and fetch a new file, you can just run the provisioning of the extension again and it will update its under template properties and settings. Just remember to change your artifactsLocation parameter.

Food for thought

If you don’t want to always update your extension to grab new files, you could use an Azure Function which would act to give you the latest URI to your file. In your extension, you would only pass in the Azure Function URI, which wouldn’t change, and the rest could be taken care of in your post provisioning script.

Conclusion

You can do the same with windows images and use DSC extensions. You will have to create and upload your DSC extension to a blob storage and the rest will work as explained in this series. Feel free to let me know your tips and tricks and questions. I’m always looking to improve.