当使用 Azure 资源管理器模板(ARM 模板)或 Bicep 文件部署 Azure 资源失败时,你会收到错误代码。 本文介绍如何查找错误代码,以便排查问题。 有关错误代码的更多信息,请参阅 常见部署错误。
错误类型
有三种类型的错误与部署相关:
- 验证错误发生在部署开始之前,由文件中的语法错误造成。 像 Visual Studio Code 这样的代码编辑器可以识别这些错误。
- 当运行部署命令但未部署资源时,将发生预检验证错误。 这些错误是部署未开始的情况下出现的。 例如,如果某个参数值不正确,则会在预检验证中发现错误。
- 部署错误发生在部署过程中,只能通过评估 Azure 环境中的部署进度来发现。
所有类型的错误都会返回用于排查部署问题的错误代码。 验证错误和预检错误会显示在活动日志中,但不会显示在部署历史记录中。 存在语法错误的 Bicep 文件不会编译为 JSON,也不会显示在活动日志中。
若要识别语法错误,可以将 Visual Studio Code 与最新的 Bicep 扩展 或 Azure 资源管理器工具扩展一起使用。
验证错误
在部署过程中验证模板并显示错误代码。 在运行部署之前,您可以通过使用 Azure PowerShell 或 Azure CLI 运行验证测试来识别验证和印前检查错误。
可以从门户部署 ARM 模板。 如果模板存在语法错误,则在尝试运行部署时会看到验证错误。 有关门户部署的更多信息,请参阅 从自定义模板部署资源。
以下示例尝试部署存储帐户,并发生验证错误。
选择消息获取更多详细信息。 模板存在语法错误,错误代码 InvalidTemplate
为 。
Summary (摘要) 显示表达式缺少右括号。
要在部署之前验证 ARM 模板,请运行 Test-AzResourceGroupDeployment。
Test-AzResourceGroupDeployment `
-ResourceGroupName examplegroup `
-TemplateFile azuredeploy.json
输出显示错误代码,例如 InvalidTemplateDeployment
或 AccountNameInvalid
可用于对模板进行故障排除和修复。
对于 Bicep 文件,语法验证问题的输出显示参数错误。
Test-AzResourceGroupDeployment: Cannot retrieve the dynamic parameters for the cmdlet.
Cannot find path '/tmp/11111111-1111-1111-1111-111111111111/main.json' because it does not exist.
若要获取更多故障排除信息,请使用 Bicep 生成 命令。 输出在括号中显示每个错误的行号和列号,以及错误消息。
bicep build main.bicep
/azuredeploy.bicep(22,51) : Error BCP064: Found unexpected tokens in interpolated expression.
/azuredeploy.bicep(22,51) : Error BCP004: The string at this location is not terminated due to an
unexpected new line character.
其他范围
有 Azure PowerShell cmdlet 用于验证订阅、管理组和租户范围的部署模板。
若要在部署之前验证 ARM 模板,请运行 az deployment group validate。
az deployment group validate \
--resource-group examplegroup \
--template-file azuredeploy.json
输出显示错误代码,例如 InvalidTemplateDeployment
或 AccountNameInvalid
可用于对模板进行故障排除和修复。
对于 Bicep 文件,输出在括号中显示每个错误的行号和列号,以及错误消息。
az deployment group validate \
--resource-group examplegroup \
--template-file main.bicep
/azuredeploy.bicep(22,51) : Error BCP064: Found unexpected tokens in interpolated expression.
/azuredeploy.bicep(22,51) : Error BCP004: The string at this location is not terminated due to an
unexpected new line character.
其他范围
有 Azure CLI 命令用于验证订阅、管理组和租户范围的部署模板。
部署错误
部署 Azure 资源时,会处理多个作。 当作通过验证但在部署期间失败时,会发生部署错误。 您可以查看有关资源组的每个部署作和每个部署的消息。
要查看有关部署作的消息,请使用资源组的 Activity log(活动日志):
登录到 Azure 门户。
转到 Resource groups (资源组 ) 并选择部署的资源组名称。
选择 活动日志。
使用筛选条件查找作的错误日志。
选择错误日志以查看作的详细信息。
要查看部署的结果,请执行以下作:
转到资源组。
选择 Settings>Deployments。
选择 Error details (错误详细信息 ) 作为部署。
将显示错误消息和错误代码 NoRegisteredProviderFound
。
要使用 PowerShell 查看部署的作消息,请使用 Get-AzResourceGroupDeploymentOperation。
要显示部署的所有作,请执行以下作:
Get-AzResourceGroupDeploymentOperation `
-DeploymentName exampledeployment `
-ResourceGroupName examplegroup
要指定特定属性类型:
(Get-AzResourceGroupDeploymentOperation `
-DeploymentName exampledeployment `
-ResourceGroupName examplegroup).StatusCode
要获取部署的结果,请使用 Get-AzResourceGroupDeployment。
Get-AzResourceGroupDeployment `
-DeploymentName exampledeployment `
-ResourceGroupName examplegroup
其他范围
有 Azure PowerShell cmdlet 用于获取订阅、管理组和租户范围的部署信息。
若要使用 Azure CLI 查看部署的作消息,请使用 az deployment operation group list。
要显示部署的所有作,请执行以下作:
az deployment operation group list \
--name exampledeployment \
--resource-group examplegroup \
--query "[*].properties"
要指定特定属性类型:
az deployment operation group list \
--name exampledeployment \
--resource-group examplegroup \
--query "[*].properties.statusCode"
若要获取部署的结果,请使用 az deployment group show。
az deployment group show \
--resource-group examplegroup \
--name exampledeployment
其他范围
有 Azure CLI 命令用于获取订阅、管理组和租户范围的部署信息。
后续步骤