🦐
【Azure】Bicepを使ってストレージアカウントを作成してみよう!
BicepというAzure特化のIaCを使って 既存のリソースグループへ Storage Account をデプロイします
ゴール:Bicepで既存RGへ安全にデプロイできるようになる
- コード(Bicep)で「同じ手順を何度でも」再現し、InductionCourse_202507_son というリソースグループに Storage Account を作成できるようにする
 - やること:Bicepファイルを1枚作って、what-if で差分を確認 → create で本番反映
 
やってみた結果
- コマンド1発で Storage Account が作成 できるようになった
 - Bicep なので、パラメータを変えるだけで再利用が簡単
 - 
what-ifが便利で、作成・変更・削除の差分が実行前に見えるため、安心して本番に適用できる(今回は検証環境での実施) 
開発環境
*Windows
- VSCode(拡張機能:Bicep 推奨)
 - Azure CLI(
azコマンド) 
フォルダ構成:
bicep-sa/
└─ main.bicep        # 本体
事前準備
- 
Azure サブスクリプションを持っている
 - 
既存のリソースグループがある:
InductionCourse_202507_son - 
Azure CLI をインストール
 - 
VSCode に Bicep 拡張を入れる
 - 
Bicep CLI は Azure CLI 経由でOK
az bicep install az bicep version 
全体の流れ
- 
main.bicepを作る - 
az login→ サブスクリプションを選ぶ - 
what-ifで差分プレビュー - 問題なければ 
createでデプロイ - 作成後に確認・後片付け(必要時)
 
Bicepのインストール(確認)
Azure CLI が入っていれば Bicep はコマンドで導入できます。
az bicep install
az bicep version
Azure CLI/アカウントの設定
ログイン
# PowerShell / Bash 共通
az login
サブスクリプション選択
# PowerShell
az account list -o table
az account set --subscription "<ID または 名前>"
# Bash (macOS/Linux)
az account list -o table
az account set --subscription "<ID または 名前>"
 main.bicep を作成(コピペOK)
既存RG(
InductionCourse_202507_son)に、Storage Account を1つ作成します。
何も指定しなくても 重複しにくい一意名 を自動生成、HTTPS/TLS1.2 強制などの基本セキュリティも既定ONされる
@description('Storage Account名。空ならRGに基づく一意名を自動生成(英小文字/数字, 3〜24文字)。')
param storageAccountName string = ''
@description('storageAccountName が空のときの接頭辞。')
@minLength(2)
@maxLength(10)
param namePrefix string = 'st'
@description('配置リージョン(既定: RGと同じ)。')
param location string = resourceGroup().location
@description('SKU(冗長化/性能)。')
@allowed([
  'Standard_LRS'
  'Standard_GRS'
  'Standard_RAGRS'
  'Standard_ZRS'
  'Standard_GZRS'
  'Standard_RAGZRS'
  'Premium_LRS'
])
param skuName string = 'Standard_LRS'
@description('アカウント種別(通常 StorageV2)。')
@allowed([
  'StorageV2'
  'FileStorage'
  'BlockBlobStorage'
])
param kind string = 'StorageV2'
@description('パブリックネットワークアクセス(Enabled/Disabled)。')
@allowed([
  'Enabled'
  'Disabled'
])
param publicNetworkAccess string = 'Enabled'
@description('共有キーアクセス許可(FalseにするとAAD中心運用)。')
param allowSharedKeyAccess bool = true
@description('Blobのパブリックアクセスを許可(アカウント側フラグ)。')
param allowBlobPublicAccess bool = false
// 空渡しなら一意名を自動生成
var finalName = (length(storageAccountName) == 0)
  ? toLower('${namePrefix}${uniqueString(resourceGroup().id)}')
  : toLower(storageAccountName)
resource stg 'Microsoft.Storage/storageAccounts@2023-01-01' = {
  name: finalName
  location: location
  sku: {
    name: skuName
  }
  kind: kind
  properties: {
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
    allowBlobPublicAccess: allowBlobPublicAccess
    publicNetworkAccess: publicNetworkAccess
    allowSharedKeyAccess: allowSharedKeyAccess
    accessTier: 'Hot'
  }
}
output storageAccountName string = stg.name
output storageAccountId string = stg.id
デプロイ前のプレビュー(what-if)
# PowerShell(行継続はバッククォート ` )
az deployment group what-if `
  -g InductionCourse_202507_son `
  -f .\main.bicep
# Bash / macOS
az deployment group what-if \
  -g InductionCourse_202507_son \
  -f ./main.bicep
期待表示(初回):Create Microsoft.Storage/storageAccounts 'stxxxxx'
本番デプロイ(create)
A) 自動生成名でそのまま
# PowerShell
az deployment group create `
  -g InductionCourse_202507_son `
  -f .\main.bicep
# Bash
az deployment group create \
  -g InductionCourse_202507_son \
  -f ./main.bicep
B) 名前を自分で決める(※英小文字/数字のみ・3〜24文字・グローバル一意)
# PowerShell
az deployment group create `
  -g InductionCourse_202507_son `
  -f .\main.bicep `
  --parameters storageAccountName=stson20250813a
# Bash
az deployment group create \
  -g InductionCourse_202507_son \
  -f ./main.bicep \
  --parameters storageAccountName=stson20250813a
PowerShell 注意点:行継続は **
**(バッククォート)。Bash の` を混ぜると「--parametersの後に式が無い」エラーになります。
作成後の確認
az storage account list -g InductionCourse_202507_son -o table
az storage account show -n <storageAccountName> -g InductionCourse_202507_son -o table
接続文字列(共有キー運用時):
az storage account show-connection-string \
  -n <storageAccountName> \
  -g InductionCourse_202507_son -o tsv
おわりに
- Bicep を使って Azure特化のIaC として、少ない手数で安全に リソースを構築できました
 - 
what-if→createの2ステップで、意図しない変更を減らしつつ 確実に反映可能 - 次の一歩として、Blob コンテナ作成、SAS/ロール割り当て、ライフサイクル管理、Private Endpoint などを組み合わせると、即戦力のストレージ基盤になります
 - 次回は作成したBicepをARMテンプレートに変換して実行できるのかやっていきます
 
トラブルシューティング(よくある詰まり)
- 
AccountNameInvalid / AlreadyTaken:命名ルール違反 or 重複。英小文字/数字のみ・3〜24文字で別名に。 - 
AuthorizationFailed:権限不足。RGに対して Contributor などの作成権限が必要。 - PowerShell で 
--parametersエラー:行継続の記号ミス(\ではなく `)。 - 
publicAccessをBlob/Containerにするとエラー:アカウント側allowBlobPublicAccess=trueが必要。 
Discussion