【问题标题】:Terraform using count.index in a resource nameTerraform 在资源名称中使用 count.index
【发布时间】:2019-01-23 18:04:58
【问题描述】:

使用 terraform 我尝试使用 count.index 将计数包含在我的资源名称中,但无法显示计数。我基本上是想将计数添加到资源名称中,以便可以找到资源,否则资源是未知的。

count = 3    
autoscaling_group_name = "${aws_autoscaling_group.exampleautoscaling-("count.index")-example.name}"

错误

resource variables must be three parts: TYPE.NAME.ATTR in:

expected is : exampleautoscaling-1-example.name,exampleautoscaling-2-example.name,exampleautoscaling-3-example.name

【问题讨论】:

    标签: terraform


    【解决方案1】:

    我的建议是添加标签并使用 name_prefix 参数。但具体到你的问题

    以下是文档中的一些 sn-ps,您可以尝试一下

    "${var.hostnames[count.index]}"
    

    resource "aws_instance" "web" {
      # ...
    
      count = "${var.count}"
    
      # Tag the instance with a counter starting at 1, ie. web-001
      tags {
        Name = "${format("web-%03d", count.index + 1)}"
      }
    }
    

    提供链接here。查看“数学”部分。

    【讨论】:

    【解决方案2】:

    您的语法不正确。您正在尝试将 count 插入资源名称的中间。您需要将其更改为以下内容:

    count = 3    
    autoscaling_group_name = "${aws_autoscaling_group.exampleautoscaling.name}-${count.index}"
    

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 2018-03-03
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 2015-04-28
      • 2021-08-06
      • 1970-01-01
      相关资源
      最近更新 更多