具有该 ID 的资源已存在 - 要通过 Terraform 管理此资源需要导入到状态中

A resource with the ID already exists - to be managed via Terraform this resource needs to be imported into the State

我通常不会问这样的问题,但是我觉得卡住了,我不想破解东西,而是花时间去理解。我是 terraform 的新手,正在尝试学习它,我为自己设定的一个简单任务是创建一个 SQL 服务器。

我的环境

我之前创建了一些资源组,每当我尝试使用相同的名称时,我都会收到错误消息。

 Error: A resource with the ID "/subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learning" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information.

现在,你看看错误,经过 2 天的 google 研究,我按照这里的步骤操作。 https://gmusumeci.medium.com/how-to-import-an-existing-azure-resource-in-terraform-6d585f93ea02 and Terraform resource with the ID already exists

我创建了一个名为 existing_statee.tf 的文件,内容如下。

resource "azurerm_resource_group" "tf_learning" {
}

运行

terraform import azurerm_resource_group.tf_learning /subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learningterraform import azurerm_resource_group.tf_learning /subscriptions/000000-0000-0000-0000-00000000005/resourceGroups/tf_learning

我重新编辑了文件并保存了。

resource "azurerm_resource_group" "tf_learning" {
  # (resource arguments)
  name = "tf_learning"
  location = "UK South"
}

然后运行以下。

terraform init
terraform plan
terraform apply

令我惊讶的是,我仍然收到错误。

 Error: A resource with the ID "/subscriptions/00000-00000-0000-0000-00000000000/resourceGroups/tf_learning" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_resource_group" for more information.
│
│   with azurerm_resource_group.RG-Terraform,
│   on main.tf line 1, in resource "azurerm_resource_group" "RG-Terraform":
│    1: resource "azurerm_resource_group" "RG-Terraform" {

我的 tf.main 文件。

resource "azurerm_resource_group" "RG-Terraform" {
  name     = var.resource-group-name
  location = var.my_location
}

resource "azurerm_sql_server" "test" {
  name                         = var.my_dev_server
  resource_group_name          = azurerm_resource_group.RG-Terraform.name
  location                     = azurerm_resource_group.RG-Terraform.location
  version                      = var.my_dev_sql_version
  administrator_login          = var.my_dev_sql_login
  administrator_login_password = "change_me"
}

resource "azurerm_sql_database" "test" {
  name                = var.my_dev_sql_database
  resource_group_name = azurerm_resource_group.RG-Terraform.name
  location            = azurerm_resource_group.RG-Terraform.location
  server_name         = azurerm_sql_server.test.name
  edition                          = var.my_dev_sql_database_sku
  requested_service_objective_name = var.my_dev_sql_database_objective

  tags = {
    environment = "dev_database_build"
  }
}

variables.tf 文件

variable "resource-group-name" {
  default     = "tf_learning"
  description = "The prefix used for all resources in this example"
}

variable "app-service-name" {
  default     = "terraform-app-service"
  description = "The name of the Web App"
}

variable "location" {
  default     = "UK South"
  description = "The Azure location where all resources in this example should be created"
}

variable "my_location" {
  type = string
  default = "UK South"
}

variable "my_dev_server"{
  type = string
  default = "test-server-test"
}

variable "my_dev_sql_login" {
  type = string
  default = "mylogin"  
}

variable "my_dev_sql_version" {
  type = string
  default = "12.0"  
}

variable "my_dev_sql_database" {
  type = string
  default = "dev_db"  
}

variable "my_dev_sql_database_tag" {
  type = string
  default = "dev sql database from TF"  
}

variable "my_dev_sql_database_sku" {
  type = string
  default = "Basic"  
}

variable "my_dev_sql_database_objective" {
  type = string
  default = "Basic"  
}

我不知道下一步该怎么做,现在我会继续研究。

我想指出这一点,您在导入状态后existing_statee.tf中进行了配置

resource "azurerm_resource_group" "tf_learning" {
   name = "tf_learning"
   location = "UK South"
}

但是在main.tf你也重新定义

resource "azurerm_resource_group" "RG-Terraform" {
  name     = var.resource-group-name
  location = var.my_location
}
variable "resource-group-name" {
  default     = "tf_learning"
  description = "The prefix used for all resources in this example"
}
variable "my_location" {
  type = string
  default = "UK South"
}

由于您已经在 existing_statee.tf 中声明了此资源,也许您应该将其从 main.tf

中删除