Terraform 的 random_shuffle 提供程序的奇怪行为 [英] Weird behavior of Terraform's random_shuffle provider
问题描述
我有以下配置:
variable "private_subnets" {
default = ["subnet-A", "subnet-B"]
}
resource "random_shuffle" "az" {
input = ["${var.private_subnets}"]
result_count = 1
}
module "server" {
source = "./modules/aws-ec2"
instance_count = 3
name = "${var.env}-server"
stack = "server"
role = "server"
ami = "${lookup(var.aws_amis, var.aws_region, "")}"
instance_type = "t2.micro"
subnet_id = "${random_shuffle.az.result[0]}"
vpc_security_group_ids = ["${var.security_groups}"]
}
我原以为 Terraform 会为每个实例随机选择一个 subnet_id
,但它总是选择列表中的第一项,即 subnet-A.根据要创建的实例数从列表中随机选择项目的最佳/建议方法是什么?
I was expecting Terraform would selected a random subnet_id
for every instance but it's always selecting the first item in the list which is subnet-A. What is the best/suggested way of randomly selecting an item off a list based on the number of instances to be created?
提前致谢!
推荐答案
random_shuffle
像大多数 Terraform 资源一样接受 count
参数,所以如果你这样做:
random_shuffle
like most Terraform resources accepts the count
parameter so if you do this like this:
resource "random_shuffle" "az" {
input = ["${var.private_subnets}"]
result_count = 1
count = 3
}
它将为您提供 3 个阵列,每个阵列有 1 个子网.访问它取决于您在模块中创建实例的确切方式,但大多数情况下您在 aws_instance 资源
上使用计数,因此它将类似于:
It will give you 3 arrays with 1 subnet each. Accessing it will depend on how exactly you create the instances inside your module but most you're using count on aws_instance resource
so it will be something like:
resource "aws_instance" "foo" {
count = 3
subnet_id = ${element(random_shuffle.az, count.index)}
}
这篇关于Terraform 的 random_shuffle 提供程序的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!