본문 바로가기

개발하자

Azure Static Web App - Terraform을 사용한 애플리케이션 설정

반응형

Azure Static Web App - Terraform을 사용한 애플리케이션 설정

테라폼을 사용하여 아래와 같이 애저 스태틱 웹 앱을 만들었습니다. 그러나 에는 리소스에 대한 응용 프로그램 설정을 설정하는 방법이 나와 있지 않습니다. 일반 애저 기능 앱의 경우 매개 변수를 볼 수 있습니다. 그러나 테라폼을 사용하여 생성된 푸른색 정적 웹 앱의 앱 설정을 설정하는 방법은 무엇인가요?

resource "azurerm_static_site" "example" {
  name                = "example"
  resource_group_name = "example"
  location            = "West Europe"
}

나는 마이크로소프트에서처럼 파라미터를 설정하고 애저 스태틱 웹 앱에 대한 ID 제공자를 구성하는 것을 좋아한다.




나는 당신이 본 테라폼 문서(링크: )를 보는 것을 믿는다. 정적 웹 앱은 매우 기본적이고 앱 설정 인수를 받지 않는다.

또한 애저 블로그에 따르면: 단순히 정적 사이트를 호스팅하는 것 이상의 작업을 수행하고 싶은 웹 앱이 있다면 전체 기능 웹 앱을 사용해야 합니다. 아마도 하시 주식회사는 이것이 프론트 엔드 페이지를 호스팅하기 위한 기본적인 웹 앱이라는 것을 의미하는 것으로 받아들였을 것이다? 확실하지 않아...

이 이론에 따르면 테라폼에서 앱 설정을 수행하려면 외관에 따라 애저 웹 앱을 만들어야 합니다:

다음은 앱 설정으로 웹 앱을 코딩하는 방법에 대한 지침이 필요한 경우의 예입니다. 당신은 아마 알고 있겠지만 참고로 여기 있다.

resource "azurerm_app_service_plan" "websiteappserviceplan" {
  name                = "appserviceplan_mysite"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name

  sku {
    tier = "Basic"
    size = "B1"
  }
}

resource "azurerm_app_service" "web_app" { 
  name                = var.webapp_name
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  app_service_plan_id = azurerm_app_service_plan.websiteappserviceplan.id


  app_settings = {
    "KEY_VAULT_URL"             = azurerm_key_vault.myvault.vault_uri
    "SUPPORTS_SAFEGUARDING"     = "1"
    "SUPPORTS_TECHNICIAN"       = "1"
  }

  client_affinity_enabled = true

  site_config {
    always_on                 = true
    min_tls_version           = "1.2"
    dotnet_framework_version  = "v5.0"
    app_command_line          = "dotnet EventManagement.Web.dll"
    ftps_state                = "Disabled"
    use_32_bit_worker_process = true
    http2_enabled             = true
    websockets_enabled        = true
  }

  logs {
    detailed_error_messages_enabled = true
    failed_request_tracing_enabled  = true
    application_logs {
      azure_blob_storage {
        level             = "Information"
        sas_url           = format("https://${azurerm_storage_account.website_log_storage.name}.blob.core.windows.net/${azurerm_storage_container.website_logs_container.name}%s", data.azurerm_storage_account_blob_container_sas.website_logs_container_sas.sas)
        retention_in_days = 365
      }
    }

    http_logs {
      azure_blob_storage {
        sas_url           = format("https://${azurerm_storage_account.website_log_storage.name}.blob.core.windows.net/${azurerm_storage_container.website_logs_container.name}%s", data.azurerm_storage_account_blob_container_sas.website_logs_container_sas.sas)
        retention_in_days = 365
      }
    }

  }

  connection_string {
    name  = "StorageAccount"
    type  = "Custom"
    value = azurerm_storage_account.website_log_storage.primary_connection_string
  }

  identity {
    type = "SystemAssigned"
  }
}




애저 스태틱 웹 앱의 애플리케이션 설정은 현재(2021년 11월 24일) 열려 있습니다. 기능이 테라폼에 추가됨에 따라 이 답변을 업데이트하겠습니다.

AzAPI Terform 제공업체 []를 사용하면 AzureRM 제공업체에서 지원하지 않는 리소스를 관리할 수 있습니다. 정적 웹 앱에 대한 애플리케이션 설정 및 Azure 기능 연결에 대한 아래 코드 샘플.

# For AzAPI provider
terraform {
  required_providers {
    azapi = {
      source  = "azure/azapi"
      version = "~> 1.0"
    }
  }
}

# For static web app 
resource azurerm_static_site swa {
  name                = "myswa"
  resource_group_name = "myrg"
  location            = "westeurope"

  # ignore below if you do not need to link functions
  sku_tier            = "Standard"
  sku_size            = "Standard"
}

# For application settings. You may change to azapi_resource once Github issue 
# https://github.com/Azure/terraform-provider-azapi/issues/256 is closed.
resource azapi_resource_action appsetting {
  type = "Microsoft.Web/staticSites/config@2022-03-01"
  resource_id = "${azurerm_static_site.swa.id}/config/appsettings"
  method = "PUT"

  body = jsonencode({
    properties = {
        "mykey"="myvalue"
    }
  })
}

# For linking Azure function. Ignore if not needed
resource azapi_resource linktofunction {
  type = "Microsoft.Web/staticSites/userProvidedFunctionApps@2022-03-01"
  name = "swalinktofunction"
  parent_id = azurerm_static_site.swa.id
  body = jsonencode({
    properties = {
      functionAppRegion = "westeurope"
      # below swafunction created else where
      functionAppResourceId = azurerm_windows_function_app.swafunction.id
    }
  })
}

응용 프로그램 설정의 경우 [] 이슈에 대해 작동하지 않기 때문에 사용할 수 밖에 없었습니다. 이것은 중요한 결과를 가져온다: . 이상적인 것은 아니지만 현재로서는 테라폼으로 얻을 수 있는 최선의 방법이다.




AzureRM + AzApi 구성을 팔로우한 것은 운이 좋았다:

resource "azurerm_static_site" "example_app" {
  name                = "example-app"
  resource_group_name = "example-rg"
  location            = "West Europe"
}

resource "azapi_update_resource" "example_app_settings" {
  type = "Microsoft.Web/staticSites@2022-03-01"
  resource_id = azurerm_static_site.example_app.id
  body = jsonencode({
    properties = {
      customDomains = [
        "www.example.com"
      ]
      repositoryUrl = "https://github.com/example"
      branch = "example"
    }
  })
}

사용 가능한 모든 속성과 형식을 보려면 정적 웹 앱으로 이동하여 개요에서 오른쪽에 있는 JSON 보기를 엽니다.

서로 다른 공급자를 사용하여 생성하고 업데이트하는 대신 정적 웹 앱을 AzAPI로만 생성하고 구성하는 것이 더 나을 수 있습니다:




이 효과와 단순성을 위해 테라폼 코드를 깨끗하게 유지하기 위해 다음을 사용하는 것을 선호합니다:

az staticwebapp appsettings set --name my_webapp_name --setting-names "SETTING_NAME=setting_value" --subscription "$(my_subscription_id)"

이것은 또한 자동화될 수 있으며, 다른 IaC 도구와 마찬가지로 idemporent이다. 그래서 나는 내 애저 데브옵스 파이프라인에서 테라폼을 실행한 후 이러한 설정을 적용하고 있다.


반응형