使用 Terraform 创建 Azure 资源时出错

sle*_*epy 6 azure terraform

我正在尝试从 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" {
Run Code Online (Sandbox Code Playgroud)

Terraform 代码如下;

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"
}
Run Code Online (Sandbox Code Playgroud)

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

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

terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1
Run Code Online (Sandbox Code Playgroud)

因此,例如,如果我之前已经创建了 10 个 Azure 资源,并且将第 11 个工具添加到我的 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" {
Run Code Online (Sandbox Code Playgroud)

谢谢!

样品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" {
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

Nan*_*ong 5

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

例如,导入现有资源组。

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

resource "azurerm_resource_group" "rg" {
}
Run Code Online (Sandbox Code Playgroud)

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

然后,您可以编辑 Terraform 文件并将现有资源的名称和位置添加到资源组中。之后,您就可以使用该资源了。有关更多详细信息,请阅读如何在 Terraform 中导入现有 Azure 资源

请注意,Terraform 导入只能将资源导入到状态中。它不生成配置。如果您只想依赖现有资源创建新资源,则可以使用数据源

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

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

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

output "id" {
  value = data.azurerm_resource_group.example.id
}
Run Code Online (Sandbox Code Playgroud)