Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Terraform: how to read list of maps?

Tags:

terraform

See the example below:

data "aws_kms_secrets" "api_key" {
  count = "${length(keys(var.keys))}"

  secret {
    name    = "secret_name"
    payload = "${element(values(var.keys), count.index)}"
  }
}

resource "aws_api_gateway_api_key" "access_key" {
  count = "${length(keys(var.keys))}"

  name  = "${var.environment}-${element(keys(var.keys), count.index)}"
  value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}"
}

It appears to be impossible to look up the plaintext values from the data resource.

value = "${lookup(element(data.aws_kms_secrets.api_key.*.plaintext, count.index), "secret_name")}"

Results in lookup: argument 1 should be type map, got type string in:

I have tried many combinations of element,lookup,*, and dictionary syntax nothing works.

my var.keys looks like:

keys = {
  key-name-one = "sssss"
  key-name-two = "sss"
}
like image 774
Rick Burgess Avatar asked Jul 26 '18 16:07

Rick Burgess


1 Answers

The trick here is to use the dictionary syntax to replace the element call, it behaves better with lists of maps.

value = "${lookup(data.aws_kms_secrets.api_key.*.plaintext[count.index], "secret_name")}"

its tempting to do data.aws_kms_secrets.api_key[count.index].plaintext that isn't valid HCL

like image 80
Rick Burgess Avatar answered Nov 15 '22 11:11

Rick Burgess