Delete resources created from template file by Azure az cli? - azure

With az deployment group create --template-file vm.bicep -g "Something", I can create just fine things which are defined in the file vm.bicep (or ARM template vm.json for that matter).
But how do I actually get rid of EXACTLY just the resources that had been created by running create with a template file? az deployment group delete doesn't support a --template-file parameter.
Edit: Clarification: I only want to get rid of what has been created by the template. Not everything in a Resource Group.

Related

Possibility to create and delete databricks resources via Azure CLI deployments

Normally when you create resources using ARM templates and the azure CLI you can create using:
az deployment group create --resource-group $resourceGroupName --template-file "infra/template.json" --parameters "infra/parameters.json"
Then when I want to delete the resources I can:
az group delete --name $resourceGroupName
However, with Databricks this will create other resource groups as well. Even if you delete the DB resource group, you've still got these other lingering resource groups. I'd like to be able to delete the databricks AND all of the other resources that were created, without having to do extra manual steps.
Any idea of a clean way to do this?
The managed resource group created by Databricks cannot be deleted manually since it was created by the Databricks resource itself. The deny assignment prevents deletion of the managed resource group.
One way to remove resource is to delete the existing workspace following is the example using azure cli
Through AZURE CLI
I have created resource group tvs and databricks tvs for the demo purpose
Delete resource
Use following command in AzureCLI to delete a resource.
Azure CLICopy
az resource delete \
--resource-group tvs\
--name tvs\
--ids *****\
(NOTE : ids is the id of the resource that can be picked from JSON view)
Before deletion
After deletion
Delete resource group
Use following command in AzureCLI to delete the resource group.
Azure CLICopy
az group delete --name tvs
Azure doesn’t currently provide a way to delete multiple resource groups at the same time.
Here’s a method that works for me.
Open Azure Portal
Click on Resource Groups
Select the Resource Groups that you want to delete
Click “Assign tags”
Assign a new tag called “disposable-service”(can be named anything) and tag value to be true
Open Azure Cloud Shell https://shell.azure.com or click on the Azure Shell icon in the Azure Portal toolbar.
the following script into Cloud Shell and hit enter.
az group list --tag disposable-service=true --query "[].[id]" --output tsv

New Azure Resources Show Up in Portal but not CLI

After running a DevOps pipeline that created several resources, only two (a new resource group and a Functions app) show up in Azure CLI using az ... list --subscription ....
Other resources, like Static Web Apps and a SQL Server don't show up in that list or when running az staticwebapp list --subscription ... or az sql server list --subscription ... with the subscription argument specified. I have confirmed that these resources DO show up in the portal, and my account is listed as an owner (inherited from subscription) for them in the portal.
I have deployed Azure Static WebApp using Azure Devops.
Followed this MSDoc to publish the static web app.
By using,
az staticwebapp list --resource-group YourRGName
and
az staticwebapp list --subscription YourSubscriptionID
,Iam able to get the StaticWeb App list
For anyone else running into this:
I have not found a solution to getting the az staticwebapp list command to work for my use case, as the results that show up immediately after creating a resource are just inconsistent. However, as a workaround that should satisfy most use cases, using this API (using az rest command) to find all resources associated with the group created by the pipeline, then filtering those down to find the Static Web App I want, seems to work.
Then, when using other APIs that reference that app before it shows up in the normal list, fully specifying subscription and resource group alongside the app's name seems to make things more reliable too.
As far as I can tell, this is probably just a limitation with the inconsistency of how long information about newly deployed resources takes to propagate that shows up when trying to find a resource immediately after creating it.

How to ignore locked resources with 'Complete' mode deployment to Azure?

Our team is using a deployment that uses multiple ARM templates to setup our environment. The first ARM template is set to deployment mode 'Complete' and removes everything, but a storage account. We're using Azure CLI to make the deployment:
az group deployment create \
--mode Incremental \
--resource-group $resourceGroupName \
--template-file $BUILD_SOURCESDIRECTORY'/Infrastructure/azuredeploy.json' \
--parameters $BUILD_SOURCESDIRECTORY'/Infrastructure/azuredeploy.parameters.'$environment'.json' \
--query $query \
--output json
However one of our resource groups contains a few locked resources (which are managed by a different team). In that particular case the strategy with a 'Complete' deployment mode fails, because Azure cannot remove the locked resources.
Understandably of course, but maybe there's a way around this? Can we, for example, instruct the ARM template to ignore specific resources? Or use CLI to instruct something similar?
The obvious way would be to move the resources to a separate resource group, but unfortunately that's not a possiblity for us. I couldn't find any other way yet, but maybe I missed something. Thanks for any answers in advance.
Another way to get around this apart from moving resources to a separate resource group (which you say is ruled out in your case anyway), would be to use Conditions with your resources.
Do note that in complete mode, Resource Manager deletes resources that exist in the resource group but aren't specified in the template. Resources that are specified in the template, but not deployed because a condition evaluates to false, aren't deleted.
For more detail on the syntax and examples, please refer to the following resources:
Structure and syntax of Azure Resource Manager templates
Conditionally deploy a resource in an Azure Resource Manager template
Hope this helps!

ARM: The easiest way to recreate everything in a resource group into another RG in the same subscription?

I have created a relatively complex IaaS environment in one of my resource groups. The environment is working very well. Now I need to re-build the same environment in another RG for testing and validation.
What would be the easiest way to re-create the same environment in another Resource Group in the same subscription? I tried to export the resource group and downloaded it. The problem is that the file “parameters.json” includes hard coded references to the original resource group name.
Is there an easy way to copy all contents of a RG to another RG in the same environment?
Thank you,
Two approaches can be used here. You can remove the resource group reference from the template and parameter files and then simply specify the resource group when you deploy from the template using PowerShell, the portal, Azure CLI, etc.
To deploy using this method in PowerShell
New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ExampleResourceGroup -TemplateFile <PathToTemplate> -TemplateParameterFile <PathToParameterFile>
Or
You can change the resource group to the new resource group in the parameters file and deploy.
You can read more about deploying using templates here.
Edit:
Just a note but you don't have to use a separate file for parameters. You can easily include the parameters in the template file as well.

How to use Azure CLI to configure or manage deployment slots in ARM mode

Looks like Azure Powershell has option of slots but ARM mode of Azure CLI is missing these capabilities.
How to setup an ARM template to create a deployment slot?
How to use Azure CLI to deploy an ARM template for a specific slot
Reference:
https://azure.microsoft.com/en-us/documentation/articles/web-sites-staged-publishing/
Looks like $ azure group deployment create is missing the slot option but it's present in $ azure webapp create <resource-group> <name> --slot <slot-name> -l <location> -p <server-farm-plan-id>.
May be we should create webapp first and then use regular ARM template to update a particular slot by specifying parameter value webSiteName = in following ARM template parameters file.
https://github.com/Azure/azure-quickstart-templates/blob/master/201-web-app-java-tomcat/azuredeploy.parameters.json

Resources