hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.5k stars 4.59k forks source link

Support for app settings configuration for azurerm_static_site #13451

Open irnc opened 2 years ago

irnc commented 2 years ago

Community Note

Description

New or Affected Resource(s)

Potential Terraform Configuration

Same as app_settings for azurerm_app_service and azurerm_function_app: app_settings - (Optional) A map of key-value pairs for App Settings and custom values.

resource "azurerm_static_site" "example" {
  name                = "example"
  app_settings = {
    "SOME_KEY" = "some-value"
  }
}

References

fvdnabee commented 2 years ago

One work-around for setting SWA App Settings is to provision an ARM Resource Group Template Deployment via azurerm_resource_group_template_deployment for a Microsoft.Web/staticSites/config resource. Just make sure you link it to the correct parent resource for your SWA.

Working example:

variable "resource_group_name" {}

data "azurerm_resource_group" "rg" {
  name = var.resource_group_name
}

variable "swa_name" {}
variable "swa_sku_tier" {}

resource "azurerm_static_site" "frontend" {
  name                = var.swa_name
  resource_group_name = data.azurerm_resource_group.rg.name
  location            = data.azurerm_resource_group.rg.location
  sku_tier            = var.swa_sku_tier
}

variable "app_setting1" {}
variable "app_setting2" {}

resource "azurerm_resource_group_template_deployment" "frontend_appsettings" {
  deployment_mode     = "Incremental"
  name                = "frontend-appsettings"
  resource_group_name = data.azurerm_resource_group.rg.name

  template_content = file("staticwebapp-arm-staticsite-config.json")
  parameters_content = jsonencode({
    staticSiteName = {
      value = azurerm_static_site.frontend.name
    },
    appSetting1 = {
      value = var.app_setting1
    },
    appSetting2 = {
      value = var.app_setting2
    },
  })
}

With staticwebapp-arm-staticsite-config.json:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "staticSiteName": {
      "type": "string"
    },
    "appSetting1": {
      "type": "string"
    },
    "appSetting2": {
      "type": "string"
    }
  },
  "variables": {},
  "resources": [
    {
      "type": "Microsoft.Web/staticSites/config",
      "apiVersion": "2020-10-01",
      "name": "[concat(parameters('staticSiteName'), '/appsettings')]",
      "kind": "string",
      "properties": {
        "APP_SETTING1": "[parameters('appSetting1')]",
        "APP_SETTING2": "[parameters('appSetting2')]"
      }
    }
  ],
  "outputs": {}
}
kiranpradeep commented 2 years ago

This issue is tagged with an enhancement label and not with a bug label. Imagine azurerm_windows_web_app or azurerm_windows_function_app resources without app_settings parameter - Not really usable. @koikonom Could the label be corrected?

Azure static web apps was GA on May/21. If this project is backed by an organisation, could a document be created which helps users get an idea about timelines (from GA) on basic resource support. At the moment, users have no clue if the resource will be supported or not. Or is the project totally reliant on community contribution to add this support?

zhenik commented 2 years ago

terraform-provider-azapi might be useful to resolve this problem.

Devvox93 commented 1 year ago

It's not just the app settings, it is also other configuration. If you look at the Terraform template reference for Microsoft.Web staticSites in the docs, as well as the subresources (check navigation to the left), you see that all those resources and settings are documented to use "azapi_resource" with

body = jsonencode({
    properties = {
        ...
    }
})

Perhaps this is a separate issue, but I don't see why solving this for the app settings could not be part of general support for the entire API version (which is 2022-03-01 in the aforementioned docs).

This issue has no milestone yet. While the azapi_resource approach is documented well, it would be nice to have an implementation like there is for other (Terraform/Azure) resources. Any update on where this fits on the roadmap?

I could maybe try to work on this if time permits it, but only if it would be merged or picked up further.

jonnekleijer commented 8 months ago

Is it supported now? Since azurerm 3.76.0 it is in the documentation: https://registry.terraform.io/providers/hashicorp/azurerm/3.76.0/docs/resources/static_site

jdelforno commented 7 months ago

Is it supported now? Since azurerm 3.76.0 it is in the documentation: https://registry.terraform.io/providers/hashicorp/azurerm/3.76.0/docs/resources/static_site

It's documented but I just tried to use it and it's not working unfortunately.