Terraform:导入模块中保存的资源,而不是根目录中的资源

Ben*_*Ben 5 terraform

我有一个现有资源,我想将其导入到我的配置中,为了保持风格,我已将其写入模块中,如下所示。

当我尝试导入它(使用terraform import resource.module id)时,出现错误,如下所示 >Before importing this resource, please create its configuration in the root module.

如果我将其添加到我的根目录中,则会在重复资源上出错。

如何导入不在我的根模块中的资源?

当前结构:#modules.tf

module my_module {
   source ./modules
   ...
} 
Run Code Online (Sandbox Code Playgroud)

#./modules/main.tf

resource 'my_resource' 'my_resource_name' {
   ...
}
Run Code Online (Sandbox Code Playgroud)

#./modules/output.tf

output {
   value = ...
}
Run Code Online (Sandbox Code Playgroud)

Che*_* A. 9

将资源导入 terraform 的最简单方法是首先运行terraform plan,检查命令输出并查看它在模块下生成的资源名称是什么。

然后跑terraform import <resource-name-in-module> <arn/id, depend on the resource>

所以在你的情况下,你可能需要运行类似的东西

terraform import 'my_module.my_resource.my_resource_name' 'id'
Run Code Online (Sandbox Code Playgroud)

请注意模块名称周围的引号;如果模块包含count,这是必要的,所以这是一个习惯的好习惯。