# Assigning values to variables dynamically

**URL:** <https://discuss.hashicorp.com/t/assigning-values-to-variables-dynamically/39523>\
**Category:** Terraform\
**Created:** [May 15, 2022, 11:03pm UTC](https://discuss.hashicorp.com/t/assigning-values-to-variables-dynamically/39523 "2022-05-15T23:03:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![geek876](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/geek876/32/17690_2.png) [@geek876](https://discuss.hashicorp.com/u/geek876)\
**Post date:** [May 15, 2022, 11:03pm UTC](https://discuss.hashicorp.com/t/assigning-values-to-variables-dynamically/39523/1 "2022-05-15T23:03:23Z")

</div>

I have a use case where by i have several objects like

```auto
object1 = {
   some-thing-1-1 = {
   }
   some-thing-1-2 = {
   }
   ...
   ...
}

```

```auto
object2 = {
   some-thing-2-1 = {
   }
   some-thing-2-2 = {
   }
   ...
   ...
}

```

I want to dynamically set a variable `object` depending on another variable `object_name`. So for example, if `object_name = object1`, the value assigned to variable `object` should be content of `object1`

```auto
object = {
   some-thing-1-1 = {
   }
   some-thing-1-2 = {
   }
   ...
   ...

```

If, however, the value of `object_name = object2`, then the value assigned to variable `object` should be content of `object2` and so on.

```auto
object = {
   some-thing-2-1 = {
   }
   some-thing-2-2 = {
   }
   ...
   ...

```

---

<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:** [May 16, 2022, 7:10am UTC](https://discuss.hashicorp.com/t/assigning-values-to-variables-dynamically/39523/2 "2022-05-16T07:10:10Z")

</div>

This is not possible in Terraform.

[https://www.terraform.io/language/expressions/references](https://www.terraform.io/language/expressions/references):

> Although many of these names use dot-separated paths that resemble [attribute notation](https://www.terraform.io/language/expressions/types#indices-and-attributes) for elements of object values, they are not implemented as real objects. This means you must use them exactly as written

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [May 16, 2022, 5:56pm UTC](https://discuss.hashicorp.com/t/assigning-values-to-variables-dynamically/39523/3 "2022-05-16T17:56:09Z")

</div>

If you want to look up a value dynamically based on another variable, the typical answer would be to construct a map whose keys are values that other variable might have.

For example, if you have a variable `var.object_name` as you described then you can construct a map in a local value whose keys are the different values that variable might take, like this:

```auto
locals {
  objects = {
    object1 = {
      some-thing-1-1 = {
      }
      some-thing-1-2 = {
      }
    }
    object2 = {
      some-thing-2-1 = {
      }
      some-thing-2-2 = {
      }
    }
  }

  object = local.objects[var.object_name]
}

```

With the above definition, `local.object` will refer to whichever of the objects in `local.objects` the `var.object_name` value refers to, or return an error if the given name isn’t one of the expected values.

The Terraform language uses static global definitions rather than dynamic ones, so this isn’t a language where you can dynamically manipulate the global scope as you might be accustomed to from more dynamic languages like Python or JavaScript. If you want to look up values dynamically like this then you can do so by constructing the appropriate data structure to do so, which in this case was a mapping since the dynamic key is a string.
