terraform模块中的可选映射变量
问题描述:
不知道这是否可能,但我有一个DynamoDb表的模块,我想使global_secondary_index
属性为可选,但我无法弄清楚如何执行此操作。terraform模块中的可选映射变量
我有以下模块
resource "aws_dynamodb_table" "basic_dynamodb_table" {
name = "${var.table_name}"
read_capacity = "${var.read_capacity}"
write_capacity = "${var.write_capacity}"
hash_key = "${var.primary_key}"
attribute {
name = "${var.primary_key}"
type = "${var.primary_key_type}"
}
global_secondary_index = ["${var.global_secondary_index}"]
}
variable "table_name" {}
variable "read_capacity" {
default = "1"
}
variable "write_capacity" {
default = "1"
}
variable "primary_key" {}
variable "primary_key_type" {}
variable "global_secondary_index" {
default = {}
type = "map"
description = "This should be optional"
}
而且,它还将被用来
module "test-table" {
source = "./modules/DynamoDb"
table_name = "test-table"
primary_key = "Id"
primary_key_type = "S"
global_secondary_index = {
name = "by-secondary-id"
hash_key = "secondaryId"
range_key = "type"
projection_type = "INCLUDE"
non_key_attributes = [
"id"
]
write_capacity = 1
read_capacity = 1
}
}
我已经试过:
- 不使用
[]
周围的插值并获得global_secondary_index: should be a list
错误 - just usi纳克的VAR
global_secondary_index = ["${var.global_secondary_index}"]
得到global_secondary_index.0: expected object, got string
错误 - 条件,但显然不支持列表或地图
- 合并地图
global_secondary_index = ["${merge(var.global_secondary_index,map())}"]
也得到global_secondary_index.0: expected object, got string
错误
出如何使这项工作现在
答
思路似乎是一个缺少的功能:
https://github.com/hashicorp/terraform/issues/3388
就这一主题个变化(合格的地图列表中的资源):
https://github.com/hashicorp/terraform/issues/12294 https://github.com/hashicorp/terraform/issues/7705
最后一件事要注意,虽然,在条件语句就可以得到偷偷摸摸的那些。
resource "some_tf_resource" "meh" {
count = "${length(keys(var.my_map)) > 0 ? 1 : 0}"
# other resource settings here
}
使用count并将其设置为0是在早期版本中避开terraforms缺少条件的黑客方式。它仍然有效=)
尽管如果有其他资源依赖于meh
资源,它会很快变得难看,所以如果您可以避免它,我不会太频繁地使用它。