2

Using Terraform, I created an Azure Static Web App as below. But the documentation does not demonstrate how to set application settings for the resource. For normal Azure functions apps we can see app_settings parameter. But how to set app settings for azure static web app created using Terraform?

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

I like to set parameters AAD_CLIENT_ID and AAD_CLIENT_SECRET to configure identity provider for the azure static web app as in microsoft documention.

4 Answers 4

5

Original Answer: Application settings on Azure static web apps is an open feature request as of now (24/Nov/2021). I will update this answer as the feature is added to Terraform.

UPDATE: With AzAPI Terraform provider [2], we could manage resources not presently supported by AzureRM provider. Code sample below for application settings and Azure function linkage to static web app.

# 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
    }
  })
}

For application settings, I was forced to use azapi_resource_action since azapi_resource was not working for issue [3]. This has an important consequence: if application settings is deleted or updated via external means like Azure portal or az cli, Terraform will not know about it. This is not ideal, but that is best we could get with Terraform for now.

Sign up to request clarification or add additional context in comments.

1 Comment

Worth to mention that now there is a workaround provided under this feature request.
1

It is indeed possible to configure application setting as of the time of this writing. For example:

resource "azurerm_static_site" "my_static_frontend" {
  name                = "my_cool_app"
  resource_group_name = azurerm_resource_group.rg.name
  location            = azurerm_resource_group.rg.location

  app_settings = {
    "DB_CONNECTION_STRING"      = local.db_conn_string
    "STORAGE_CONNECTION_STRING" = azurerm_storage_account.mystorageacct.secondary_connection_string
    "AZURE_CLIENT_ID"           = azuread_application_registration.my_static_frontend.client_id
    "AZURE_CLIENT_SECRET"       = azuread_application_password.my_static_frontend.value
  }
}

1 Comment

Provider minimum version: 3.76.0.
1

For this effect, and for the sake of simplicity and keeping the terraform code clean, I prefer to use az cli:

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

This can also be automated and, just as any IaC tool, is idempotent. Thus on my Azure DevOps pipeline, I'm applying these settings after running terraform.

1 Comment

+1. Agree. I would prefer this over azapi_resource_action. I am not marking as answer, only since, original question title requested solution using Terraform.
0

I was lucky with following AzureRM + AzApi configuration:

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"
    }
  })
}

to see all available properties and their format, go to your Static Web App and in the Overview open the JSON View on the right side.

It might be a better idea to create and configure the Static Web App with AzAPI only instead of creating and updating with different providers: https://learn.microsoft.com/de-de/azure/templates/microsoft.web/staticsites?pivots=deployment-language-terraform

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.