简体   繁体   English

Terraform:使用模块循环输出变量

[英]Terraform: Loop over output variable using modules

I'm starting with terraform and I'm building a small project, just some aws instances which one with an ebs volume attached. 我从terraform开始,我正在构建一个小项目,只是一些附加了ebs卷的aws实例。

I believe I'll start to work with terraform more, so I want to do the work by defining modules that I can then reuse in other projects. 我相信我会开始更多地使用terraform,所以我想通过定义我可以在其他项目中重用的模块来完成工作。

For that, I created 3 modules (one for instances, another to create ebs volumes and the last one to attach the volumes). 为此,我创建了3个模块(一个用于实例,另一个用于创建ebs卷,最后一个用于附加卷)。

On other place, I have a "main.tf" in which I call those modules. 在其他地方,我有一个“main.tf”,我在其中称这些模块。 The problem I'm having is that to create and attach volumes I need some data like the instanceID. 我遇到的问题是创建和附加卷我需要一些数据,如instanceID。

To get the InstanceID I defined an output variable on the "instance" module (which has to be a list in case the instance count is more than one): 要获取InstanceID,我在“instance”模块上定义了一个输出变量(如果实例计数多于一个,则必须是一个列表):

output "instance_id" {
  value = ["${aws_instance.instance.*.id}"]
}

Then, on my main.tf file I call the variable on the module: 然后,在我的main.tf文件中,我调用模块上的变量:

module "aws-instance" {
  source = "../../Terraform/aws-instance"
  instance_type = "t2.micro"
  instance_count = "2"
}

(some other code...)

module "aws-volume-attachment" {
  source = "../../Terraform/aws-volume-attachment"
  device_name = "/dev/sdf"
  instance_id = "${element("${module.aws-instance.instance_id}", count.index)}"
  volume_id = "${element("${module.aws-ebs-volume.volume_id}", count.index)}"
}

But I get an error: 但是我收到一个错误:

Error: module "aws-volume-attachment": count variables are only valid within resources

My question is, how can I loop over the variable so that I can attach each volume to one instance ? 我的问题是,如何循环变量以便我可以将每个卷附加到一个实例?

"Count" is not supported when declaring Terraform modules. 声明Terraform模块时不支持 “Count”。 You have 2 options: 你有2个选择:

  1. Declare module for each instance and for each ebs volume. 为每个实例和每个ebs卷声明模块。 And pass variables into module without "count.index" part. 并将变量传递给模块而不使用“count.index”部分。

  2. Put Both EC2 and ESB in the same module, as they can't really work without each other. 将EC2和ESB放在同一个模块中,因为它们彼此无法正常工作。 This a good practice for structuring terraform projects - to make modules for entire application stacks. 这是构建terraform项目的一种很好的做法 - 为整个应用程序堆栈制作模块。 Because when you have too many small modules - it becomes a bit messy. 因为当你有太多小模块时 - 它变得有点乱。

Count is not supported in modules, only in resources. 模块不支持Count,仅在资源中支持。 In your instance you would want to pass the value of instance_count to the instance, volume and attachment modules. 在您的实例中,您可能希望将instance_count的值传递给实例,卷和附件模块。 You would then use instance_count for the value of count within the modules (at the resource level). 然后,您将使用instance_count作为模块内的count值(在资源级别)。

If it is using count , your aws-instance module will output a "list" of instance_ids . 如果使用count ,您的aws-instance module将输出的“名单” instance_ids The same would go for the volumes. 卷也是如此。

Pass these lists into the aws-volume-attachment module ( instance_id and volume_id variables must be of type list ) then iterate over these using count.index . 将这些列表传递到aws-volume-attachment模块( instance_idvolume_id变量必须是类型list ),然后使用count.index迭代这些count.index

Excuse the resource name, I'm not familiar with AWS provider as I work with Azure, but hopefully this shows the principal.: 请原谅资源名称,我不熟悉AWS提供程序,因为我使用Azure,但希望这显示主体:

resource "aws-volume-attachment" {
  count = "${var.instance_count}"
  ...

  volume_id = "${var.volume_ids[count.index]}"
  instance_id = "${var.instance_ids[count.index])"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM