【发布时间】: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,它也会抛出相同的错误。