Skip to content

Commit f903aba

Browse files
committed
Add a sample to the deployment script article
1 parent 93e3eac commit f903aba

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

articles/azure-resource-manager/bicep/deployment-script-bicep.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Use deployment scripts in Bicep
33
description: Learn how to create, monitor, and troubleshoot deployment scripts in Bicep.
44
ms.custom: devx-track-bicep
55
ms.topic: conceptual
6-
ms.date: 12/13/2023
6+
ms.date: 05/20/2024
77
---
88

99
# Use deployment scripts in Bicep
@@ -143,6 +143,75 @@ New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateFil
143143
Write-Host "Press [ENTER] to continue ..."
144144
```
145145

146+
## Use managed identity
147+
148+
The following example demonstrates how to use managed identity to interact with Azure from inside the deployment script.
149+
150+
```bicep
151+
@description('The location of the resources.')
152+
param location string = resourceGroup().location
153+
154+
@description('The storage account to list blobs from.')
155+
param storageAccountData {
156+
name: string
157+
container: string
158+
}
159+
160+
@description('The role id of Storage Blob Data Reader.')
161+
var storageBlobDataReaderRoleId = '2a2b9908-6ea1-4ae2-8e65-a410df84e7d1'
162+
163+
@description('The storage account to read blobs from.')
164+
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-04-01' existing = {
165+
name: storageAccountData.name
166+
}
167+
168+
@description('The Storage Blob Data Reader Role definition from [Built In Roles](https://learn.microsoft.com/en-us/azure/role-based-access-control/built-in-roles).')
169+
resource storageBlobDataReaderRoleDef 'Microsoft.Authorization/roleDefinitions@2022-05-01-preview' existing = {
170+
scope: subscription()
171+
name: storageBlobDataReaderRoleId
172+
}
173+
174+
@description('The user identity for the deployment script.')
175+
resource scriptIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-07-31-preview' = {
176+
name: 'script-identity'
177+
location: location
178+
}
179+
180+
@description('Assign permission for the deployment scripts user identity access to the read blobs from the storage account.')
181+
resource dataReaderRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
182+
scope: storageAccount
183+
name: guid(storageBlobDataReaderRoleDef.id, scriptIdentity.id, storageAccount.id)
184+
properties: {
185+
principalType: 'ServicePrincipal'
186+
principalId: scriptIdentity.properties.principalId
187+
roleDefinitionId: storageBlobDataReaderRoleDef.id
188+
}
189+
}
190+
191+
@description('The deployment script.')
192+
resource script 'Microsoft.Resources/deploymentScripts@2023-08-01' = {
193+
name: 'script'
194+
location: location
195+
kind: 'AzureCLI'
196+
identity: {
197+
type: 'UserAssigned'
198+
userAssignedIdentities: {
199+
'${scriptIdentity.id}': {}
200+
}
201+
}
202+
properties: {
203+
azCliVersion: '2.59.0'
204+
retentionInterval: 'PT1H'
205+
arguments: '${storageAccount.properties.primaryEndpoints.blob} ${storageAccountData.container}'
206+
scriptContent: '''
207+
#!/bin/bash
208+
set -e
209+
az storage blob list --auth-mode login --blob-endpoint $1 --container-name $2
210+
'''
211+
}
212+
}
213+
```
214+
146215
## Monitor and troubleshoot a deployment script
147216

148217
When you deploy a deployment script resource, you need a storage account to store the user script, the execution results, and the `stdout` file. You can specify your own storage account. For more information, see [Use an existing storage account](./deployment-script-develop.md#use-an-existing-storage-account).

0 commit comments

Comments
 (0)