如何让 terraform 自动导入所有 "already exists" 资源?

How to have terraform import all of the "already exists" resources automatically?

当我 运行 terraform apply -auto-approve 我得到以下错误:

Error: A resource with the ID "/subscriptions/.../resourceGroups/RG-SCUSTFStorage" 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.

我了解我需要 运行 terraform import 将资源导入我的工作区。问题是我需要一次为所有缺少的资源指定一个资源 ID。

有什么方法可以让 terraform import 自动导入所有“已经存在”的资源而无需一次输入一个资源 ID?

遗憾的是,您只能手动导入带有资源ID的现有资源:

The import command doesn’t automatically generate the configuration to manage the infrastructure, though. Because of this, importing existing infrastructure into Terraform is a multi-step process.

有关部署前所有 Terraform 脚本的更多详细信息here. I will suggest you use remote state storage。如果您没有包含所有已部署资源的状态文件,那么您只能将它们一一导入。