Functions & Expressions

💤0
Lv 10 XP
← 📜 Infrastructure as Code · ARM Templates

Functions & Expressions

Advanced ⭐ 120 XP ⏱ 14 min #arm#azure#functions

Use ARM template functions to build dynamic names, references, and values.

📖Theory

ARM templates use functions inside [ ] expressions to compute values at deploy time. The ones you’ll meet most:

  • resourceGroup() / subscription() — deployment context (location, id)
  • parameters() / variables() — pull declared values
  • concat() / format() — build strings (e.g. resource names)
  • uniqueString() — deterministic hash for globally-unique names
  • reference() / resourceId() — refer to other resources’ runtime props

These let one template adapt to its environment — generating unique storage account names, deriving locations, and wiring resource dependencies dynamically.

🌍Real-World Example
"variables": {
  "storageName": "[concat('st', uniqueString(resourceGroup().id))]",
  "location": "[resourceGroup().location]"
},
"resources": [{
  "type": "Microsoft.Storage/storageAccounts",
  "apiVersion": "2023-01-01",
  "name": "[variables('storageName')]",
  "location": "[variables('location')]",
  "sku": { "name": "Standard_LRS" },
  "kind": "StorageV2"
}]
✍️Hands-On Exercise
  1. Use concat and uniqueString to build a unique storage account name.
  2. Reference the deployment’s location with resourceGroup().location.
  3. Explain what reference() retrieves and when you’d use it.
  4. Why is uniqueString deterministic, and why does that matter?
🧾Cheat Sheet
FunctionUse
resourceGroup()RG context (location, id)
parameters('x')Read a parameter
variables('x')Read a variable
concat() / format()Build strings
uniqueString()Deterministic unique hash
resourceId()Build a resource ID
reference()Read a resource’s runtime props
💬Common Interview Questions
How do you generate a globally unique resource name in ARM?

Combine a prefix with uniqueString of a stable scope, e.g. concat('st', uniqueString(resourceGroup().id)) — deterministic, so repeated deployments produce the same name.

What does the reference() function do?

It retrieves runtime properties of a resource (like an endpoint or key) so you can use values that only exist after a resource is deployed.

📚Official Documentation

📝 My notes on this topic

Auto-saves as you type