Search code examples
amazon-web-servicesaws-lambdaterraform

convert tuple of key value pairs to set Terraform


I'm trying to convert tuple of key value pairs to set to be sent to lambda function environment variables, something like the following:

[
    {
      "name"  = "some_name"
      "value" = "some_value"
    },
    {
      "name"  = "some_name"
      "value" = "some_value"
    }
]

now, let's say I could save the above into a variable and named it environmet_variables

I want to pass it lambda function, so I tried the following:

resource "aws_lambda_function" "lambda" {

  environment {
    variables = {
        for key, value in var.environment_variables :
        key => value
    }
  }
}

it's not working and I get the following error :

 var.environment_variables is tuple with 13 elements
│
│ Inappropriate value for attribute "variables": element "2": string required.


Solution

  • Try:

    { for variable in var.environment_variables : variable.name => variable.value }
    

    It works in a TF console:

    >> env-test.tf
    variable "environment_variables" {
      type = list
      default =  [
        {
          "name"  = "key1"
          "value" = "1"
        },
        {
          "name"  = "key2"
          "value" = "2"
        }
      ]
    }
    
    $ terraform console
    > { for variable in var.environment_variables : variable.name => variable.value }
    {
      "key1" = "1"
      "key2" = "2"
    }