执行地形脚本时找不到服务器场



我是Terraform的新手,我正在尝试为我的组织使用Terraform建立基础设施。在这种情况下,我已经将app_service_planal部署到azure,现在我正试图将其用于我想要使用terraform脚本创建的app_service

以下是我的app_service片段:

data "azurerm_resource_group" "test-rg" {
name = "test-rg"
}
data "azurerm_app_service_plan" "test-asp" {
name = "test-asp"
}
resource "azurerm_app_service" "test-app" {
name                = "test-app"
location            = data.azurerm_resource_group.test-rg.location
resource_group_name = data.azurerm_resource_group.test-rg.name
app_service_plan_id = azurerm_app_service_plan.test-asp.id
}

但我得到了一个ServerFarm未找到错误,这就是错误的样子:

Error: Error creating App Service "test-app" (Resource Group "test-rg"): web.AppsClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: Code="NotFound" Message="Cannot find ServerFarm with name test-asp." Details=[{"Message":"Cannot find ServerFarm with name test-asp."},{"Code":"NotFound"},{"ErrorEntity":{"Code":"NotFound","ExtendedCode":"51004","Message":"Cannot find ServerFarm with name test-asp.","MessageTemplate":"Cannot find {0} with name {1}.","Parameters":["ServerFarm","test-asp"]}}]

我还试图将这个app_service_plan导入到我的地形状态中,这就是我在导入后填充app_service_pran的方式:

resource "azurerm_app_service_plan" "test-asp"{
is_xenon                     = false
kind                         = "linux"
location                     = "eastus2"
maximum_elastic_worker_count = 1
name                         = "test-asp"
per_site_scaling             = false
reserved                     = true
resource_group_name          = "test-rg"
sku {
capacity = 1
size     = "B1"
tier     = "Basic"
}
}

这是我的导入命令:

terraform import azurerm_app_service_plan.test-asp /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/test-rg/providers/Microsoft.Web/serverfarms/test-asp

有人能建议我如何将这个已经存在的app_service_plan用于我的app_service吗。非常感谢。

在这种情况下,请确保您已经在与服务计划相同的区域创建了应用程序服务。此外,您需要指定与您的应用程序服务相匹配的服务计划。例如,您可以在基于windows的服务计划上创建一个windows应用程序应用程序,在基于Linux的服务计划中创建一个Linux应用程序应用。

也许,你的例子中有一些拼写错误。使用数据源:azurerm_app_service_plan时,应输入nameresource_group_name参数引用,并使用数据源引用app_service_plan_id

所以把你的代码改成

data "azurerm_resource_group" "test-rg" {
name = "test-rg"
}
data "azurerm_app_service_plan" "test-asp" {
name                = "test-asp"
resource_group_name = "test-rg"    # or data.azurerm_resource_group.test-rg.name
}
resource "azurerm_app_service" "test-app" {
name                = "test-app"
location            = data.azurerm_app_service_plan.test-asp.location
resource_group_name = data.azurerm_resource_group.test-rg.name
app_service_plan_id = data.azurerm_app_service_plan.test-asp.id
}

我在尝试为azure中的容器部署web应用程序时遇到了同样的问题。为我的应用程序服务计划设置位置容器服务Web应用程序解决了我的问题。下面是我的例子

使用Linux创建应用程序服务计划

resource "azurerm_app_service_plan" "appserviceplan" {
name                = "MyServicePlan-ContainerApp"
location            = "westeurope"
resource_group_name = azurerm_resource_group.rg.name
# Define Linux as Host OS
kind     = "Linux"
reserved = true # Mandatory for Linux plans
# Choose size
sku {
tier = "Standard"
size = "S1"
}
}

如果您想将映像推送到Azure容器注册表(ACR(,则需要LOCAL环境变量

locals {
env_variables = {
DOCKER_REGISTRY_SERVER_URL            = "{URL TO DOCKER REGISTRY}"
DOCKER_REGISTRY_SERVER_USERNAME       = "{ADMIN USERNAME of ACR}"
DOCKER_REGISTRY_SERVER_PASSWORD       = "{ADMIN PASS for ACR}"
}
}

为该应用程序服务计划中的容器创建Azure Web应用程序

resource "azurerm_app_service" "my-app-service-container" {
name                    = "my-app-service-container"
location                = "westeurope"
resource_group_name     = azurerm_resource_group.rg.name
app_service_plan_id     = azurerm_app_service_plan.appserviceplan.id
https_only              = false
client_affinity_enabled = true
site_config {
scm_type  = "VSTSRM"
always_on = "true"

health_check_path = "/health" # health check required in order that internal app service plan load balancer do not load-balance on instance down
}
app_settings = local.env_variables 
}

最新更新