writing-bicep-templates について
writing-bicep-templatesは、Azure Bicepのインフラテンプレートを作成するための規約と例をまとめたスキルです。Container Apps、ID、ロール割り当てなどを構成する開発者向けです。
主な機能
- 01リソース命名とパラメーターの規約
- 02マネージドIDとRBACのパターン
- 03Container Appsの構成ガイド
- 04Bicepのビルドとデプロイ検証
活用シーン
- 01Azureインフラモジュールの定義
- 02IDベースのACRプルを構成
- 03デプロイ前にテンプレートを検証
インストール
コマンドをコピーしてターミナルで実行します。
npx skills add https:/ / github.com/ microsoft-foundry/ foundry-agent-webapp --skill writing-bicep-templatesインストール後、Claude Code で/writing-bicep-templatesを入力して読み込まれたか確認します。
詳細
Bicep コーディング標準
目標:一貫性があり、安全な Azure インフラストラクチャを構築します
命名規則
uniqueString() の resourceToken を使用します。
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
マネージド ID
ACR のプルと OBO にはユーザー割り当て MI を使用します(循環依存を回避できます)。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 にアタッチします。registries: [{ server: acr.loginServer, identity: miId }] を使用して ACR のプルに MI を使います。
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
主要な設定:システム ID、ゼロへのスケール、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 のプルパターン
ACR のプルにはユーザー割り当て MI を使用します(管理者の資格情報やシークレットは不要です)。
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 とマネージド ID のデバッグ