Azure Series Part 5: Using bootstrap script to install packages while provisioning the Linux Virtual Machines

Microsoft Azure provides a Custom Script Extension which is useful for post-deployment configuration, software installation, or any other configuration/management task. 

Before we get started I shared in this below post how we can provision the Linux Virtual Machines in Azure.

Azure Series Part 4: Provisioning and accessing the Linux Virtual Machines

We will use the same Azure ARM template we used in the last post. We will modify code by adding the Custom Script Extension.

Let us get Started!

Fist of all we will following below steps:

  1. Create bash bootstrap script
  2. Upload the script to a location like Azure Blob storage or github or anywhere which VM can access
  3. Add extension code into VMs ARM template

Bootstrap Script

Below is the bootstrap script code we are going to use to run in the VM during the provisioning of the VM

#!/bin/bash
yum update -y
yum install httpd24 -y
service httpd start
chkconfig httpd on
echo "<html><body><h1>Hello The Web Spark, This is via Automated Custom Script</h1></body></html>" > /var/www/html/index.html

Script Location and Script Upload

Let us upload the script into Azure Blob inside the Azure Storage Container.

Step 1: Login to azure portal and create a storage if you have not yet created

Step 2: Click on Containers and create a new container inside it

Step 3: Upload the script inside the container and give access permissions

For automation purposes, you can also use the below azure cli commands to create a storage container and upload a script.

az storage container create -n mystoragecontainer --public-access blob
az storage blob upload -f /path/to/file -c mycontainer -n MyBlob

Adding Custom Script Extension in ARM template

Add the below code in the current ARM template to enable the custom script extension.

{
  "name": "config-app",
  "type": "Extensions",
  "location": "[resourceGroup().location]",
  "apiVersion": "2019-03-01",
  "dependsOn": [
    "[concat('Microsoft.Compute/virtualMachines/', parameters('virtualMachineName'))]"
  ],
  "tags": {
    "displayName": "config-app"
  },
  "properties": {
    "publisher": "Microsoft.Azure.Extensions",
    "type": "CustomScript",
    "typeHandlerVersion": "2.1",
    "autoUpgradeMinorVersion": true,
    "settings": {
      "skipDos2Unix":false,
      "timestamp":123456789          
    },
    "protectedSettings": {
       "storageAccountName": "thewebsparkacc",
       "storageAccountKey": "<storage-account-key>",
	   "fileUris": ["https://thewebsparksacc.blob.core.windows.net/thewebspark/script.sh"],
	   "commandToExecute": "sh script.sh",
       "managedIdentity" : {}
    }
  }
}

The important part of the above code is the protected settings, it include script file URL, command to execute the bash script, name of the storage account where script is stored and storage account key.

The storage account key can be found under Settings > Access Keys in Storage Account Service in Azure portal.

Provision the VM again using ARM template as shown in last post – Azure Series Part 4: Provisioning and accessing the Linux Virtual Machines and you will see that the script is installed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s