Configuring CORS in a Terraform Azure App Service Resource
Asked Answered
G

2

10

I have the following Terraform resource for configuring an Azure app service:

resource "azurerm_app_service" "app_service" {
  name                = "Test-App-Service-3479112"
  location            = "${azurerm_resource_group.resource_group.location}"
  resource_group_name = "${azurerm_resource_group.resource_group.name}"
  app_service_plan_id = "${azurerm_app_service_plan.app_service_plan.id}"

  site_config {
    dotnet_framework_version = "v4.0"
    remote_debugging_version = "VS2012"
  }

  app_settings {
    "ASPNETCORE_ENVIRONMENT" = "test"
    "WEBSITE_NODE_DEFAULT_VERSION" = "4.4.7"
  }
}

I am attempting to add a CORS origin value to be utilized in my resource. Is there way to add this in Terraform, or if there is not, how could I go about configuring this in my Terraform file (possibly with the Azure SDK)?

Gentoo answered 8/12, 2017 at 16:16 Comment(0)
J
10

All,

This is now available here: https://www.terraform.io/docs/providers/azurerm/r/app_service.html#cors

And here is my example:

resource "azurerm_app_service" "my-app" {
  name                = "${var.api_name}"
  location            = "${var.location}"
  resource_group_name = "${var.resource_group}"
  app_service_plan_id = "${azurerm_app_service_plan.api_asp.id}"

  site_config {
    cors {
      allowed_origins = ["https://${var.ui_name}${var.dns_suffix}"]
    }
  }

  identity = {
    type = "SystemAssigned"
  }
}

And proof that the above works: enter image description here

Jehius answered 25/6, 2019 at 21:25 Comment(0)
A
3

Currently, it is not supported. You could check azurerm_app_service.

In currently terrform version, cors is not supported.

If possible, you could use Azure template to do this, you could check this example.

 "properties": {
        "cors": {
          "allowedOrigins": [
            "[concat('https://', parameters('siteName'), '.azurewebsites.net')]"
          ]
        },
Abuttals answered 11/12, 2017 at 1:55 Comment(2)
here is CORS module: github.com/transactiveltd/terraform-azurerm-app-service-cors and it seems to be working for me.Aiguillette
I see cors - (Optional) A cors block on terraform documentation, but the cors object block isn't working in site_config. Thanks tridy for helping out with terraform link.Languish

© 2022 - 2024 — McMap. All rights reserved.