2017-02-28 57 views
3

不知道這是否可能,但我有一個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錯誤

出如何使這項工作現在

回答

1

思路似乎是一個缺少的功能:

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資源,它會很快變得難看,所以如果您可以避免它,我不會太頻繁地使用它。