azureterraformazure-web-app-serviceterraform-provider-azure

How to set Java runtime stack for Azure Linux App Service using Terraform


I'm trying to configure the Java runtime stack for an Azure Linux App Service using Terraform.

I followed the Terraform documentation for azurerm_linux_web_app and configured the java_server, java_server_version, and java_version values according to the output of az webapp list-runtimes --os-type linux

1- First try

Here's my current Terraform code:

    resource "azurerm_linux_web_app" "app_service" {
      site_config {
         application_stack {
           java_server         = "JAVA"
           java_server_version = "java21"
           java_version        = "21"
          }
          app_command_line = "java -jar /home/site/wwwroot/app.jar"
        }
     }

However, after deploying, the Runtime Stack section in the Azure portal shows an empty configuration (see this screenshot).

2- Second:

I came across this related GitHub issue, which say that the linux_fx_version property can be set to fix this. So i updated my code, and tried with this:

    resource "azurerm_linux_web_app" "app_service" {
      site_config {
          linux_fx_version="JAVA|21"
          app_command_line = "java -jar /home/site/wwwroot/app.jar"
        }
     }

However, in the newer versions of the azurerm_linux_web_app resource, this property is now computed and can no longer be set manually.

╷
│ Error: Value for unconfigurable attribute
│ 
│   with module.compute.azurerm_linux_web_app.app_service,
│   on modules\004-compute\main.tf line 20, in resource "azurerm_linux_web_app" "app_service":
│   20:     linux_fx_version = "JAVA|21"
│ 
│ Can't configure a value for "site_config.0.linux_fx_version": its value will be decided automatically based on the result of applying this configuration.

Solution

  • Set Java runtime stack for Azure Linux App Service using Terraform

    Continuation from the comment section the newer Terraform providers, i.e., after azurerm 3.40+, linux_fx_version is computed internally you cannot manually set linux_fx_version anymore.

    Now, Terraform automatically builds linux_fx_version based on the fields you specify inside application_stack {}.

    Configuration:

    resource "azurerm_linux_web_app" "app_service" {
      name                = "your-linux-webapp"
      resource_group_name = azurerm_resource_group.this.name
      location            = azurerm_resource_group.this.location
      service_plan_id     = azurerm_service_plan.this.id
    
      site_config {
        application_stack {
          java_server         = "JAVA"
          java_server_version = "21" 
          java_version        = "21"  
        }
        app_command_line = "java -jar /home/site/wwwroot/app.jar"
      }
    }
    

    Refer:

    https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/linux_web_app#application_stack-1