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?

AzureFront Doorでindex.htmlを表示させたい

0
Posted at

AzureFront Doorでindex.htmlを表示させたい

terraformのコード

main.tf

terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.100"   # 最新に合わせて適宜更新
    }
  }
}

provider "azurerm" {
  features {}
}

# --------------------------------------------------
# 変数(適宜 .tfvars や変数で管理推奨)
# --------------------------------------------------
variable "location" { default = "japaneast" }
variable "prefix"   { default = "myfrontdoor" }

# --------------------------------------------------
# Resource Group
# --------------------------------------------------
resource "azurerm_resource_group" "rg" {
  name     = "${var.prefix}-rg"
  location = var.location
}

# --------------------------------------------------
# Storage Account + Static Website
# --------------------------------------------------
resource "azurerm_storage_account" "static" {
  name                     = lower(replace("${var.prefix}static", "-", ""))
  resource_group_name      = azurerm_resource_group.rg.name
  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"          # 必要に応じて GRS / RAGRS
  account_kind             = "StorageV2"
  min_tls_version          = "TLS1_2"
  https_traffic_only_enabled = true

  static_website {
    index_document     = "index.html"
    error_404_document = "404.html"     # 任意(SPAなら index.html でもOK)
  }
}

# $webコンテナに index.html をアップロードする例(ローカルから)
# → 本番ではCI/CDやazcopyを使うのが普通です
resource "azurerm_storage_blob" "index" {
  name                   = "index.html"
  storage_account_name   = azurerm_storage_account.static.name
  storage_container_name = "$web"
  type                   = "Block"
  content_type           = "text/html"
  source_content         = <<EOF
<!DOCTYPE html>
<html>
<head><title>Hello Azure</title></head>
<body>
  <h1>こんにちは! Azure Front Door + Static Website です</h1>
  <p>Terraformで作成されました (${timestamp()})</p>
</body>
</html>
EOF
}

# --------------------------------------------------
# Azure Front Door (Standard)
# --------------------------------------------------
resource "azurerm_cdn_frontdoor_profile" "fd" {
  name                = "${var.prefix}-fd"
  resource_group_name = azurerm_resource_group.rg.name
  sku_name            = "Standard_AzureFrontDoor"
}

resource "azurerm_cdn_frontdoor_endpoint" "endpoint" {
  name                     = "web"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.fd.id
}

resource "azurerm_cdn_frontdoor_origin_group" "origin_group" {
  name                     = "storage-origin-group"
  cdn_frontdoor_profile_id = azurerm_cdn_frontdoor_profile.fd.id

  health_probe {
    path                    = "/"
    request_type            = "HEAD"
    protocol                = "Https"
    interval_in_seconds     = 100
  }

  load_balancing {
    sample_size                     = 4
    successful_samples_required     = 3
  }
}

resource "azurerm_cdn_frontdoor_origin" "storage_origin" {
  name                          = "storage-origin"
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.origin_group.id
  enabled                       = true

  certificate_name_check_enabled = false   # Storageはカスタムドメインでない限りfalse

  host_name          = azurerm_storage_account.static.primary_web_host
  http_port          = 80
  https_port         = 443
  origin_host_header = azurerm_storage_account.static.primary_web_host
}

resource "azurerm_cdn_frontdoor_route" "route" {
  name                          = "default-route"
  cdn_frontdoor_endpoint_id     = azurerm_cdn_frontdoor_endpoint.endpoint.id
  cdn_frontdoor_origin_group_id = azurerm_cdn_frontdoor_origin_group.origin_group.id
  cdn_frontdoor_origin_ids      = [azurerm_cdn_frontdoor_origin.storage_origin.id]

  supported_protocols    = ["Http", "Https"]
  patterns_to_match      = ["/*"]
  forwarding_protocol    = "MatchRequest"
  link_to_default_domain = true

  # SPA対応(任意だが強く推奨)
  # 404をindex.htmlにリダイレクトしたい場合 → Rule Setで設定
  # ここでは簡易的に forwarding だけ
}

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?