Desplegar Azure Static Web Apps con Terraform

Si bien es cierto que Azure Static Web Apps está soportado por el provider azurerm de Terraform, la configuración a día de hoy es demasiado básica como para automatizar un entorno con este servicio. En el artículo de hoy te comparto cómo he desplegado una Azure Static Web App con un repositorio de GitHub asociado y alguna App Setting configurada.

Azure Static Web Apps a través del provider de azurerm

A día de hoy, esta es toda la configuración que podemos especificar a través de azurerm:

Configuración de Azure Static Web App con azurerm en Terraform

Si alguna vez has trabajado con Azure Static Web Apps, para que esta sea útil es necesario al menos poder especificarle cuál es el repositorio que va a utilizar para gestionar el despliegue. Esta configuración no está disponible a través del proveedor.

Usar AzApi para configurar Azure Static Web Apps en Terraform

Por si no lo sabías, hay un proveedor adicional llamado AzAPI que nos permite integrarlo dentro de nuestra plantilla de Terraform para todas aquellas funcionalidades que no están implementadas en el provider oficial. En el caso de Azure Static Web App he usado este provider para asociar mi servicio a un repo de GitHub y para añadir algunas settings de la siguiente manera:

# Provider
provider "azurerm" {
  features {}
}
terraform {
  required_providers {
    azapi = {
      source = "azure/azapi"
    }
    azurerm = {
      source = "hashicorp/azurerm"
    }
  }
}
variable "location" {
  type    = string
  default = "westeurope"
}
variable "resource_group" {
  type    = string
  default = "swa-terraform"
}

resource "azurerm_resource_group" "rg" {
  name     = var.resource_group
  location = var.location
}

variable "github_repo" {
  type    = string
  default = "<YOUR_GITHUB_REPO>"
}
variable "github_token" {
  type    = string
  default = "<YOUR_GITHUB_TOKEN>"
}
resource "azurerm_static_site" "swa" {
  name                = "swa"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location
  sku_tier            = "Standard"
  sku_size            = "Standard"
}
resource "azapi_update_resource" "swa_github" {
  type        = "Microsoft.Web/staticSites@2022-09-01"
  resource_id = azurerm_static_site.swa.id

  body = jsonencode({
    properties = {
      provider        = "Github",
      repositoryUrl   = var.github_repo
      branch          = "main"
      repositoryToken = var.github_token
      buildProperties = {
        apiLocation     = "api"
        appLocation     = "static-web"
        apiBuildCommand = ""
      }
    }
  })
  depends_on = [azurerm_static_site.swa]
}
resource "azapi_resource_action" "swa_app_settings" {
  type        = "Microsoft.Web/staticSites/config@2022-09-01"
  resource_id = "${azurerm_static_site.swa.id}/config/appsettings"
  method      = "PUT"

  body = jsonencode({
    properties = {
      "WebPubSubConnectionString" = "MyConnectionString"
      "WebPubSubHub"              = "MyHub"
    }
  })
  depends_on = [azurerm_static_site.swa]
}

output "swa_endpoint" {
  value = "https://${azurerm_static_site.swa.default_host_name}"
}

¡Saludos!