我们可以更改子模块“.terraform/modules”中的某些内容吗?

Posted

技术标签:

【中文标题】我们可以更改子模块“.terraform/modules”中的某些内容吗?【英文标题】:Can we change something in child modules ".terraform/modules"? 【发布时间】:2021-11-18 03:28:48 【问题描述】:

当我运行 terraform plan 时出现以下错误:

Error: Reference to undeclared input variable
│ 
│   on .terraform/modules/ec2-datapipeline/main.tf line 5, in resource "aws_instance" "this":
│    5:   count = "$var.count"
│ 
│ An input variable with the name "count" has not been declared. This
│ variable can be declared with a variable "count"  block.
╵
╷
│ Error: Incorrect attribute value type
│ 
│   on .terraform/modules/ec2-datapipeline/main.tf line 13, in resource "aws_instance" "this":
│   13:   vpc_security_group_ids = ["$var.vpc_security_group_ids"]
│     ├────────────────
│     │ var.vpc_security_group_ids is a list of string, known only after apply
│ 
│ Inappropriate value for attribute "vpc_security_group_ids": element 0:
│ string required.
╵
╷
│ Error: Unsupported argument
│ 
│   on .terraform/modules/ec2-datapipeline/main.tf line 23, in resource "aws_instance" "this":
│   23:   root_block_device      = "$var.root_block_device"
│ 
│ An argument named "root_block_device" is not expected here. Did you mean to
│ define a block of type "root_block_device"?
╵
╷
│ Error: Unsupported argument
│ 
│   on .terraform/modules/ec2-datapipeline/main.tf line 24, in resource "aws_instance" "this":
│   24:   ebs_block_device       = "$var.ebs_block_device"
│ 
│ An argument named "ebs_block_device" is not expected here. Did you mean to
│ define a block of type "ebs_block_device"?
╵
╷
│ Error: Unsupported argument
│ 
│   on .terraform/modules/ec2-datapipeline/main.tf line 25, in resource "aws_instance" "this":
│   25:   ephemeral_block_device = "$var.ephemeral_block_device"
│ 
│ An argument named "ephemeral_block_device" is not expected here. Did you
│ mean to define a block of type "ephemeral_block_device"?
╵
╷
│ Error: Error in function call
│ 
│   on .terraform/modules/ec2-datapipeline/main.tf line 36, in resource "aws_instance" "this":
│   36:   tags = "$merge(var.tags, map("Name", format("%s-%d", var.name, count.index+1)))"
│     ├────────────────
│     │ count.index is a number, known only after apply
│     │ var.name will be known only after apply
│ 
│ Call to function "map" failed: the "map" function was deprecated in
│ Terraform v0.12 and is no longer available; use tomap( ... ) syntax to
│ write a literal map.

当我在 .terraform/modules/main.tfvariables.tf 文件中修复它们时,计划成功并且当我从 CLI 触发时它通过了。但是,当我从 UI 触发计划时实际推送代码时,它不起作用。

所以,当我做terraform init --upgrade 时。它恢复正常,我可以看到同样的问题。

你能帮我解决这个问题吗?谢谢!

【问题讨论】:

您的问题缺乏细节。你具体改了什么?计划是什么?更改前的原始文件是什么? 我已经更新了这个问题,你能检查一下吗?谢谢! 【参考方案1】:

.terraform/modules/ec2-datapipeline/main.tf 中的内容实际上是来自名为“ec2-datapipeline”的外部模块的 main.tf 文件,该模块在您自己的代码中某处引用(查找 module "ec2-datapipeline")。

您必须修复“ec2-datapipeline”源中的错误,提交并标记它,然后在您自己的代码中更新源引用。

您的更改在重新初始化时消失的原因是,您通过了 --upgrade 开关,该开关告诉 Terraform 重新下载外部模块源,即使它们在本地缓存在 .terraform/modules 缓存中。

【讨论】:

以上是关于我们可以更改子模块“.terraform/modules”中的某些内容吗?的主要内容,如果未能解决你的问题,请参考以下文章

当子模块 repo 更改时,在主 repo 上自动运行工作流

Git子模块与依赖管理?

Git:在分支中创建子模块

多模块项目仅编译父pom不编译子module

Git - 子模块脏 - Visual Studio项目文件

如何摆脱 Git 子模块未跟踪状态?