用途
ARM Templates 与 Bicep 用于以 Azure 原生方式部署 Azure 基础设施。Bicep 是推荐使用的领域特定语言,可编译为 ARM JSON,提供更简洁的语法、模块和一等工具链支持。该技能适用于需要 Azure 原生基础设施即代码(IaC)而不依赖第三方工具、组织标准化 Azure 并希望与门户紧密集成、部署前需要 What-If 分析、将现有 ARM JSON 模板迁移到 Bicep 以提升可维护性,以及在资源组、订阅、管理组或租户级别进行部署的场景。
主要能力
- Bicep 基础:使用参数、变量、资源声明和输出定义部署。
- 资源组部署:示例包含虚拟网络与网络安全组,配置子网和 NSG 规则。
- 虚拟机部署:示例包含网络接口、系统分配托管标识、SSH 公钥认证和启动诊断。
- Bicep 模块:定义可复用模块(如存储账户),并在主模板中引用模块输出。
- ARM JSON 模板结构:包含 schema、contentVersion、parameters、variables、resources 和 outputs。
- 部署命令:验证、What-If 预览、资源组/订阅/管理组级别部署、导出资源组、反编译 ARM JSON 到 Bicep、构建 Bicep 到 ARM JSON、列出和删除部署。
- 参数文件:支持 JSON 参数文件,并可从 Key Vault 引用机密。
- 链接与嵌套模板、故障排查及相关技能。
使用方式
前置条件:安装 Azure CLI(需组织批准的已验证签名包),安装 Bicep CLI(Azure CLI 2.20+ 已捆绑),验证安装,登录并设置订阅。
# Install Azure CLI
# Prerequisite: organization-approved Azure CLI installed from a verified signed package.
az version
# Install Bicep CLI (bundled with Azure CLI 2.20+)
az bicep install
az bicep upgrade
# Verify installation
az bicep version
登录并设置订阅:
az login
az account set --subscription "my-subscription-id"
部署命令示例:
# Validate a Bicep template before deployment
az deployment group validate \
--resource-group mygroup \
--template-file main.bicep \
--parameters environment='prod' baseName='myapp'
# Preview changes with What-If
az deployment group what-if \
--resource-group mygroup \
--template-file main.bicep \
--parameters environment='prod' baseName='myapp'
# Deploy Bicep to resource group
az deployment group create \
--resource-group mygroup \
--template-file main.bicep \
--parameters environment='prod' baseName='myapp' \
--name "deploy-$(date +%Y%m%d-%H%M%S)"
# Deploy ARM JSON with parameter file
az deployment group create \
--resource-group mygroup \
--template-file template.json \
--parameters @parameters.prod.json
# Subscription-level deployment (e.g., resource groups, policies)
az deployment sub create \
--location eastus \
--template-file subscription-level.bicep \
--parameters @params.json
# Management group deployment
az deployment mg create \
--management-group-id my-mg \
--location eastus \
--template-file mg-policy.bicep
# Export resource group to ARM JSON
az group export --name mygroup --output json > exported-template.json
# Decompile ARM JSON to Bicep
az bicep decompile --file exported-template.json
# Build Bicep to ARM JSON (for inspection)
az bicep build --file main.bicep --outfile main.json
# List deployments and their status
az deployment group list \
--resource-group mygroup \
--output table
# Delete a failed deployment
az deployment group delete \
--resource-group mygroup \
--name my-failed-deployment
适用场景
- 需要 Azure 原生 IaC 且不引入第三方工具。
- 组织标准化 Azure,希望与门户紧密集成。
- 部署变更前需要 What-If 分析。
- 将现有 ARM JSON 模板迁移到 Bicep 以提升可维护性。
- 需要在资源组、订阅、管理组或租户级别进行部署。
限制
- 基础设施命令可能中断服务:变更状态前需确认目标主机/范围,并准备备份或快照。
- 仅文档导入:上游脚本和模板未随附。