Terraform-加载模块时出错-没有这样的文件或目录(文件夹重命名后)



我在使用terraform时遇到了一个奇怪的问题。

考虑以下文件系统:

├── Modules
│   └── IAM
│       ├── outputs.tf
│       ├── resources.tf
│       └── variables.tf
├── outputs.tf
├── providers.tf
├── resources.tf
(Created automatically  by Terraform)
├── terraform.tfstate
└── terraform.tfstate.backup

我从root模块调用IAM模块:

##resources.tf
# Other Resource configuration...

# Calling IAM module
module "iam" {
source = "Modules/IAM"
}

这很简单,一切都很好,直到我将模块的文件夹重命名为名称

文件夹的原始名称是IAM_Module,当我将其更改为IAM时,事情开始破裂。

terraform init的输出(模块的名称是iam,所以不要混淆(:

Initializing modules...
- module.iam
Error downloading modules: Error loading modules: module iam:
open .terraform/modules/94...5b: no such file or directory

检查.terraform/modules内部,我可以看到,由于某种原因,指向文件夹旧名称(IAM_Module(的符号链接仍然存在:

├── 94...5b -> /lab1/Modules/IAM
├── b7...3d -> /lab1/Modules/IAM_Module <--- Previous name of folder
└── modules.json

terraform.tfstate内部,我可以看到模块存在(在根之后(:

"modules": [
{
"path": [
"root"
],
"outputs": {},
"resources": {},
"depends_on": []
},
{
"path": [
"root",
"iam" <----- Here
],
"outputs": {},
"resources": {

我尝试terraform refresh,但收到相同的no such file or directory错误。

Terraform版本:v0.11.11。

任何帮助都将不胜感激。


(*(在这个线程中,他们似乎通过手动干预解决了相同的错误(由不同原因引起(,我更希望避免这种情况。

我试图再次重现这个问题

首先,我手动删除了.terraform文件并运行了terraform init
现在我处于文件重命名前的状态。

在我再次重命名该文件后,我运行了terraform plan,并收到以下明确的错误:

Error: Error loading modules: module iam: not found, may need to run 'terraform init'.

运行terraform init后,我检查了.terraform/modules文件,看到了两个符号链接:
-一个用于新模块
-一个用于旧模块,现在用红色

这两个模块仍然出现在modules.json文件下。

不知道为什么我第一次尝试时没有弹出这个消息,但我认为在这种情况下,Terraform get命令可能也会有所帮助——来自文档:

terraform get命令用于下载和更新模块在根模块中提到。

在我的情况下,模块已经下载,所以我必须使用-update标志运行terraform get

如果模块已经下载并且-update标志未设置,Terraform不会有任何作用。


我认为在删除自动生成的文件(如.terraform(之前,需要考虑两种解决方案之一(terraform init / get(。

我希望这能帮助到别人。

最新更新