Skip to content

Commit 60a5d2a

Browse files
authored
Fail function example (#26)
Added Azure Bicep Fail Function Example.
1 parent 94b7ab1 commit 60a5d2a

File tree

5 files changed

+122
-0
lines changed

5 files changed

+122
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Azure Bicep - Fail Function
2+
3+
## Introduction
4+
5+
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.
6+
7+
## 📃 Benefits of the Fail Function
8+
9+
**Early Validation**: Catches configuration errors before Azure resources are created.
10+
11+
**Clear Error Messages**: Provides custom, descriptive error messages.
12+
13+
**Enforces Standards**: Ensures naming conventions and business rules are followed consistently.
14+
15+
**Prevents Runtime Issues**: Stops deployments that would fail to function properly.
16+
17+
## Examples
18+
19+
### Storage Account Naming Convention
20+
21+
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.
22+
23+
```bicep
24+
var storageAccountPrefix = 'st'
25+
var storageAccountNameChecked = startsWith(storageAccountName, storageAccountPrefix)
26+
? storageAccountName
27+
: fail('The storage account name must start with "${storageAccountPrefix}".')
28+
```
29+
30+
### Web App Runtime Validation
31+
32+
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.
33+
34+
```bicep
35+
var varRuntime = contains(toUpper(runtime), 'DOTNET') ? runtime : fail('The runtime parameter must contain "DOTNET"!')
36+
```
37+
38+
## 🚀 Deployment
39+
40+
In Visual Studio Code open a terminal and run:
41+
42+
CLI
43+
44+
```bash
45+
az login
46+
az account set --subscription 'subscription name or id'
47+
az deployment group create -g 'your-rg' --confirm-with-what-if -f './storage.bicep' -p 'storage.bicepparam'
48+
// amend to webApp.bicep / webApp.bicepparam to test that example
49+
```
50+
51+
or PowerShell
52+
53+
```powershell
54+
Connect-AzAccount
55+
Set-AzContext -Subscription "subscription name or id"
56+
New-AzResourceGroupDeployment -Confirm -ResourceGroup "your-rg" -TemplateFile "storage.bicep" -TemplateParameterFile "storage.bicepparam"
57+
// amend to webApp.bicep / webApp.bicepparam to test that example
58+
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@description('Name of the storage account - without st prefix. ')
2+
param storageAccountName string
3+
4+
@description('Azure resource location.')
5+
param location string = resourceGroup().location
6+
7+
var storageAccountPrefix = 'st'
8+
var storageAccountNameChecked = startsWith(storageAccountName, storageAccountPrefix)
9+
? storageAccountName
10+
: fail('The storage account name must start with "${storageAccountPrefix}".')
11+
12+
resource resStorageAccount 'Microsoft.Storage/storageAccounts@2022-09-01' = {
13+
name: storageAccountNameChecked
14+
location: location
15+
sku: {
16+
name: 'Standard_LRS'
17+
}
18+
kind: 'StorageV2'
19+
properties: {}
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using './storage.bicep'
2+
3+
param storageAccountName = 'blobstorageaccount001'
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@description('The name of the App Service Plan')
2+
param appServicePlanName string = uniqueString(resourceGroup().id)
3+
4+
@description('The name of the Web App')
5+
param webAppName string = uniqueString(resourceGroup().id)
6+
7+
// Fail the deployment if the runtime is not specified
8+
@description('The runtime stack for the Web App, e.g. "DOTNET|9.0"')
9+
param runtime string = ''
10+
11+
// Fail if runtime does not contain 'DOTNET'
12+
var varRuntime = contains(toUpper(runtime), 'DOTNET') ? runtime : fail('The runtime parameter must contain "DOTNET"!')
13+
14+
resource appServicePlan 'Microsoft.Web/serverfarms@2024-11-01' = {
15+
name: appServicePlanName
16+
location: resourceGroup().location
17+
sku: {
18+
name: 'F1'
19+
tier: 'Free'
20+
}
21+
properties: {
22+
reserved: true
23+
}
24+
}
25+
26+
resource webApp 'Microsoft.Web/sites@2024-11-01' = {
27+
name: webAppName
28+
location: resourceGroup().location
29+
kind: 'app'
30+
properties: {
31+
serverFarmId: appServicePlan.id
32+
httpsOnly: true
33+
siteConfig: {
34+
linuxFxVersion: varRuntime
35+
}
36+
}
37+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using './webApp.bicep'
2+
3+
//change to param runtime = 'DOTNETCORE|9.0' to pass fail function
4+
param runtime = 'PYTHON|3.11'

0 commit comments

Comments
 (0)