# Run lookup inside of templatefile

**URL:** <https://discuss.hashicorp.com/t/run-lookup-inside-of-templatefile/17196>\
**Category:** Terraform\
**Created:** [November 5, 2020, 12:36am UTC](https://discuss.hashicorp.com/t/run-lookup-inside-of-templatefile/17196 "2020-11-05T00:36:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![iamleet](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/iamleet/32/14316_2.png) [@iamleet](https://discuss.hashicorp.com/u/iamleet)\
**Post date:** [November 5, 2020, 12:36am UTC](https://discuss.hashicorp.com/t/run-lookup-inside-of-templatefile/17196/1 "2020-11-05T00:36:54Z")

</div>

Possible feature. I am managing secrets using SOPS and Terragrunt. I’d like to create a map in locals / inputs and pass this map to terraform with a bunch of account numbers and have look up pluck out the correct map for the templatefile function.

Is this something that might be possible for a future feature or just something not really possible.

Reasoning is I use maps and for\_each for creating resources and make associations to dependencies using lookup and a key name that match outputs from modules. The arch makes it so I don’t have to redefine modules but without the possibility of lookup inside of the templatefile I am forced to redefine a module to pass it a variable with the secrets per instance. (i’d end of with 3 modules instead of 1 in my main.tf - wack)

```auto
policy = templatefile("../${path.root}/policies/bucketPolicies/${each.key}.json", lookup( var.accounts, each.value.accounts, "account not found") )

module "bucket-policies" {
    source = "../modules/bucketPolicies"
    globals = var.globals
    bucket-policies = var.bucket-policies
}

variable "bu-bucket-policies" {
    type = map
    default = {
         bu-policy = {
            provider = "aws"
            bucket = "cbsi-bu-dpt-sftp"
            accounts = "bu" <-- look up would use bu to link to secret
        }
    }
}

```

---

<div class="post-metadata">

**Author:** ![atldevops](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/atldevops/32/17218_2.png) [@atldevops](https://discuss.hashicorp.com/u/atldevops)\
**Post date:** [February 15, 2021, 3:20pm UTC](https://discuss.hashicorp.com/t/run-lookup-inside-of-templatefile/17196/2 "2021-02-15T15:20:30Z")

</div>

Did you ever figure this out? I am having a similar issue with using lookup the way you do.

---

<div class="post-metadata">

**Author:** ![iamleet](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/iamleet/32/14316_2.png) [@iamleet](https://discuss.hashicorp.com/u/iamleet)\
**Post date:** [February 15, 2021, 4:15pm UTC](https://discuss.hashicorp.com/t/run-lookup-inside-of-templatefile/17196/3 "2021-02-15T16:15:02Z")

</div>

I was able to pass the policies by encrypting them in their entirety and passing it as a string/input value to Terraform. From there I used ToMap() to generate a map I can run lookup on. 😉 (it’s ugly but it works)
