Terraform v0.11.8
+ provider.aws v1.36.0
+ provider.null v1.0.0
variable "global_secondary_index" {
type = "list"
default = []
}
data "null_data_source" "global_secondary_index" {
count = "${length(var.global_secondary_index)}"
inputs = {
name = "${lookup(var.global_secondary_index[count.index], "name")}"
write_capacity = "${var.write_capacity}"
read_capacity = "${var.read_capacity}"
hash_key = "${lookup(var.global_secondary_index[count.index], "hash_key")}"
projection_type = "ALL"
}
}
resource "aws_dynamodb_table" "table" {
name = "${var.name}${var.environment}"
hash_key = "${var.hash_key}"
range_key = "${var.range_key}"
write_capacity = "${var.write_capacity}"
read_capacity = "${var.read_capacity}"
attribute = "${concat(list(map("name", var.hash_key, "type", "S")), var.attributes)}"
ttl = "${var.ttl}"
global_secondary_index = ["${data.null_data_source.global_secondary_index.*.outputs}"]
point_in_time_recovery {
enabled = true
}
stream_enabled = "${var.stream_enabled}"
stream_view_type = "${var.stream_enabled ? "NEW_AND_OLD_IMAGES" : ""}"
}
https://gist.github.com/EgidioCaprino/f89af0465287f53c6992c4d50131d8a5
Error: module.users_table.aws_dynamodb_table.table: "global_secondary_index.0.hash_key": required field is not set
Error: module.users_table.aws_dynamodb_table.table: "global_secondary_index.0.name": required field is not set
Error: module.users_table.aws_dynamodb_table.table: "global_secondary_index.0.projection_type": required field is not set
Error: module.users_table.aws_dynamodb_table.table: "global_secondary_index.0.read_capacity": required field is not set
Error: module.users_table.aws_dynamodb_table.table: "global_secondary_index.0.write_capacity": required field is not set
global_secondary_index attribute to be populated with a valid list, specified in the `null_data_source_
Items in global_secondary_index do not have valid attributes, as specified in ther null_data_source
Hi @EgidioCaprino! Sorry this didn't work as expected.
The problem here is that global_secondary_index is a child block, not an attribute. Therefore you can't assign an expression to it like this. Terraform's error message here is incorrect in Terraform 0.11 because of some weird interactions in the implementation.
The forthcoming Terraform 0.12 will generate a proper error message here, and will also include a way to get the result I think you wanted:
# Forthcoming Terraform 0.12 syntax. (Some details may change before release)
variable "global_secondary_index" {
type = list(object({
name = string
hash_key = string
}))
default = []
}
resource "aws_dynamodb_table" "table" {
# ...
dynamic "global_secondary_index" {
for_each = var.global_secondary_index
content {
name = global_secondary_index.value.name
write_capacity = var.write_capacity
read_capacity = var.read_capacity
hash_key = global_secondary_index.value.hash_key
projection_type = "ALL"
}
}
# ...
}
This new special dynamic block type allows use of a list or map value to dynamically construct zero or more child blocks of a particular type. This specialized syntax allows Terraform to support all of the usual features of nested blocks, such as argument name/type validation, further levels of nested blocks, etc.
This feature is covered by #7034, so I'm going to close this one just to consolidate the discussion over there.
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.