【问题标题】:Error when creating Azure resources with Terraform使用 Terraform 创建 Azure 资源时出错
【发布时间】:2020-11-20 17:26:50
【问题描述】:

我正在尝试从 Azure 网站 CLI 创建 Azure 资源,但奇怪的是,它在运行“terraform apply”后会引发以下错误。以前不是这样的。

Error: A resource with the ID "/subscriptions/certdab-as441-4a670-bsda-2456437/resourceGroups/commapi" 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.

  on terra.tf line 14, in resource "azurerm_resource_group" "rg1":
  14: resource "azurerm_resource_group" "rg1" {

地形代码如下;

provider "azurerm" {
  version = "=2.20.0"
  features {}
  subscription_id = "c413asdasdasdadsasda1c77"
  tenant_id       = "da956asdasdadsadsasd25a535"
}

resource "azurerm_resource_group" "rg" {
  name     = "commapi"
  location = "West Europe"
}


resource "azurerm_resource_group" "rg1" {
  name     = "commapi"
  location = "West Europe"
}

关于他们的网站,如果已经创建了资源组,我们应该导入它。但我不明白是怎么回事?

他们说我应该像下面这样导入,我应该将此行添加到我的 terraform 代码中吗? 还是应该为每个 Azure 工具运行此导入命令?

terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1

例如,如果我之前已经创建了 10 个 Azure 资源,并且如果我将 11.th 工具添加到我的 Terraform 代码中,我是否应该为之前已经创建的每 10 个资源运行这个“导入”命令?太奇怪了。

如何创建这些资源?

编辑: 如果我再试一次,会抛出下面的错误;

错误:ID 为“/subscriptions/asdd-asde1-4asd-bsda-asasd/resourceGroups/commerceapi/providers”的资源

/Microsoft.ApiManagement/service/commapi-apim" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management" for more information.

  on terra.tf line 65, in resource "azurerm_api_management" "apm":
  65: resource "azurerm_api_management" "apm" {

谢谢!

示例 2:

例如,当我创建 API 时,它会抛出以下错误;

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_api_management.apm: Creating...

Error: A resource with the ID "/subscriptions/c4112313-123-123-123-1c77/resourceGroups/testapi/providers/Microsoft.ApiManagement/service/api-apim" already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for "azurerm_api_management" for more information.

  on commerceApi.tf line 67, in resource "azurerm_api_management" "apm":
  67: resource "azurerm_api_management" "apm" {

之后,当我尝试导入命令时,这次它会抛出以下错误;

PS C:\Users\set\Desktop\Terraform\Test_Commerceapi> terraform import azurerm_api_management.apm /subscriptions/c4asdb-234e1-23-234a-23424337/resourceGroups/testapi
azurerm_api_management.apm: Importing from ID "/subscriptions/c4234324-234-234-23-4347/resourceGroups/testapi"...
azurerm_api_management.apm: Import prepared!
  Prepared azurerm_api_management for import
azurerm_api_management.apm: Refreshing state... [id=/subscriptions/c4234324-23-4234-234324-77/resourceGroups/testapi]

Error: making Read request on API Management Service "" (Resource Group "testapi"): apimanagement.ServiceClient#Get: Invalid input: autorest/validation: validation failed: parameter=serviceName constraint=MinLength value="" details: value length must be greater than or equal to 1

【问题讨论】:

  • 为什么要在 terraform 代码中创建两个同名的资源组?
  • 这只是一个示例,即使我添加了一个 vnet 或 VM,它也会抛出相同的错误。

标签: azure terraform


【解决方案1】:

如果您想将现有基础设施置于 Terraform 管理之下,您可以使用Import。

例如,导入现有资源组。

像这样在.tf 文件中声明资源。

resource "azurerm_resource_group" "rg" {
}

然后运行terraform import azurerm_resource_group.rg /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1 导入现有资源组。

然后您可以编辑 Terraform 文件并将现有资源的名称和位置添加到资源组。在此之后,您就可以使用该资源了。阅读how to Import an Existing Azure Resource in Terraform了解更多详情。

请注意,Terraform import 只能将资源导入状态。它不生成配置。如果你只是想依赖现有资源创建新资源,你可以使用data sources。

数据源允许获取或计算数据以在其他地方使用 地形配置。使用数据源允许 Terraform 配置以使用在 Terraform 之外定义的信息, 或由另一个单独的 Terraform 配置定义。

例如,使用Data Source: azurerm_resource_group 访问有关现有资源组的信息。您可以在现有 VNet 中创建新资源。

data "azurerm_resource_group" "example" {
  name = "existing"
}

output "id" {
  value = data.azurerm_resource_group.example.id
}

【讨论】:

  • 例如,我有一个 .tf 文件,它有 15 个 Azure 资源。几个月后我决定在这个文件中添加一个新资源,那么此时我应该“导入”之前已经创建的所有这 15 个资源吗?
  • 否,如果新资源与现有资源无关,则只需使用 terraform 创建此新资源即可。 terraform import 只是用来将资源导入到状态文件(.tfstate)中,以便您可以使用 terraform 管理所有资源。
  • 您还有什么问题吗?
  • 对不起,我仍然不明白应该将哪个“导入”行添加到哪个文件中。我会尝试在今天添加一个新示例。
  • 您可以在主 .tf 文件中声明现有资源,然后根据您的示例运行 terraform import azurerm_api_management.apm <apimID>。
猜你喜欢
  • 2023-02-11
  • 2020-11-18
  • 2022-07-24
  • 2018-11-24
  • 2022-01-20
  • 2021-03-31
  • 1970-01-01
  • 2021-01-21
  • 2019-05-19
相关资源
最近更新 更多