terraform模块中的可选映射变量

时间:2017-02-28 00:10:38

标签: amazon-dynamodb terraform

不确定这是否可行,但我有一个DynamoDb表的模块,我想让global_secondary_index属性可选,但我无法弄清楚如何去做。

我有以下模块

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错误
  • 仅使用var global_secondary_index = ["${var.global_secondary_index}"] get 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错误

现在有关如何使这项工作的想法

1 个答案:

答案 0 :(得分:2)

似乎是一个缺少的功能:

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资源,那很快就会变得难看,所以如果你能避免它,我就不会经常使用它。