【问题标题】:Terraform can't create resource in group already existTerraform 无法在组中创建资源已存在
【发布时间】:2022-02-17 22:47:03
【问题描述】:

我将使用 Terraform 创建一个示例 azure Web 应用程序。 我使用此代码来创建资源。

这是我的 main.tf 文件:

resource "azurerm_resource_group" "rg" {
  name     = var.rgname
  location = var.rglocation
}

resource "azurerm_app_service_plan" "plan" {
  name                = var.webapp_plan_name
  location            = var.rglocation
  resource_group_name = var.rgname
  sku {
    tier     = var.plan_settings["tier"]
    size     = var.plan_settings["size"]
    capacity = var.plan_settings["capacity"]
  }
}

resource "azurerm_app_service" "webapp" {
  name                = var.webapp_name
  location            = var.rglocation
  resource_group_name = var.rgname
  app_service_plan_id = azurerm_app_service_plan.plan.id
}

这是变量.tf

# variables for Resource Group
variable "rgname" {
  description = "(Required)Name of the Resource Group"
  type        = string
  default     = "example-rg"
}

variable "rglocation" {
  description = "Resource Group location like West Europe etc."
  type        = string
  default     = "eastus2"
}

# variables for web app plan
variable "webapp_plan_name" {
  description = "Name of webapp"
  type        = string
  default     = "XXXXXXXXXx"
}

variable "plan_settings" {
  type        = map(string)
  description = "Definition of the dedicated plan to use"

  default = {
    kind     = "Linux"
    size     = "S1"
    capacity = 1
    tier     = "Standard"
  }
}

variable "webapp_name" {
  description = "Name of webapp"
  type        = string
  default     = "XXXXXXXX"
}

terraform apply --auto-approve 显示错误:

Error: creating/updating App Service Plan "XXXXXXXXXXX" (Resource Group "example-rg"): web.AppServicePlansClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- Original Error: Code="ResourceGroupNotFound" Message="Resource group 'example-rg' could not be found." 

但在 Azure Portal 中,会创建资源组

我的代码有什么问题?

在传递到下一个资源之前,我可以在 Terraform 上检查是否创建了资源吗?

【问题讨论】:

    标签: azure terraform


    【解决方案1】:

    你需要引用资源来表明它们之间的依赖关系到terraform,这样才能保证先创建资源组,然后再创建其他资源。该错误表明资源组尚不存在。您的代码尝试先创建 ASP,然后再创建 RG。

    resource "azurerm_resource_group" "rg" {
      name     = var.rgname
      location = var.rglocation
    }
    
    resource "azurerm_app_service_plan" "plan" {
    ...
      resource_group_name = azurerm_resource_group.rg.name
    ...
    }
    

    【讨论】:

    • 谢谢!但我不能使用变量?
    【解决方案2】:

    ... 但是我不能使用变量?

    要回答这个问题并提供有关当前问题的更多详细信息,您的原始代码中发生的情况是您没有以 terraform 可以用来创建其dependency graph 的方式引用 terraform 资源。

    如果var.rgname 等于<my-rg-name> 并且azurerm_resource_group.rg.name 也等于<my-rg-name> 那么从技术上讲,这是一样的,对吧?

    嗯,不,不一定。它们确实具有相同的价值。不同之处在于第一个只是回显该值。第二个包含相同的值,但它也是 terraform 的指令,说:“嘿,等一下。我们需要来自 azurerm_resource_group.rg 的名称值,所以让我先确定一下,然后我会提供此资源"

    区别很微妙但很重要。以这种方式使用值可以让 Terraform 了解它首先必须提供什么,并让它创建它的依赖关系图。单独使用变量不会。尤其是在大型项目中,请始终尝试使用资源变量值,而不仅仅是输入变量,这将有助于避免不必要的问题。

    使用输入变量来定义某些初始数据是很常见的,例如您在上面所做的资源组的名称。然而,在那之后,每次在清单中引用 resource_group_name 时,您应该始终使用 terraform 生成的值,所以 resource_group_name = azurerm_resource_group.rg.name

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 2019-01-26
      • 2020-10-31
      • 2020-04-16
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 2020-04-13
      相关资源
      最近更新 更多