# How to use the each.key value as a variable name

**URL:** <https://discuss.hashicorp.com/t/how-to-use-the-each-key-value-as-a-variable-name/32793>\
**Category:** Terraform\
**Created:** [December 6, 2021, 9:24pm UTC](https://discuss.hashicorp.com/t/how-to-use-the-each-key-value-as-a-variable-name/32793 "2021-12-06T21:24:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![doug.rosen.c3fl](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/doug.rosen.c3fl/32/16616_2.png) [@doug.rosen.c3fl](https://discuss.hashicorp.com/u/doug.rosen.c3fl)\
**Post date:** [December 6, 2021, 9:24pm UTC](https://discuss.hashicorp.com/t/how-to-use-the-each-key-value-as-a-variable-name/32793/1 "2021-12-06T21:24:25Z")

</div>

I want to pass in a list of strings that are table names I’m going to create in a module. The table definitions will be in a file in the subfolder where the module is. So my module will look something like this:

```auto
module "parquet_tables" {
    source = "./create_table"
    table_names = ["tab1", "tab2"]
    providers = {
        aws 
  }
}

```

Then in a file in the subfolder I will have definitions in locals for each table:

```auto
locals {
  tab1 = {
    table_name = "table 1"
    table_description = ....
    s3_location = "s3://....
    database_name ="dbname"
.
. }
}

```

Then in my create\_table I want to iterate through the table\_names list and create each table with for\_each:

```auto
resource "aws_glue_catalog_table" "tablex" {
  provider = aws
  for_each = toset(var.table_names)
  name = local.${each.key}.table_name
  database_name = local.${each.key}.database_name

```

This doesn’t work of course - it tells me an attribute name is required after the “local.” How can I get that string value of each table name I pass in as a table name to reference the local value of the same name?

---

<div class="post-metadata">

**Author:** ![lodotek](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/lodotek/32/36094_2.png) [@lodotek](https://discuss.hashicorp.com/u/lodotek)\
**Post date:** [June 5, 2023, 7:35pm UTC](https://discuss.hashicorp.com/t/how-to-use-the-each-key-value-as-a-variable-name/32793/2 "2023-06-05T19:35:17Z")

</div>

Did you ever manage to figure this out?

---

<div class="post-metadata">

**Author:** ![maxb](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/maxb/32/26655_2.png) [@maxb](https://discuss.hashicorp.com/u/maxb)\
**Post date:** [June 6, 2023, 9:19am UTC](https://discuss.hashicorp.com/t/how-to-use-the-each-key-value-as-a-variable-name/32793/3 "2023-06-06T09:19:04Z")

</div>

It is impossible, Terraform doesn’t support dynamic access to arbitrary locals.

Though, if you refactored it so it was instead dynamic access to keys within a map/object value, that was itself stored in a **single** local, that could work.

---

<div class="post-metadata">

**Author:** ![marknl](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/marknl/32/34779_2.png) [@marknl](https://discuss.hashicorp.com/u/marknl)\
**Post date:** [June 6, 2023, 11:33am UTC](https://discuss.hashicorp.com/t/how-to-use-the-each-key-value-as-a-variable-name/32793/4 "2023-06-06T11:33:22Z")

</div>

You should be able to do something like this (not tested):

```auto
locals {
  tables = {
    "tab1" = {
      table_name = "tab1"
      table_description = "..."
      s3_location = "s3://..."
      database_name ="dbname"
    }
    "tab2" = {
      # ...
    }
  }
}

resource "aws_glue_catalog_table" "tablex" {
  provider = aws
  for_each = toset(var.table_names)
  name = local.tables[each.key].table_name
  database_name = local.tables[each.key].database_name
}

```
