0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ストレージアカウントのシステム割当てマネージドIDを有効化する方法

0
Last updated at Posted at 2025-09-29

はじめに

Azure Storage Accountでシステム割り当てマネージドID(System-assigned Managed Identity)を有効化する方法を3つのアプローチで解説します。マネージドIDを使用することで、Key Vaultへのアクセスやカスタマーマネージドキーの管理を安全に行うことができます。

1. Azure ポータルからの設定方法

1.1. 手順

  1. Azure ポータルにサインイン
  2. 対象のストレージアカウントを選択
  3. 暗号化メニューにアクセス
    • 左側メニューから「セキュリティとネットワーク」→「暗号化」を選択
  4. マネージドIDの設定
    • 「カスタマー マネージド キー」を選択
    • 適当なキーを選択(一時的でも可)
    • 「ID の種類」で「システム割り当て」を選択
    • 保存」をクリック

初回実行時の想定エラー
Key Vault へのアクセス権が無いとのエラーが表示される場合があります。
これは正常な動作で、後からKey Vaultのアクセスポリシーを設定すれば解決します。

1.2. 公式ドキュメント

参考:システム割り当てマネージド ID を使用してアクセスを承認する

Azure portal でシステム割り当てマネージド ID を使用してカスタマー マネージド キーを構成すると、システム割り当てマネージド ID は内部でストレージ アカウントに割り当てられます。

2. Azure CLI での設定方法

Azure CLIでもデプロイが可能です。おそらく、この方法が一番簡単で便利です。

2.1. 基本コマンド

# システム割り当てマネージドIDを有効化
az storage account update \
  --name "your-storage-account" \
  --resource-group "your-resource-group" \
  --assign-identity

2.2. 詳細設定例

# 変数設定
STORAGE_ACCOUNT_NAME="mystorageaccount"
RESOURCE_GROUP="myresourcegroup"
KEY_VAULT_NAME="mykeyvault"
KEY_NAME="mykey"

# 1. ストレージアカウントでマネージドIDを有効化
az storage account update \
  --name $STORAGE_ACCOUNT_NAME \
  --resource-group $RESOURCE_GROUP \
  --assign-identity

# 2. マネージドIDのプリンシパルIDを取得
PRINCIPAL_ID=$(az storage account show \
  --name $STORAGE_ACCOUNT_NAME \
  --resource-group $RESOURCE_GROUP \
  --query identity.principalId \
  --output tsv)

echo "Managed Identity Principal ID: $PRINCIPAL_ID"

# 3. Key Vaultへのアクセス権を付与
az keyvault set-policy \
  --name $KEY_VAULT_NAME \
  --object-id $PRINCIPAL_ID \
  --key-permissions get wrapKey unwrapKey

# 4. カスタマーマネージドキーを設定
az storage account update \
  --name $STORAGE_ACCOUNT_NAME \
  --resource-group $RESOURCE_GROUP \
  --encryption-key-name $KEY_NAME \
  --encryption-key-vault $KEY_VAULT_NAME \
  --encryption-key-source Microsoft.Keyvault

2.3. 確認コマンド

# マネージドIDの状態確認
az storage account show \
  --name $STORAGE_ACCOUNT_NAME \
  --resource-group $RESOURCE_GROUP \
  --query identity \
  --output table

2.4. 公式ドキュメント

参考:システム割り当てマネージド ID を使用してアクセスを承認する(Azure CLI)

3. Bicep での設定方法

3.1. 基本テンプレート

@description('Storage Account name')
param storageAccountName string

@description('Location')
param location string = resourceGroup().location

@description('Key Vault name')
param keyVaultName string

@description('Key name for customer-managed key')
param keyName string

// Storage Account with System-assigned Managed Identity
resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: storageAccountName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  identity: {
    type: 'SystemAssigned'  // ← システム割り当てマネージドIDを有効化
  }
  properties: {
    minimumTlsVersion: 'TLS1_2'
    allowBlobPublicAccess: false
    supportsHttpsTrafficOnly: true
    encryption: {
      services: {
        blob: {
          enabled: true
        }
        file: {
          enabled: true
        }
      }
      keySource: 'Microsoft.Keyvault'
      keyvaultproperties: {
        keyname: keyName
        keyvaulturi: 'https://${keyVaultName}.vault.azure.net/'
      }
    }
  }
}

// Key Vault (既存の場合)
resource keyVault 'Microsoft.KeyVault/vaults@2023-02-01' existing = {
  name: keyVaultName
}

// Key Vault アクセスポリシー(ストレージアカウント用)
resource keyVaultAccessPolicy 'Microsoft.KeyVault/vaults/accessPolicies@2023-02-01' = {
  name: 'add'
  parent: keyVault
  properties: {
    accessPolicies: [
      {
        tenantId: storageAccount.identity.tenantId
        objectId: storageAccount.identity.principalId
        permissions: {
          keys: [
            'get'
            'wrapKey'
            'unwrapKey'
          ]
        }
      }
    ]
  }
}

// Output
output storageAccountId string = storageAccount.id
output managedIdentityPrincipalId string = storageAccount.identity.principalId
output managedIdentityTenantId string = storageAccount.identity.tenantId

3.2. 既存ストレージアカウントの更新

// 既存のストレージアカウントにマネージドIDを追加
resource existingStorageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' existing = {
  name: 'existing-storage-account'
}

// マネージドIDを有効化する更新
resource updateStorageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: existingStorageAccount.name
  location: existingStorageAccount.location
  sku: existingStorageAccount.sku
  kind: existingStorageAccount.kind
  identity: {
    type: 'SystemAssigned'  // マネージドIDを追加
  }
  properties: existingStorageAccount.properties
}

3.3. デプロイコマンド

# Bicepテンプレートをデプロイ
az deployment group create \
  --resource-group "your-resource-group" \
  --template-file "storage-managed-identity.bicep" \
  --parameters \
    storageAccountName="mystorageaccount" \
    keyVaultName="mykeyvault" \
    keyName="mykey"

4. 設定後の確認方法

2025/9/29現在、Azureポータルからストレージアカウントを表示したところ、IDメニューが見当たりませんでした。この後紹介する方法で、設定確認をしてください。
image.png

4.1. Azure CLI での確認

# マネージドIDの詳細確認
az storage account show \
  --name "your-storage-account" \
  --resource-group "your-resource-group" \
  --query "{name:name, identity:identity}" \
  --output table

# プリンシパルIDのみ取得
az storage account show \
  --name "your-storage-account" \
  --resource-group "your-resource-group" \
  --query identity.principalId \
  --output tsv

4.3. Azure PowerShell での確認

# ストレージアカウントの情報取得
$storageAccount = Get-AzStorageAccount -ResourceGroupName "your-resource-group" -Name "your-storage-account"

# マネージドIDの確認
Write-Output "Managed Identity Type: $($storageAccount.Identity.Type)"
Write-Output "Principal ID: $($storageAccount.Identity.PrincipalId)"
Write-Output "Tenant ID: $($storageAccount.Identity.TenantId)"

まとめ

Azure Storage Account でシステム割り当てマネージドIDを有効化する3つの方法を紹介しました:

  1. Azure ポータル: GUI での簡単設定(初心者向け)
  2. Azure CLI: スクリプト化・自動化に適している
  3. Bicep: Infrastructure as Code での管理

各方法にはそれぞれメリットがあるため、環境や要件に応じて適切な方法を選択してください。マネージドIDを活用することで、より安全で管理しやすいストレージ暗号化を実現できます。

参考リンク

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?