如何通过Terraform部署Azure API App Service?仅发现Web App部署方案
如何用Terraform部署Azure API App Service
当然可以用Terraform部署Azure API App Service,Azure API App本质是配置了API专属特性的App Service,不需要单独的Terraform资源,用你找到的azurerm_windows_web_app(或azurerm_linux_web_app)即可完成部署,只需在资源中添加API相关的配置项。
核心配置示例
以下是一个基础的Terraform配置示例,用于部署Windows环境的API App:
provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = "api-app-rg" location = "East US" } resource "azurerm_app_service_plan" "example" { name = "api-app-service-plan" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location sku_name = "B1" os_type = "Windows" } resource "azurerm_windows_web_app" "api_app" { name = "my-api-app-001" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location service_plan_id = azurerm_app_service_plan.example.id site_config { # 启用API App专属配置 api_settings { enabled = true } # 可选:配置CORS规则,适配API跨域需求 cors { allowed_origins = ["https://your-frontend-domain.com"] support_credentials = true } } # 可选:设置API应用的运行时,比如.NET Core application_stack { dotnet_version = "6.0" } }
关键配置说明
site_config.api_settings.enabled = true:这是标记该App Service为API App的核心配置,启用后Azure会自动开启API相关的管理特性(比如API门户、OAuth集成等)。- 运行时配置:根据你的API技术栈,在
application_stack中指定对应的版本(如Python、Node.js、.NET等)。 - CORS配置:如果你的API需要被前端调用,务必在
cors块中配置允许的域名,避免跨域问题。
如果需要部署Linux环境的API App,只需将azurerm_windows_web_app替换为azurerm_linux_web_app,并调整对应的application_stack配置即可。
内容的提问来源于stack exchange,提问作者pks
相关产品推荐
相关产品推荐

