
リソースマネージャーテンプレートを利用したストレージアカウントの作成(Azure CLI)
最低限のパラメーターでリソースマネージャーテンプレートを作成し、ストレージアカウントを作成したものが以下の記事です。
https://azuretech.jp/?p=421
最低限過ぎたため、実際に運用に耐えそうな設定を追加し、さらに、テンプレートをAzure Portalかてらではなく、コマンドから作成したいと思います。
番号 | 機能 | 説明 |
1 | accessTier | ストレージのアクセス層を Cool に設定 |
2 | minimumTlsVersion | TLS 1.2 を強制 |
3 | allowBlobPublicAccess | BLOB の匿名アクセスを許可 |
4 | deleteRetentionPolicy | BLOB の論理削除を 7 日間保持 |
5 | containerDeleteRetentionPolicy | コンテナーの論理削除を 7 日間保持 |
6 | restorePolicy | 論理削除された項目の完全削除を有効化 |
7 | isVersioningEnabled | BLOB のバージョン管理を有効化 |
8 | changeFeed | BLOB の変更フィードを有効化し、7日間保持 |
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageName": {
"type": "string",
"minLength": 3,
"maxLength": 24,
"metadata": {
"description": "The name of the Azure storage resource"
}
},
"storageSKU": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS",
"Standard_ZRS",
"Premium_LRS",
"Premium_ZRS",
"Standard_GZRS",
"Standard_RAGZRS"
]
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-05-01",
"name": "[parameters('storageName')]",
"location": "[resourceGroup().location]",
"kind": "StorageV2",
"sku": {
"name": "[parameters('storageSKU')]"
},
"properties": {
"accessTier": "Cool", // ① アクセス層
"minimumTlsVersion": "TLS1_2", // ② TLSバージョン
"allowBlobPublicAccess": true, // ③ BLOBの匿名アクセス
"deleteRetentionPolicy": { // ④ BLOBの論理削除
"enabled": true,
"days": 7
},
"containerDeleteRetentionPolicy": { // ⑤ コンテナーの論理削除
"enabled": true,
"days": 7
},
"restorePolicy": { // ⑥ 完全削除の有効化
"enabled": true
},
"isVersioningEnabled": true, // ⑦ BLOBのバージョン管理
"changeFeed": { // ⑧ BLOBの変更フィード
"enabled": true,
"retentionInDays": 7
}
},
"tags": {
"displayName": "[parameters('storageName')]"
}
}
],
"outputs": {
"storageEndpoint": {
"type": "object",
"value": "[reference(parameters('storageName')).primaryEndpoints]"
}
}
}
デプロイします。
az deployment group create \
--resource-group リソースグループ名 \
--template-file リソースマネージャーテンプレート名 \
--parameters storageName=タグ名 storageSKU=Standard_LRS
無事に作成できました。

ただし、データ保護などは設定後でないと設定できないようです。

であれば、一旦、不要なコードを削除すると以下のようなテンプレートとなります。
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"storageName": {
"type": "string",
"minLength": 3,
"maxLength": 24,
"metadata": {
"description": "The name of the Azure storage resource"
}
},
"storageSKU": {
"type": "string",
"defaultValue": "Standard_LRS",
"allowedValues": [
"Standard_LRS",
"Standard_GRS",
"Standard_RAGRS",
"Standard_ZRS",
"Premium_LRS",
"Premium_ZRS",
"Standard_GZRS",
"Standard_RAGZRS"
]
}
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2023-05-01",
"name": "[parameters('storageName')]",
"location": "[resourceGroup().location]",
"kind": "StorageV2",
"sku": {
"name": "[parameters('storageSKU')]"
},
"properties": {
"accessTier": "Cool", // ① アクセス層
"minimumTlsVersion": "TLS1_2", // ② TLSバージョン
"allowBlobPublicAccess": true, // ③ BLOBの匿名アクセス
},
"tags": {
"displayName": "[parameters('storageName')]"
}
}
],
"outputs": {
"storageEndpoint": {
"type": "object",
"value": "[reference(parameters('storageName')).primaryEndpoints]"
}
}
}
上記のテンプレートで、ストレージアカウントを作成し、Azure CLIで設定変更を実行します。
# Azure CLIにて実行
# ストレージアカウント名とリソースグループ名を指定
STORAGE_ACCOUNT_NAME=ストレージアカウント名
RESOURCE_GROUP_NAME=リソースグループ名
# 4. BLOBの論理的な削除(Soft Delete)を7日間有効にする
az storage account blob-service-properties update \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--delete-retention-days 7 \
--enable-delete-retention true
# 5. コンテナーの論理的な削除を7日間有効にする
az storage account blob-service-properties update \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--container-delete-retention-days 7 \
--enable-container-delete-retention true
# 7. BLOB のバージョン管理を有効にする
az storage account blob-service-properties update \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--enable-versioning true
# 8. BLOB の変更フィードを有効にし、保持期間を7日間に設定
az storage account blob-service-properties update \
--account-name $STORAGE_ACCOUNT_NAME \
--resource-group $RESOURCE_GROUP_NAME \
--enable-change-feed true \
--change-feed-retention-days 7
下記の設定はまだ、Azure CLIで用意されていないようなので、Azure PowerShellにて実行します。
# 6. 論理的に削除された項目の完全な削除を有効にする(Restore Policy)
Enable-AzStorageBlobDeleteRetentionPolicy `
-ResourceGroupName "リソースグループ名" `
-StorageAccountName "ストレージアカウント名" `
-RetentionDays 7 `
-AllowPermanentDelete `
-PassThru
設定変更できました。テストで用意しておけば、本番環境などで作成するときは迷いもなく同じ設定を再現できるのでカスタムデプロイはとても便利です。
