Terraform 模块依赖关系破坏了 template_file
Posted
技术标签:
【中文标题】Terraform 模块依赖关系破坏了 template_file【英文标题】:Terraform module dependency breaks template_file 【发布时间】:2018-02-21 10:46:29 【问题描述】:不确定这是错误还是我做错了什么。当我在两个模块之间引入依赖关系时,依赖模块的 template_file 数据中的变量会起作用,并且 terraform 抱怨它找不到它。
我有两个模块,saltmaster 和 cassandra。目标是让 saltmaster 通过输出变量公开其 IP 地址,并让 cassandra 使用它。
如果我通过将 IP 地址硬编码为 cassandra 模块的输入来切断模块之间的依赖关系,那么所有资源都会正确创建。但是当我引入依赖时,terraform 会抱怨:
module.cassandra.aws_instance.momentum_cassandra[2]:资源“data.template_file.momentum_cassandra”没有变量“data.template_file.momentum_cassandra.*.vars.hostname”的属性“vars.hostname”
请注意,我在 template_file 的 vars 部分定义了 cassandra 实例的主机名,然后在 aws_instance 的 tags 块中使用它。并且它单独工作正常,但是当 cassandra 模块依赖于 saltmaster 模块时会中断。
cassandra.tf
data "template_file" "momentum_cassandra"
count = "$var.instance_count"
template = "$file("$path.module/userdata.sh")"
vars
hostname = "$format("%s%02d", var.name, count.index + 1)"
saltmaster = "$var.saltmaster"
salt_installtype = "$var.salt_installtype"
seed = "$count.index == 0 ? "true" : "false""
datacenter = "$var.datacenter"
rack = "$var.rack"
resource "aws_instance" "momentum_cassandra"
count = 3
provider = "aws.us_dev"
ami = "$var.ami"
instance_type = "$var.instance_type"
key_name = "$var.instance_key_name"
subnet_id = "$element(var.vpc_subnet_ids, count.index)"
iam_instance_profile = "$var.iam_instance_profile"
vpc_security_group_ids = ["$aws_security_group.momentum_cassandra.id"]
user_data = "$data.template_file.momentum_cassandra.*.rendered[count.index]"
lifecycle
ignore_changes = ["ami", "user_data"]
tags
Name = "$data.template_file.momentum_cassandra.*.vars.hostname[count.index]"
main.tf
module "cassandra"
source = "./tf-aws-momentum-cassandra"
saltmaster = "$module.saltmaster.private_ip"
saltmaster.tf
# create the userdata for bootstrapping
data "template_file" "saltmaster_userdata"
count = "$var.instance_count"
template = "$file("$path.module/userdata.sh")"
vars
hostname = "$format("%s%02d", var.name, count.index + 1)"
saltmaster = "localhost"
environment = "$var.environment"
installtype = "$var.installtype"
# create the salt master
resource "aws_instance" "momentum_salt_master"
provider = "aws.us_dev"
ami = "$var.ami"
instance_type = "$var.instance_type"
key_name = "$var.instance_key_name"
vpc_security_group_ids = ["$aws_security_group.momentum_salt_master.id"]
subnet_id = "$element(var.vpc_subnet_ids, count.index)"
iam_instance_profile = "$var.iam_instance_profile"
user_data = "$element(data.template_file.saltmaster_userdata.*.rendered, count.index)"
tags
Name = "$element(data.template_file.saltmaster_userdata.*.vars.hostname, count.index)"
Environment = "$var.environment"
Application = "$var.application"
Role = "SaltMaster"
root_block_device
volume_type = "gp2"
volume_size = 40
delete_on_termination = true
output "private_ip"
value = "$aws_instance.momentum_salt_master.private_ip"
【问题讨论】:
你也可以展示你的saltmaster
模块吗?
你以前读过这个吗? github.com/hashicorp/terraform/issues/…
@ydaetskcoR 我已经用我的 saltmaster 模块的内容更新了我的问题。
【参考方案1】:
根据您使用的 terraform 版本,这可能已修复 here,但是,请尝试替换
"$data.template_file.momentum_cassandra.*.rendered[count.index]"
"$data.template_file.momentum_cassandra.*.vars.hostname[count.index]"
在cassandra.tf
和element
查找
"$element(data.template_file.momentum_cassandra.*.rendered, count.index)"
"$element(data.template_file.momentum_cassandra.*.vars.hostname, count.index)"
你在saltmaster.tf
中实际使用的
【讨论】:
以上是关于Terraform 模块依赖关系破坏了 template_file的主要内容,如果未能解决你的问题,请参考以下文章
当代码移动到模块时,Terraform 正在破坏 AWS 资源