简体   繁体   中英

How are errors/failures handled in automated Azure Resource Group deployments?

I have read various articles on automatically deploying resources groups in Azure, including the use of templates and how to troubleshoot failed deployments.

What is not clear in those articles is what rollback capabilities are in built if any, and/or the best way to revert the resource infrastructure back to the last successful state in the event of errors midway through the deployment.

For example, in Octopus Deploy there is the notion of certain build steps that are only triggered in the event of failure which essentially put everything back the way it was before the deployment started.

I can see that it is possible to "validate" your templates and infrastructure by running the Test-AzureRmResourceGroupDeployment cmdlet to reduce potential errors before actually running the deployment, and also that it is possible to view the provisioning state following deployment by running Get-AzureRmResourceGroupDeployment :

在此处输入图片说明

from which I could check for the failed status and conditionally run a script to clean up after the failure.

However, is there anything built in to cater for this scenario?

While it is possible to roll a deployment over the top of an existing environment. The primary purpose of templates is (or appears to be) to deploy a new environment.

Which means that if something fails along the way, you delete the whole thing and start again. Or create your own logic to go in and figure out why. The only thing that Azure will do in the event of a failure is to report it to you. It is then up to you to make the decision about how you will react to that.

My personal approach is to deploy the basic building blocks via templates (so VMs, storage, etc) and then have a configuration management engine take over the more complex tasks of deploying software, and defining the configuration. Something that does have the intelligence to roll things back and repair them.

This is what I've put together. It may help but basically you have to throw your own error as the result from Test-AzureRmResourceGroupDeployment is null for success but has an object when it fails.

Do{
   Try{
       Write-Output "Testing Deployment..."
       If ($TestResult = Test-AzureRmResourceGroupDeployment -ResourceGroupName $ResourceGroupName -TemplateFile $VMTemplatePath -TemplateParameterObject $VMDeploymentParameters) {
          Throw "Testing failed.`r`n$($TestResult.Message)`r`n"
       }
       Write-Output "Testing complete."
       $TestResponse = "N"
   }
   Catch{
       Write-Output $_
       If ($TestResponse = (Read-Host "Would you like to try again? Y/N.") -ne "Y") { Exit }
   }
}
While($TestResponse -eq "Y")

Cheers

Lewis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM