Bicep/ARM/CLI スクリプト集(Azure/Microsoft Entra 中心)
- 山崎行政書士事務所
- 9月13日
- 読了時間: 5分

0) 前提
ログ送付先の Log Analytics Workspace(LAW) と、長期保管用の Storage Account(必要ならイミュータブル/WORM)を用意。
テナントスコープのデプロイが必要なもの(Entra の診断設定など)は、Tenant Root でのデプロイ権限が必要。
変数は <...> を各自の値に置換。
1) Entra(旧AAD)の診断ログ → Log Analytics(Bicep:テナントスコープ)
目的:AuditLogs/SignInLogs 等を LAW に出力(Storage/EH 併送も可)。備考:CLI では扱いにくいため Bicep/ARM 推奨。
entra-diagnostics.bicep
targetScope = 'tenant'
// ========= Parameters =========
@description('Diagnostic setting name')
param diagName string = 'entra-to-law'
@description('Destination LAW resourceId')
param lawResourceId string
@allowed([
[]
// 代表カテゴリ(必要に応じて削る)
// 'AuditLogs','SignInLogs','NonInteractiveUserSignInLogs','ServicePrincipalSignInLogs',
// 'ManagedIdentitySignInLogs','ProvisioningLogs','ADFSSignInLogs','B2CRequests'
])
var categories = [
'AuditLogs'
'SignInLogs'
'NonInteractiveUserSignInLogs'
'ServicePrincipalSignInLogs'
'ManagedIdentitySignInLogs'
'ProvisioningLogs'
]
// ========= Resource =========
resource aadDiag 'microsoft.aadiam/diagnosticSettings@2017-04-01' = {
name: diagName
properties: {
workspaceId: lawResourceId
logs: [for c in categories: {
category: c
enabled: true
retentionPolicy: {
enabled: false
days: 0
}
}]
}
}
デプロイ(例)
az deployment tenant create \
--name entra-diag-$(date +%Y%m%d%H%M) \
--template-file ./entra-diagnostics.bicep \
--parameters lawResourceId="/subscriptions/<SUBID>/resourceGroups/<RG>/providers/Microsoft.OperationalInsights/workspaces/<LAW>"
2) Log Analytics:保持期間(ワークスペース/テーブル)設定
2-1) CLI(最速)
# ワークスペースのインタラクティブ保持(日数)
az monitor log-analytics workspace update \
--resource-group <RG> \
--workspace-name <LAW> \
--retention-time 730
# テーブル単位(例:SigninLogs と AuditLogs)
az monitor log-analytics workspace table update \
--resource-group <RG> --workspace-name <LAW> \
--name SigninLogs --retention-time 730 --total-retention-time 2555
az monitor log-analytics workspace table update \
--resource-group <RG> --workspace-name <LAW> \
--name AuditLogs --retention-time 730 --total-retention-time 2555
2-2) Bicep(テーブル単位の IaC 化)
param workspaceRg string
param workspaceName string
resource law 'Microsoft.OperationalInsights/workspaces@2025-02-01' existing = {
name: workspaceName
scope: resourceGroup(workspaceRg)
}
resource signin 'Microsoft.OperationalInsights/workspaces/tables@2025-02-01' = {
name: 'SigninLogs'
parent: law
properties: {
plan: 'Analytics'
retentionInDays: 730
totalRetentionInDays: 2555
}
}
resource audit 'Microsoft.OperationalInsights/workspaces/tables@2025-02-01' = {
name: 'AuditLogs'
parent: law
properties: {
plan: 'Analytics'
retentionInDays: 730
totalRetentionInDays: 2555
}
}
3) LAW データエクスポート → Storage(Bicep)
目的:SigninLogs/AuditLogs 等を 到着即エクスポート して長期保存(WORM と組み合わせ)。
param workspaceRg string
param workspaceName string
param exportName string = 'export-aad-to-storage'
@description('Export destination storage account resourceId')
param storageAccountId string
resource law 'Microsoft.OperationalInsights/workspaces@2025-02-01' existing = {
name: workspaceName
scope: resourceGroup(workspaceRg)
}
resource exportRule 'Microsoft.OperationalInsights/workspaces/dataExports@2025-02-01' = {
name: exportName
parent: law
properties: {
enable: true
destination: {
resourceId: storageAccountId
}
tableNames: [
'SigninLogs'
'AuditLogs'
]
}
}
4) Storage:WORM(イミュータブル)/リーガルホールド(CLI)
# 時間ベース保持(例:365日)
az storage container immutability-policy create \
--account-name <STG> --container-name <CONTAINER> --period 365
# リーガルホールド(タグ例:audit, evidencelog)
az storage container legal-hold set \
--account-name <STG> --container-name <CONTAINER> \
--tags audit evidencelog
5) Key Vault:Purge Protection 有効化(Bicep/CLI)
5-1) 新規作成(Bicep)
param location string
param kvName string
param tenantId string
resource kv 'Microsoft.KeyVault/vaults@2024-12-01-preview' = {
name: kvName
location: location
properties: {
tenantId: tenantId
enableRbacAuthorization: true
enablePurgeProtection: true
sku: {
family: 'A'
name: 'standard'
}
networkAcls: {
defaultAction: 'Deny'
bypass: 'AzureServices'
ipRules: []
virtualNetworkRules: []
}
publicNetworkAccess: 'Disabled' // 必要に応じて 'Enabled'
}
}
5-2) 既存 Vault の Purge 有効化(CLI)
az keyvault update --name <KV_NAME> --resource-group <RG> --enable-purge-protection true
6) Private Endpoint(Storage/Blob)+ Private DNS(Bicep 一括)
ハブ&スポーク/オンプレ連携を想定し、DNS ゾーンリンク+PE 連携まで。
param location string
param vnetName string
param subnetName string = 'snet-private-endpoints'
param stgName string
param rgName string
// 既存ストレージ
resource stg 'Microsoft.Storage/storageAccounts@2023-05-01' existing = {
name: stgName
}
// VNet & Subnet(既存なら existing に変更)
resource vnet 'Microsoft.Network/virtualNetworks@2023-09-01' = {
name: vnetName
location: location
properties: {
addressSpace: { addressPrefixes: [ '10.20.0.0/16' ] }
subnets: [
{
name: subnetName
properties: {
addressPrefix: '10.20.1.0/24'
privateEndpointNetworkPolicies: 'Disabled'
}
}
]
}
}
resource pdns 'Microsoft.Network/privateDnsZones@2020-06-01' = {
name: 'privatelink.blob.core.windows.net'
location: 'global'
}
resource link 'Microsoft.Network/privateDnsZones/virtualNetworkLinks@2020-06-01' = {
name: 'vnet-link'
parent: pdns
properties: {
registrationEnabled: false
virtualNetwork: {
id: vnet.id
}
}
}
resource pe 'Microsoft.Network/privateEndpoints@2023-09-01' = {
name: 'pe-stg-blob'
location: location
properties: {
subnet: { id: vnet.properties.subnets[0].id }
privateLinkServiceConnections: [
{
name: 'pe-stg-blob-conn'
properties: {
privateLinkServiceId: stg.id
groupIds: [ 'blob' ]
}
}
]
}
}
// PE と Private DNS Zone の関連付け
resource pdzg 'Microsoft.Network/privateEndpoints/privateDnsZoneGroups@2022-09-01' = {
name: 'pdz-group'
parent: pe
properties: {
privateDnsZoneConfigs: [
{
name: 'blob-zone'
properties: {
privateDnsZoneId: pdns.id
}
}
]
}
}
7) サブスクリプション Activity Log → LAW(Bicep:サブスクリプションスコープ)
targetScope = 'subscription'
@description('Diagnostic setting name')
param diagName string = 'activitylog-to-law'
@description('Destination LAW resourceId')
param lawResourceId string
resource subDiag 'Microsoft.Insights/diagnosticSettings@2021-05-01-preview' = {
name: diagName
properties: {
workspaceId: lawResourceId
logs: [
{ category: 'Administrative', enabled: true }
{ category: 'Security', enabled: true }
{ category: 'ServiceHealth', enabled: true }
{ category: 'Alert', enabled: true }
{ category: 'Recommendation', enabled: true }
{ category: 'Policy', enabled: true }
{ category: 'Autoscale', enabled: true }
{ category: 'ResourceHealth', enabled: true }
]
}
}
デプロイ(例)
az deployment sub create \
--name actlog-diag-$(date +%Y%m%d%H%M) \
--template-file ./activitylog-to-law.bicep \
--location <SUB-LOCATION> \
--parameters lawResourceId="/subscriptions/<SUBID>/resourceGroups/<RG>/providers/Microsoft.OperationalInsights/workspaces/<LAW>"
8) ポリシー(ISO/規制ビュー)割当(CLI)
例:表示名に “ISO 27001” を含む イニシアチブ を自動検索→割当。
# 目的のイニシアチブ定義IDを検索(部分一致)
DEF_ID=$(az policy set-definition list \
--query "[?contains(displayName, 'ISO 27001')].{id:id}[0].id" -o tsv)
# 割り当て(サブスクリプション)。必要なら --enforcement-mode Default を指定
az policy assignment create \
--name iso-initiative-assignment \
--display-name "ISO 27001 Initiative" \
--policy-set-definition $DEF_ID \
--assign-identity \
--location <SUB-LOCATION> \
--params '{}'
9) デプロイ・チートシート
# Resource Group スコープ
az deployment group create -g <RG> -f ./xxx.bicep -p <PARAMS>
# Subscription スコープ
az deployment sub create -l <LOCATION> -f ./xxx.bicep -p <PARAMS>
# Tenant スコープ(Entra 診断)
az deployment tenant create -f ./entra-diagnostics.bicep -p <PARAMS>
10) よくある詰まりポイント(実運用メモ)
Entra 診断のカテゴリはテナントの機能有無で差があるため、存在しないカテゴリは削除して適用。
LAW の保持は「分析保持(~730日)+総保持(~4383日)」の二段で設計。監査・フォレンジックは エクスポート+WORM と併用が堅実。
Private DNS はゾーンリンクを忘れやすい。切替演習(nslookup) の記録を監査に出せる形で残す。
参照リンク集(一次情報・根拠)
Entra(テナント)診断設定の ARM/Bicep 仕様(microsoft.aadiam/diagnosticSettings の API とカテゴリ)
Azure Resource 診断設定(拡張リソース):Microsoft.Insights/diagnosticSettings のスキーマとスコープ指定(サブスクリプション Activity Log 等)
Log Analytics:テーブル単位の保持(workspaces/tables、retention/totalRetention と上限)【+1】
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/data-retention-configure
https://learn.microsoft.com/en-us/azure/templates/microsoft.operationalinsights/workspaces/tables
Log Analytics:データエクスポート(workspaces/dataExports) のプロパティ(destination.resourceId/tableNames/enable)【+1】
https://learn.microsoft.com/en-us/azure/azure-monitor/logs/logs-data-export
https://learn.microsoft.com/en-us/azure/templates/microsoft.operationalinsights/workspaces/dataexports
Storage:イミュータブル(WORM)/リーガルホールド の設定方法(コンテナ/バージョンスコープ)【+1】
https://learn.microsoft.com/en-us/azure/storage/blobs/immutable-policy-configure-container-scope
https://learn.microsoft.com/en-us/azure/storage/blobs/immutable-policy-configure-version-scope
Private Endpoint × DNS:統合シナリオと DNS ゾーン値(privatelink.blob.core.windows.net 等)【+1】
https://learn.microsoft.com/en-us/azure/private-link/private-endpoint-dns-integration
https://learn.microsoft.com/en-us/azure/private-link/private-endpoint-dns
Key Vault:Soft-Delete/Purge Protection の設計・有効化(ARM/Bicep/CLI)【+2 +2】
https://learn.microsoft.com/en-us/azure/key-vault/general/key-vault-recovery
https://learn.microsoft.com/en-us/azure/key-vault/general/soft-delete-overview
https://learn.microsoft.com/en-us/azure/templates/microsoft.keyvault/vaults
https://learn.microsoft.com/en-us/cli/azure/keyvault?view=azure-cli-latest#az-keyvault-update





コメント