Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

* does not work as expected with null_data_source #18926

Closed
EgidioCaprino opened this issue Sep 22, 2018 · 3 comments
Closed

* does not work as expected with null_data_source #18926

EgidioCaprino opened this issue Sep 22, 2018 · 3 comments

Comments

@EgidioCaprino
Copy link

Terraform Version

Terraform v0.11.8
+ provider.aws v1.36.0
+ provider.null v1.0.0

Terraform Configuration Files

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" : ""}"
}

Debug Output

https://gist.github.com/EgidioCaprino/f89af0465287f53c6992c4d50131d8a5

Crash Output

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

Expected Behavior

global_secondary_index attribute to be populated with a valid list, specified in the `null_data_source_

Actual Behavior

Items in global_secondary_index do not have valid attributes, as specified in ther null_data_source

@apparentlymart
Copy link
Member

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.

@apparentlymart
Copy link
Member

This feature is covered by #7034, so I'm going to close this one just to consolidate the discussion over there.

@ghost
Copy link

ghost commented Apr 2, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants