Answers: <p>This could be done with a <code>dynamic</code> block but its pretty complicated. This is how I would do it but there are other ways.</p>
  <pre><code>variable &quot;enable_vars&quot; {
    type        = bool
    default     = false
  }
  
  locals {
    default_lambda_vars = {
      FOO = 1
    }
    extra_vars = {
      BAR = 2
      BAZ = 3
    }
  
    final_lambda_vars = var.enable_vars ? merge(local.default_lambda_vars, local.extra_vars) : local.default_lambda_vars
  }
  
  
  resource &quot;aws_lambda_function&quot; &quot;mylambda&quot; {
  
      #...
  
      environment {
          variables = local.final_lambda_vars
      }
  
  }
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2022-10-31T08:10:23.293Z
Answers: 2022-10-31T08:10:23.293Z
Answers: Levi
Body: <p>I have an <a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function" rel="nofollow noreferrer">aws_lambda_function</a> resource like below:</p>
  <pre><code>resource &quot;aws_lambda_function&quot; &quot;mylambda&quot; {
  
      #...
      
      environment {
          variables = {
              FOO = 1
          }
      }
  
  }
  </code></pre>
  <p>I'm tring to add some environment variables dynamically based on my <code>var.enable_vars</code></p>
  <pre><code>variable &quot;enable_vars&quot; {
    type        = bool
    default     = false
  }
  
  
  resource &quot;aws_lambda_function&quot; &quot;mylambda&quot; {
  
      #...
      
      environment {
          variables = {
              FOO = 1
      #### if var.enable_vars == true
      #       BAR = 2
      #       BAZ = 3
          }
      }
  
  }
  </code></pre>
  <p>How to achieve that? Is not clear to me if a <code>dynamic</code> block can be used there.</p>
  
Comments: CC BY-SA 4.0
Comments: 2022-10-19T16:26:12.197Z
Comments: You could set the environment variables in the code itself from a file conditionally based on one variable set during the build time
Comments: jabbson
Comments: CC BY-SA 4.0
Comments: 2022-10-19T18:57:03.573Z
Comments: @jabbson can you add more details?
Comments: sgargel
Comments: CC BY-SA 4.0
Comments: 2022-10-19T21:11:13.080Z
Comments: you could have your environment variables stored in the .env files, uploaded along with the code and then loaded during execution. Also you could use ternary operator, if setting BAR = 0 or -1 could mean the same as not being set within your app logic.
Comments: jabbson
Tags: <terraform><terraform-module>
Title: Terraform conditional block inside a map
UserDisplayName: sgargel
ThreadText: # Terraform conditional block inside a map
  
  (Asked by: username_0 on 2022-10-19)
  
  I have an [aws\_lambda\_function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_function) resource like below:
  
  ```
  resource "aws_lambda_function" "mylambda" {
  
      #...
      
      environment {
          variables = {
              FOO = 1
          }
      }
  
  }
  
  ```
  
  I'm tring to add some environment variables dynamically based on my `var.enable_vars`
  
  ```
  variable "enable_vars" {
    type        = bool
    default     = false
  }
  
  resource "aws_lambda_function" "mylambda" {
  
      #...
      
      environment {
          variables = {
              FOO = 1
      #### if var.enable_vars == true
      #       BAR = 2
      #       BAZ = 3
          }
      }
  
  }
  
  ```
  
  How to achieve that? Is not clear to me if a `dynamic` block can be used there.
  
  **Tags:** terraform, terraform-module
  
  > **Comments:**
  > **username_1**: You could set the environment variables in the code itself from a file conditionally based on one variable set during the build time  
  > **username_0**: @username_1 can you add more details?  
  > **username_1**: you could have your environment variables stored in the .env files, uploaded along with the code and then loaded during execution. Also you could use ternary operator, if setting BAR = 0 or -1 could mean the same as not being set within your app logic.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2022-10-31. Score: 3 | Accepted answer
  
  This could be done with a `dynamic` block but its pretty complicated. This is how I would do it but there are other ways.
  
  ```
  variable "enable_vars" {
    type        = bool
    default     = false
  }
  
  locals {
    default_lambda_vars = {
      FOO = 1
    }
    extra_vars = {
      BAR = 2
      BAZ = 3
    }
  
    final_lambda_vars = var.enable_vars ? merge(local.default_lambda_vars, local.extra_vars) : local.default_lambda_vars
  }
  
  resource "aws_lambda_function" "mylambda" {
  
      #...
  
      environment {
          variables = local.final_lambda_vars
      }
  
  }
  
  ```
  
  
  ---
  
  