我正在尝试从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" {
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"
}
关于他们的网站,如果已经创建了资源组,则应该导入它。但是我不知道怎么办?
他们说我应该像下面那样导入,是否应该将此行添加到我的terraform代码中? 还是应该为每个Azure工具运行此导入命令?
terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1
例如,如果我之前已经创建了10个Azure资源,并且在Terraform代码中添加了第11.th个工具,那么我应该为之前创建的那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
答案 0 :(得分:0)
如果要将现有基础结构置于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导入只能将资源导入到状态中。它不会生成配置。如果您仅打算依靠现有资源来创建新资源,则可以使用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
}