Skip to content

Instantly share code, notes, and snippets.

@olafloogman
Created December 8, 2021 04:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olafloogman/e5f623d881cd49742f78f72c503655c5 to your computer and use it in GitHub Desktop.
Save olafloogman/e5f623d881cd49742f78f72c503655c5 to your computer and use it in GitHub Desktop.
targetScope = 'resourceGroup'
@description('Resource name to check in current scope (resource group)')
param resourceName string
@description('Resource ID of user managed identity with reader permissions in current scope')
param identityPrincipalId string
param location string = resourceGroup().location
param utcValue string = utcNow()
// used to pass into deployment script
var resourceGroupName = resourceGroup().name
// The script below performs an 'az resource list' command to determine whether a resource exists
resource resource_exists_script 'Microsoft.Resources/deploymentScripts@2020-10-01' = {
name: 'resource_exists'
location: location
kind: 'AzureCLI'
identity: {
type: 'UserAssigned'
userAssignedIdentities: {
'${identityPrincipalId}': {}
}
}
properties: {
forceUpdateTag: utcValue
azCliVersion: '2.15.0'
timeout: 'PT10M'
arguments: '\'${resourceGroupName}\' \'${resourceName}\''
scriptContent: 'result=$(az resource list --resource-group ${resourceGroupName} --name ${resourceName}); echo $result; echo $result | jq -c \'{Result: map({name: .name})}\' > $AZ_SCRIPTS_OUTPUT_PATH'
cleanupPreference: 'OnSuccess'
retentionInterval: 'P1D'
}
}
output exists bool = length(resource_exists_script.properties.outputs.Result) > 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment