简体   繁体   中英

Terrafrom. Data, archive_file error or bug?

I've a problem with archive_file data source. I've a condition which is creates a lambda layer:

resource "aws_lambda_layer_version" "layers" {
   for_each = { for layers, name in local.lambdas.options.layers : layers => name if local.lambdas.init.self && local.lambdas.extras.layers }
   filename = "not_working" #data.archive_file.layers[each.key].output_path
   source_code_hash = "not_working" #data.archive_file.layers[each.key].output_base64sha256
   
   ...
}

Resource "aws_lambda_layer_version" "layers" working as expected, and creates a lambda layer, if filename and source_code_hash are defined as shown above (Without using data source).

And I have a

data "archive_file"  "layers"  {
   for_each = { for layers, name in local.lambdas.options.layers : layers => name if local.lambdas.init.self && local.lambdas.extras.layers }
   type = lookup(each.value.archive, "type", null)
   
   ...
}

As you can see, the for_each block are same with resource block, but I'v got an error:

each.value is object with 2 attributes

Finally here is my module block, where I call this resources

layers  = [
   {
      name        = "test-layer"
      runtimes    = [ "nodejs12.x" ]
      archive     = {
         type        = "zip"
      
         ...
      }
   }
]

So my question is, why same for_each block working differently?

I've reproduced your code, for me it is working;

resource "null_resource" "cmd" {
   for_each        = { for lambda, name in local.lambdas.init.mods : lambda => name if local.lambdas.init.self && local.lambdas.trigger.laps }
   provisioner "local-exec" {
       command     = lookup(each.value.archive, "command", null)
       working_dir = lookup(each.value.archive, "modules", null)
   }
}
data "archive_file"  "layers"  {
   for_each        = { for lambda, name in local.lambdas.init.mods : lambda => name if local.lambdas.init.self && local.lambdas.trigger.laps }
   depends_on      = [ null_resource.cmd ]
   type            = lookup(each.value.archive,    "type",    null)
   source_dir      = lookup(each.value.archive,    "modules", null)
   output_path     = "${lookup(each.value.archive, "outputs", null)}/${lookup(each.value, "function", null)}.${lookup(each.value.archive, "type", null)}"
   output_file_mode  = lookup(each.value.archive,  "mode",    null)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM