Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions bicep-examples/fail-function/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Azure Bicep - Fail Function

## Introduction

The `fail()` function in Bicep allows you to stop deployments with custom error messages when conditions are not met. This is useful for enforcing business rules, validating parameters, and ensuring proper configuration before resources are deployed.

## 📃 Benefits of the Fail Function

✅ **Early Validation**: Catches configuration errors before Azure resources are created.

✅ **Clear Error Messages**: Provides custom, descriptive error messages.

✅ **Enforces Standards**: Ensures naming conventions and business rules are followed consistently.

✅ **Prevents Runtime Issues**: Stops deployments that would fail to function properly.

## Examples

### Storage Account Naming Convention

Ensures storage account names start with "st" prefix. The fail function here is populating the `storageAccountNameChecked` var with our fail condition, if the `storageAccountName` value does not start with the value from `storageAccountPrefix` then it will fail pre-deployment.

```bicep
var storageAccountPrefix = 'st'
var storageAccountNameChecked = startsWith(storageAccountName, storageAccountPrefix)
? storageAccountName
: fail('The storage account name must start with "${storageAccountPrefix}".')
```

### Web App Runtime Validation

Ensures that the DOTNET runtime stack is specified. In this example, the `fail` function checks whether the `runtime` parameter includes "DOTNET" by using the `contains` function. If "DOTNET" is not found in the `runtime` value, the deployment will be stopped before any resources are created, displaying your custom error message.

```bicep
var varRuntime = contains(toUpper(runtime), 'DOTNET') ? runtime : fail('The runtime parameter must contain "DOTNET"!')
```

## 🚀 Deployment

In Visual Studio Code open a terminal and run:

CLI

```bash
az login
az account set --subscription 'subscription name or id'
az deployment group create -g 'your-rg' --confirm-with-what-if -f './storage.bicep' -p 'storage.bicepparam'
// amend to webApp.bicep / webApp.bicepparam to test that example
```

or PowerShell

```powershell
Connect-AzAccount
Set-AzContext -Subscription "subscription name or id"
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "storage.bicep" -TemplateParameterFile "storage.bicepparam"
// amend to webApp.bicep / webApp.bicepparam to test that example
```
20 changes: 20 additions & 0 deletions bicep-examples/fail-function/storage.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@description('Name of the storage account - without st prefix. ')
param storageAccountName string

@description('Azure resource location.')
param location string = resourceGroup().location

var storageAccountPrefix = 'st'
var storageAccountNameChecked = startsWith(storageAccountName, storageAccountPrefix)
? storageAccountName
: fail('The storage account name must start with "${storageAccountPrefix}".')

resource resStorageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
name: storageAccountNameChecked
location: location
sku: {
name: 'Standard_LRS'
}
kind: 'StorageV2'
properties: {}
}
3 changes: 3 additions & 0 deletions bicep-examples/fail-function/storage.bicepparam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using './storage.bicep'

param storageAccountName = 'blobstorageaccount001'
37 changes: 37 additions & 0 deletions bicep-examples/fail-function/webApp.bicep
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
@description('The name of the App Service Plan')
param appServicePlanName string = uniqueString(resourceGroup().id)

@description('The name of the Web App')
param webAppName string = uniqueString(resourceGroup().id)

// Fail the deployment if the runtime is not specified
@description('The runtime stack for the Web App, e.g. "DOTNET|9.0"')
param runtime string = ''

// Fail if runtime does not contain 'DOTNET'
var varRuntime = contains(toUpper(runtime), 'DOTNET') ? runtime : fail('The runtime parameter must contain "DOTNET"!')

resource appServicePlan 'Microsoft.Web/serverfarms@2024-11-01' = {
name: appServicePlanName
location: resourceGroup().location
sku: {
name: 'F1'
tier: 'Free'
}
properties: {
reserved: true
}
}

resource webApp 'Microsoft.Web/sites@2024-11-01' = {
name: webAppName
location: resourceGroup().location
kind: 'app'
properties: {
serverFarmId: appServicePlan.id
httpsOnly: true
siteConfig: {
linuxFxVersion: varRuntime
}
}
}
4 changes: 4 additions & 0 deletions bicep-examples/fail-function/webApp.bicepparam
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
using './webApp.bicep'

//change to param runtime = 'DOTNETCORE|9.0' to pass fail function
param runtime = 'PYTHON|3.11'