简体   繁体   中英

Terraform can't create resource in group already exist

I would create a sample azure web app using Terraform. I use this code to create the resoucre.

This is my main.tf file:

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
}

and this is the variable.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"
}

The terraform apply --auto-approve show a error:

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

but in Azure Portal, the ressource group is created

what's wrong in my code?

Can i hold on Terraform to check if resource is created or not before pass to next resource?

You need to reference the resources to indicate the dependency between them to terraform, so that it can guarantee, that the resource group is created first, and then the other resources. The error indicates, that the resource group does not exist, yet. Your code tries to create the ASP first and then the 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
...
}

... but I can't use variable?

To answer that question and give more detail about the current issue, what's happening in your original code is that you're not referencing terraform resources in a way that terraform can use to create its dependency graph .

if var.rgname equals <my-rg-name> and azurerm_resource_group.rg.name also equals <my-rg-name> then technically, that's the same, right?

Well, no, not necessarily. They indeed have the same value. The difference is that the first one just echos the value. The second one contains the same value but it's also instruction to terraform saying, "Hey, wait a minute. We need the name value from azurerm_resource_group.rg so let me make sure I set that up first, and then I'll provision this resource "

The difference is subtle but important. Using values in this way lets Terraform understand what it has to provision first and lets it create its dependency graph. Using variables alone does not. Especially in large projects, alway try to use the resource variable value instead of just input variables which will help avoid unnecessary issues.

It's quite common to use an input variable to define certain initial data, like the name of the resource group as you did above. After that, however, every time resource_group_name is referenced in the manifests you should always use terraform generated value, so resource_group_name = azurerm_resource_group.rg.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM