You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Terraform配置Azure Linux App Service的Java运行时栈

问题描述

我正尝试用Terraform配置Azure Linux App Service的Java运行时栈,参考了azurerm_linux_web_app的文档,并且根据az webapp list-runtimes --os-type linux的输出配置了java_server、java_server_version和java_version参数,但遇到了两个问题:

第一次尝试

使用以下Terraform代码配置:

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

部署完成后,Azure门户中的Runtime Stack区域显示配置为空。

第二次尝试

看到相关Issue提到设置linux_fx_version可以解决问题,于是更新代码:

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

但新版本的azurerm_linux_web_app资源中,linux_fx_version已变为计算属性,无法手动设置,报错如下:

╷
│ 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.
解决方案

问题出在第一次尝试中application_stack的参数值配置错误,导致Azure无法正确识别运行时栈。正确的参数组合需要和az webapp list-runtimes --os-type linux输出的格式匹配,具体调整如下:

正确的Terraform配置

resource "azurerm_linux_web_app" "app_service" {
  # 补充必填参数:name、resource_group_name、service_plan_id等
  name                = "your-app-name"
  resource_group_name = "your-rg-name"
  service_plan_id     = azurerm_service_plan.app_service_plan.id

  site_config {
    application_stack {
      java_server         = "JAVA"
      java_server_version = "21"  # 移除前缀"java",直接使用版本号
      java_version        = "21"
    }
    app_command_line = "java -jar /home/site/wwwroot/app.jar"
  }
}

关键说明

  1. 参数格式匹配:java_server_version需直接使用纯版本号(如"21"),Azure运行时栈标识格式为JAVA|21,对应到application_stack中就是java_server="JAVA"、java_server_version="21",带前缀的"java21"会导致识别失败。
  2. 计算属性规则:新版本azurerm provider中,linux_fx_version由application_stack配置自动生成,属于计算属性,无需手动设置,这也是第二次尝试报错的原因。
  3. 必填参数补充:上述代码补充了azurerm_linux_web_app的核心必填参数,实际部署时需替换为你的资源信息。

部署完成后,Azure门户的Runtime Stack区域会正确显示为Java 21。

内容的提问来源于stack exchange,提问作者Fayzs

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 06:51:11