【问题标题】:terraform use count index in module [duplicate]模块中的terraform使用计数索引[重复]
【发布时间】:2019-05-21 23:46:02
【问题描述】:

我想为我的 aws ec2 实例使用 terraform 模块中的 count.index 来按递增顺序命名实例

file: ec2/main.tf
 resource "aws_instance" "instance"{
    ami = "ami-xxx"
    tags {
     Name = "var.instance"
     }
    count = "var.count"

}

file: ec2instance.tf

module "ec2"{
 source = "./ec2"
 count = 3
 instance_name = "firsttypeinstance-${count.index+1}"
}

module "ec20"{
 source = "./ec2"
 count = 2
 instance_name = "secondtype-${count.index+1}"

}

我希望将实例名称填充为

firsttypeinstance-1 第一种实例-2 firsttypeinstance-3

secondtype-1 secondtype-2

但我得到了无法在模块中使用计数索引的错误

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    From terraform doc:

    除上述之外,参数名称 countfor_eachlifecycle 目前未被 Terraform 使用,但保留用于计划的未来功能。

    但是,您可以在模块中创建 my_count 变量并将其用于模块内的资源

    模块 ec2

    resource "aws_instance" "instance"{
        ami = "ami-xxx"
        tags {
            Name = "var.instance-${count.index}"
        }
        count = "var.my_count"
    }
    

    主模块

    module "ec2"{
       source = "./ec2"
       my_count = 3
       instance_name = "firsttypeinstance" ## actually instance prefix
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-10
      • 2021-06-26
      • 2021-09-08
      相关资源
      最近更新 更多