0

I have a requirement to create a AZ CLI script to create a few resources and I would like to make it idempotent in a way that it checks if a resource exists before tries creating it.

The is what I have for creating an action group but my question is for checking any resource to see if it exists.

az monitor action-group create `
--name $ActionGroupName `
--resource-group $ResourceGroupName `
--short-name $AGShortName

The below will throw error if the resource does not exist. How can I safely check if a resource exists?

az monitor action-group show `
--name $ActionGroupName `
--resource-group $ResourceGroupName `
Parth Sekar
  • 164
  • 8
  • Does this answer your question? [Azure Powershell - Check to see if resource exists](https://stackoverflow.com/questions/30076601/azure-powershell-check-to-see-if-resource-exists) – easleyfixed Apr 05 '23 at 15:39
  • @easleyfixed That's for PowerShell, this question is for az CLI. So no, it doesn't answer the question. – Daniel Mann Apr 05 '23 at 15:57
  • Oh I thought you would have the rights to run a shell command to find out, my bad, hope there is a cli one. – easleyfixed Apr 05 '23 at 16:07
  • What about this one? https://stackoverflow.com/questions/46458034/azure-cli-how-to-check-if-a-resource-exists?rq=2 – easleyfixed Apr 05 '23 at 16:21
  • That is az group specific. We don't have an `exists` method for other resources. Hence, the question. – Parth Sekar Apr 05 '23 at 16:40

1 Answers1

0

Please use below command for a graceful wait:

az resource wait --exists --name "nameofactiongroup" \
--resource-group "resourcegroupname" \
--resource-type "actiongroups" \
--namespace Microsoft.Insights

The above command will wait till that particular resource is created/exists

You can find more options about that command: az resource wait --help

enter image description here

Parth Sekar
  • 164
  • 8
Shiva Patpi
  • 197
  • 5