关于 writing-bicep-templates
writing-bicep-templates 是一项技能,提供编写 Azure Bicep 基础设施模板的规范和示例,面向配置 Container Apps、身份与角色分配等 Azure 资源的开发者。
主要功能
- 01资源命名与参数规范
- 02托管身份和 RBAC 模式
- 03Container Apps 配置指南
- 04Bicep 构建与部署验证
适用场景
- 01定义 Azure 基础设施模块
- 02配置基于身份的 ACR 拉取
- 03部署前验证模板
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ microsoft-foundry/ foundry-agent-webapp --skill writing-bicep-templates安装后在 Claude Code 中输入/writing-bicep-templates确认已加载。
详细介绍
Bicep 编码规范
目标:创建一致且安全的 Azure 基础设施
命名约定
使用 resourceToken(通过 uniqueString() 创建):
var token = toLower(uniqueString(subscription().id, environmentName, location))
name: '${abbrs.appContainerApps}web-${token}' // ca-web-abc123
例外:ACR 仅支持字母数字字符:cr${resourceToken}
参数
始终添加 @description(),并对受约束的值使用 @allowed():
@description('Environment (dev, prod)')
param environmentName string
@description('Azure region')
@allowed(['eastus2', 'westus2'])
param location string = 'eastus2'
输出
公开关键标识符,供 azd 和其他模块使用:
output containerAppName string = containerApp.name
output webEndpoint string = 'https://${containerApp.properties.configuration.ingress.fqdn}'
output identityPrincipalId string = containerApp.identity.principalId
托管标识
使用用户分配的 MI 进行 ACR 拉取和 OBO(避免循环依赖)。在基础设施模块中创建它,以便在创建 Container App 之前就能使用其 principalId:
resource managedIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2024-11-30' = {
name: '${abbrs.managedIdentityUserAssignedIdentities}web-${resourceToken}'
location: location
properties: { isolationScope: 'Regional' }
}
output managedIdentityPrincipalId string = managedIdentity.properties.principalId
使用 identity: { type: 'UserAssigned', userAssignedIdentities: { '${miId}': {} } } 将其附加到 Container App。使用 MI 进行 ACR 拉取,方式为 registries: [{ server: acr.loginServer, identity: miId }]。
RBAC 分配
使用 guid() 生成名称,并指定 principalType:
resource roleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
name: guid(resource.id, principalId, roleId)
properties: {
roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', roleId)
principalId: principalId
principalType: 'ServicePrincipal'
}
}
Container Apps
关键设置:系统标识、缩放至零,以及仅使用 HTTPS:
resource containerApp 'Microsoft.App/containerApps@2023-05-01' = {
identity: { type: 'UserAssigned', userAssignedIdentities: { '${userAssignedIdentityId}': {} } }
properties: {
configuration: {
ingress: {
external: true
targetPort: 8080
allowInsecure: false
}
}
template: {
scale: { minReplicas: 0, maxReplicas: 3 }
}
}
}
ACR 拉取模式
使用用户分配的 MI 进行 ACR 拉取(不使用管理员凭据或密钥):
registries: [{
server: containerRegistry.properties.loginServer
identity: userAssignedIdentityId // MI with AcrPull role
}]
验证
az bicep build --file main.bicep
az deployment group what-if --template-file main.bicep
项目特定:模块层次结构
main.bicep (subscription scope)
├─ Resource group
├─ main-infrastructure.bicep (ACR + Container Apps Env + Log Analytics + User-Assigned MI)
├─ entra-app.bicep (SPA app + conditional OBO backend app with FIC + admin consent)
├─ main-app.bicep (Container App with MI-based ACR pull)
└─ RBAC (Cognitive Services User role via postprovision CLI)
项目特定:Container App 配置
resource containerApp 'Microsoft.App/containerApps@2024-03-01' = {
identity: {
type: 'UserAssigned'
userAssignedIdentities: { '${userAssignedIdentityId}': {} }
}
properties: {
managedEnvironmentId: containerAppsEnvironmentId
configuration: {
ingress: {
external: true
targetPort: 8080
allowInsecure: false
}
registries: [{
server: containerRegistry.properties.loginServer
identity: userAssignedIdentityId // MI-based pull, no secrets
}]
}
template: {
containers: [{
name: 'web'
image: containerImage
env: containerEnv // Base env + conditional OBO env
resources: { cpu: json('0.5'), memory: '1Gi' }
}]
scale: { minReplicas: 0, maxReplicas: 3 }
}
}
}
output fqdn string = containerApp.properties.configuration.ingress.fqdn
output identityPrincipalId string = containerApp.identity.principalId
相关技能
- deploying-to-azure - 部署命令和钩子工作流
- writing-csharp-code - Container Apps 的后端配置
- troubleshooting-authentication - RBAC 和托管标识调试
评论
支持 Markdown