★ 初級

05. Storage Account

Azure のストレージは 1 つの「Storage Account」 の中に Blob / File / Queue / Table の 4 サービスが同居する独特な構造。AWS の S3 と似ているけど名前の付け方やネットワーク制御が違います。

4 つのストレージサービス

サービスAWS 対応用途
BlobS3オブジェクトストレージ。画像・ログ・バックアップ
FilesEFSSMB/NFS ファイル共有
QueueSQS軽量メッセージキュー
TableDynamoDB (簡易)NoSQL Key-Value

1 つの Storage Account を作ると 4 つともエンドポイントが用意されます。実際は使う 1〜2 種類しか触らないことが多い。

Storage Account の基本

resource "azurerm_storage_account" "data" {
  # 名前は全世界一意、3-24 文字、英数小文字のみ(ハイフン不可)
  name                = "samyappprdjpe001"
  resource_group_name = azurerm_resource_group.main.name
  location            = azurerm_resource_group.main.location

  account_tier             = "Standard"   # Standard or Premium
  account_replication_type = "GRS"        # LRS / ZRS / GRS / GZRS / RAGRS
  account_kind             = "StorageV2"  # v2 が標準

  min_tls_version          = "TLS1_2"
  https_traffic_only_enabled = true
  allow_nested_items_to_be_public = false

  tags = local.common_tags
}
命名の制約 Storage Account 名は ハイフン不可・英数小文字のみ・3-24 文字。他リソースの命名規約とは別ルール。一意性が必要なので suffix にアカウント ID 等を入れる。

パフォーマンス階層とレプリケーション

account_tier用途
StandardHDD ベース。汎用
PremiumSSD ベース。低レイテンシ。BlockBlobStorage 等の専用 kind と組み合わせ
account_replication_type耐久性料金
LRS1 リージョン内 3 コピー最安
ZRS1 リージョン 3 AZ+小
GRS2 リージョン(プライマリ 3 + セカンダリ 3)+中
GZRSZRS + 別リージョン最高耐久

Blob Container

resource "azurerm_storage_container" "images" {
  name                  = "images"
  storage_account_id    = azurerm_storage_account.data.id
  container_access_type = "private"   # blob / container / private
}

# 個別ファイルをアップロード(小規模なら)
resource "azurerm_storage_blob" "logo" {
  name                   = "logo.png"
  storage_account_name   = azurerm_storage_account.data.name
  storage_container_name = azurerm_storage_container.images.name
  type                   = "Block"
  source                 = "${path.module}/assets/logo.png"
}

本番向けの推奨設定

resource "azurerm_storage_account" "data" {
  # ... 基本項目 ...

  # 暗号化
  infrastructure_encryption_enabled = true   # 二重暗号化

  # ライフサイクル:Blob versioning とソフト削除
  blob_properties {
    versioning_enabled       = true
    change_feed_enabled      = true
    last_access_time_enabled = true

    delete_retention_policy {
      days = 30   # 削除した Blob を 30 日保持
    }
    container_delete_retention_policy {
      days = 30
    }
  }

  # ネットワーク制限(後述)
  public_network_access_enabled = false

  # 共有キー無効化(Entra ID 認証のみに)
  shared_access_key_enabled = false
}

静的 Web サイトホスティング

resource "azurerm_storage_account" "site" {
  # ... 基本項目 ...
  static_website {
    index_document     = "index.html"
    error_404_document = "404.html"
  }
}

# 配信エンドポイント: storage_account.primary_web_endpoint
# 例: https://samyapp.z23.web.core.windows.net/
output "static_site_url" {
  value = azurerm_storage_account.site.primary_web_endpoint
}

独自ドメイン + HTTPS で公開するなら Azure Front DoorCDN を前段に。10 章 参照。

ネットワークルール

resource "azurerm_storage_account_network_rules" "data" {
  storage_account_id = azurerm_storage_account.data.id

  default_action = "Deny"
  bypass         = ["AzureServices"]

  ip_rules = ["203.0.113.0/24"]   # オフィス IP

  virtual_network_subnet_ids = [
    azurerm_subnet.private.id,
  ]
}

これで「VNet 内 + オフィス IP からのみアクセス可」になります。完全にプライベートにするなら Private Endpoint