Deploy an ARM Template with Verification and Cleanup: Step‑by‑Step Guidance
0 reputation · 14 Dec 2021, 04:21 UTC
0 reputation · 14 Dec 2021, 04:21 UTC
I need to deploy a simple ARM template that creates a storage account and an App Service. Using Azure CLI, what are the exact commands, prerequisites, and numbered steps to create the resource group, deploy the template, verify that the deployment succeeded, and then clean up the resources? How should I handle errors, use verbose or debug output, and rollback if the deployment fails? Please include concrete examples, verification steps (e.g., checking ProvisioningState), and cleanup commands. Also explain how to set the correct location for the resource group and why it matters.
34775 reputation · 14 Dec 2021, 15:09 UTC
To deploy a storage account and App Service using an ARM template, follow these sequential steps. This assumes you have the Azure CLI installed and are authenticated via az login.
First, define your variables to ensure consistency across commands. The location (e.g., eastus) is critical because it determines the physical data center where your resources reside, affecting latency, regional availability of specific SKUs, and compliance with data residency laws.
# Define variables
RG_NAME="MyDemoRG"
LOCATION="eastus"
TEMPLATE_FILE="azuredeploy.json"
PARAMS_FILE="azuredeploy.parameters.json"
DEPLOYMENT_NAME="DemoDeployment"Create the resource group:
az group create --name $RG_NAME --location $LOCATIONBefore committing resources, validate the template to catch syntax errors or invalid parameter values without incurring costs.
az deployment group validate --resource-group $RG_NAME --template-file $TEMPLATE_FILE --parameters $PARAMS_FILEIf validation succeeds, execute the deployment. Use --verbose to see detailed progress in the terminal.
az deployment group create --resource-group $RG_NAME --template-file $TEMPLATE_FILE --parameters $PARAMS_FILE --name $DEPLOYMENT_NAME --verboseChecking the CLI output is insufficient for automation. You must verify the provisioningState of the deployment object.
az deployment group show --resource-group $RG_NAME --name $DEPLOYMENT_NAME --query properties.provisioningState -o tsvThe expected output is Succeeded. If the output is Failed, inspect the error details using:
az deployment group show --resource-group $RG_NAME --name $DEPLOYMENT_NAME --query properties.errorYou can also confirm the expected resources exist:
az resource list --resource-group $RG_NAME --output tableTo avoid ongoing costs, delete the entire resource group. This is the most efficient way to remove all associated resources (Storage Account, App Service, and App Service Plan) simultaneously.
az group delete --name $RG_NAME --yes --no-waitVerify cleanup by attempting to show the group; it should return a ResourceGroupNotFound error.
az group show --name $RG_NAMEIf a deployment hangs or fails with a generic error, append --debug to your command. This provides the raw HTTP request/response logs between the CLI and the Azure Resource Manager API.
ARM templates do not have a built-in "undo" button for a failed deployment. You have two primary options:
create command again. ARM is idempotent, meaning it will only update the resources that need changing.Note: To provide more specific rollback advice, please specify if you are using a CI/CD pipeline (like GitHub Actions or Azure DevOps) or deploying manually from a local terminal.
Use comments to ask for clarification. Post a solution as an answer.
34,775 reputation · 14 Dec 2021, 08:50 UTC
To build on the deployment steps provided, it is important to distinguish between the two deployment modes available in ARM: Incremental (the default) and Complete. This choice significantly impacts how your resource group is managed during the process.
For verification, you can check which mode was used by inspecting the deployment properties via the Azure Portal or CLI. If you are automating a cleanup-and-redeploy cycle, using a dedicated resource group for each deployment is the safest way to avoid the risks associated with Complete mode.