iac,

Running Scripts from ARM Templates

Sven Malvik Sven Malvik Connect Jul 11, 2020 · 3 mins read

Why would we want t execute code within an ARM template? Sometimes we need some value in an ARM template that we don’t want to copy and paste around, like secrets. Evgeny Borzenin describes in one of his blog posts how to create a password in ARM, and then create a database with this password. This post will show how we can use the deploymentScript in ARM in its purest form.

Let’s list key vaults in a subscription. The most simple way is using the Azure CLI with az keyvault list. I will show how to use the deploymentScript in ARM to achieve the same, but 100 times more complicated. This post shall just shows what’s possible with this new feature. It’s still in preview, so it might disappear later, or come in a different form.

We set the scene

# Set the subscription you will perform on
az account set -s "YOUR_SUBSCRIPTION "

# Create identity to be able to execute code in deployment script
az identity create -g "sma-rg" -n "myUserAssignedIdentity"

# Read the principleId for the next step
principalId=$(az identity show -g sma-rg -n myUserAssignedIdentity --query principalId)

# Assign contributor role on the identity you just created
az role assignment create --assignee-object-id $principalId --role Contributor

# Read the id of your identity. Yu will set this in the ARM template
YOUR_IDENTITY=$(az identity show -g sma-rg -n myUserAssignedIdentity --query id)

The ARM template

In this ARM template we do not provision anything. This ARM template shows the deploymentScripts in its pure form. We use Azure CLI to list all Azure Key Vaults in our subscription.

{
    "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
    },
    "variables": {},
    "resources": [
        {
            "type": "Microsoft.Resources/deploymentScripts",
            "apiVersion": "2019-10-01-preview",
            "name": "myscript",
            "location": "[resourceGroup().location]",
            "kind": "AzureCLI",
            "identity": {
                "type": "UserAssigned",
                "userAssignedIdentities": {
                    "YOUR_IDENTITY": {
                    }
                }
            },
            "properties": {
                "azCliVersion": "2.0.80",
                "timeout": "PT30M",
                "cleanupPreference": "OnSuccess",
                "retentionInterval": "P1D",
                "scriptContent": "result=$(az keyvault list); echo $result | jq -c '{Result: map({id: .id})}' > $AZ_SCRIPTS_OUTPUT_PATH"
            }
        }
    ],
    "outputs": {
        "result": {
            "value": "[reference('myscript').outputs]",
            "type": "object"
        }
    }
}

Execution

# We execute the ARM template and format the output as json
az deployment group create --name "deployscript-test" --resource-group "deployscript-test-rg" --template-file PATH_TO_ARM_FILE | jq .properties.outputs.result.value.Result

Output

[
  {
    "id": "/subscriptions/bfsjkdbfjkdsfbkjsdbkdsf/resourceGroups/some-rg/providers/Microsoft.KeyVault/vaults/some-kv",
    "resourceGroup": "some-rg"
  },
  {
    "id": "/subscriptions/bfsjkdbfjkdsfbkjsdbkdsf/resourceGroups/some-rg/providers/Microsoft.KeyVault/vaults/some-other-kv",
    "resourceGroup": "some-rg"
  }
]

Conclusion

I executed inline code, but we can also execute a remote script. Instead of scriptContent, we would choose primaryScriptURI.

Join Newsletter
Get the latest updates right in your inbox. I never spam!
Sven Malvik
Written by Sven Malvik

Latest Stories

Introduction to Farmer - IaC with Azure

As many companies move their services to the cloud, the way we interact with the cloud, the tooling, becomes more important. In Azure we ...

Sep 26, 2020

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access ...

Feb 05, 2021

Using App Configuration in Azure DevOps

Application deployments dependent often on environment specific data like the name of a resource group, location or flags for certain use...

Aug 01, 2020

Using Feature Flags with Azure App Configuration

Sometimes we would like to test a new feature of an application. Or we would like to disable code junks because they are not fully implem...

May 16, 2020

Azure API Management with ARM

Deploying an ARM template (Azure Resource Management)-template from GitHub is the simplest way of provisioning an instance of API Managem...

Mar 28, 2020

AZ-303 Self-Study Guide for Becoming an Azure Solution Architect

Microsoft updated it’s role based exam for AZ-300. It’s now called AZ-303 and launched last year. This certification is a great proof for...

Feb 01, 2021

Serving Website Images from Azure CDN with SSL

In this post I will show you step by step how to serve images on a website from Azure CDN with SSL enabled. My blog has a couple of Azure...

Apr 25, 2020

Azure App Configuration Introduction

We build this great application that we configure exactly the way it fits into our environments, and then we realize that changing a conf...

May 09, 2020

ARM for NodeJS Azure Web App with App Configuration Integration

Our Azure infrastructure has some configurations that our developers need to know like the name of the currently active AKS cluster and A...

Jun 13, 2020

How to Recover a Virtual Machine with Azure Backup Service

This episode is about the Azure Backup Service, and how we can restore a virtual machine. I have already a Windows Server 2016 Datacenter...

Dec 29, 2020

Latest Stories

Introduction to Farmer - IaC with Azure

Introduction to Farmer - IaC with Azure

As many companies move their services to the cloud, the way we interact with the cloud, the tooling, becomes more important. In Azure we ...

Sep 26, 2020

Event-Driven Infrastructure with App Configuration

Event-Driven Infrastructure with App Configuration

Azure App Configuration is great for externalizing application configurations. But what if an application is our infrastructure? How coul...

Sep 12, 2020

How to Reference Key Vault Secrets in Azure API Management

How to Reference Key Vault Secrets in Azure API Management

In an enterprise, an Azure API Management instance is often shared by many teams and many developers. The developers may all have access ...

Feb 05, 2021

Using App Configuration in Azure DevOps

Using App Configuration in Azure DevOps

Application deployments dependent often on environment specific data like the name of a resource group, location or flags for certain use...

Aug 01, 2020

Using Feature Flags with Azure App Configuration

Using Feature Flags with Azure App Configuration

Sometimes we would like to test a new feature of an application. Or we would like to disable code junks because they are not fully implem...

May 16, 2020

Azure API Management with ARM

Azure API Management with ARM

Deploying an ARM template (Azure Resource Management)-template from GitHub is the simplest way of provisioning an instance of API Managem...

Mar 28, 2020

AZ-303 Self-Study Guide for Becoming an Azure Solution Architect

AZ-303 Self-Study Guide for Becoming an Azure Solution Architect

Microsoft updated it’s role based exam for AZ-300. It’s now called AZ-303 and launched last year. This certification is a great proof for...

Feb 01, 2021

Serving Website Images from Azure CDN with SSL

Serving Website Images from Azure CDN with SSL

In this post I will show you step by step how to serve images on a website from Azure CDN with SSL enabled. My blog has a couple of Azure...

Apr 25, 2020

Azure App Configuration Introduction

Azure App Configuration Introduction

We build this great application that we configure exactly the way it fits into our environments, and then we realize that changing a conf...

May 09, 2020

ARM for NodeJS Azure Web App with App Configuration Integration

ARM for NodeJS Azure Web App with App Configuration Integration

Our Azure infrastructure has some configurations that our developers need to know like the name of the currently active AKS cluster and A...

Jun 13, 2020

How to Recover a Virtual Machine with Azure Backup Service

How to Recover a Virtual Machine with Azure Backup Service

This episode is about the Azure Backup Service, and how we can restore a virtual machine. I have already a Windows Server 2016 Datacenter...

Dec 29, 2020