使用Terraform创建Azure资源时出错
我试图从Azure网站CLI创建Azure资源,但奇怪的是,在运行“terraform apply”后,它抛出了下面的错误。以前不是这样的使用Terraform创建Azure资源时出错,azure,terraform,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
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"
}
关于他们的网站,如果已经创建了资源组,我们应该导入它。但我不明白怎么回事
他们说我应该像下面这样导入,我应该把这一行添加到我的地形代码中吗?
还是应该为每个Azure工具运行此导入命令
terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1
因此,例如,如果我之前已经创建了10个Azure资源,并且如果我将第11个工具添加到我的Terraform代码中,那么我是否应该为之前已经创建的10个资源运行此“导入”命令?真奇怪
如何创建这些资源
编辑:
如果我再试一次,抛出下面的错误
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
错误:ID为“/订阅/asdd-asde1-4asd-bsda-ASASSD/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
之后,当我尝试导入命令时,这次它抛出下面的错误
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
如果您想将现有基础设施置于地形管理之下,可以使用 例如,导入现有资源组 像这样在
.tf
文件中声明资源
resource "azurerm_resource_group" "rg" {
}
然后运行terraform import azurerm_resource_group.rg/subscriptions/00000000-0000-0000-00000000/resourceGroups/group1
导入现有资源组
然后,您可以编辑地形文件并将现有资源的名称和位置添加到资源组中。之后,您就可以使用该资源了。有关详细信息,请阅读
请注意,Terraform import只能将资源导入状态。它不会生成配置。如果您只是想依靠现有资源创建新资源,则可以使用
数据源允许获取或计算数据,以便在中的其他位置使用
地形配置。使用数据源可以进行地形配置
利用地形以外定义的信息的配置,
或由另一个单独的地形配置定义
例如,用于访问有关现有资源组的信息。您可以在现有VNet中创建新资源
data "azurerm_resource_group" "example" {
name = "existing"
}
output "id" {
value = data.azurerm_resource_group.example.id
}
如果您想将现有基础设施置于地形管理之下,可以使用 例如,导入现有资源组 像这样在
.tf
文件中声明资源
resource "azurerm_resource_group" "rg" {
}
然后运行terraform import azurerm_resource_group.rg/subscriptions/00000000-0000-0000-00000000/resourceGroups/group1
导入现有资源组
然后,您可以编辑地形文件并将现有资源的名称和位置添加到资源组中。之后,您就可以使用该资源了。有关详细信息,请阅读
请注意,Terraform import只能将资源导入状态。它不会生成配置。如果您只是想依靠现有资源创建新资源,则可以使用
数据源允许获取或计算数据,以便在中的其他位置使用
地形配置。使用数据源可以进行地形配置
利用地形以外定义的信息的配置,
或由另一个单独的地形配置定义
例如,用于访问有关现有资源组的信息。您可以在现有VNet中创建新资源
data "azurerm_resource_group" "example" {
name = "existing"
}
output "id" {
value = data.azurerm_resource_group.example.id
}
为什么在terraform代码中创建两个同名的资源组?这只是一个示例,即使我添加了一个vnet或VM,它也会抛出相同的错误。为什么在terraform代码中创建两个同名的资源组?这只是一个示例,即使我添加了一个vnet或VM,它也会抛出相同的错误。例如,我有一个.tf文件,它有一个15个Azure资源。几个月后,我决定向该文件添加一个新资源,所以此时我应该“导入”吗“之前已经创建的所有这15个资源?不,如果新资源与现有资源无关,您只需要使用terraform创建此新资源。terraform导入仅用于将资源导入到状态文件(
.tfstate
)中,以便您可以使用terraform管理所有资源。您还有任何问题吗?抱歉,我仍然不知道应该向哪个文件添加什么“导入”行。今天我将尝试添加一个新示例。您可以在主.tf文件中声明现有资源,然后根据您的示例运行terraform import azurerm_api_management.apm
。例如,我有一个.tf文件,它有15个Azure资源。几个月后,我决定向该文件添加一个新资源,那么此时我是否应该“导入”之前已创建的所有这15个资源?不,如果新资源与现有资源无关,您只需要使用terraform创建此新资源。terraform导入仅用于将资源导入到状态文件(.tfstate
)中,以便您可以使用terraform管理所有资源。您还有任何问题吗?抱歉,我仍然不知道应该向哪个文件添加什么“导入”行。今天我将尝试添加一个新示例。您可以在主.tf文件中声明现有资源,然后根据示例运行terraform import azurerm\u api\u management.apm
。