Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'At least one field is expected inside environment' error in terraform

When I tried to apply a terraform in order to create a lambda function, I got this error:

 Error: At least one field is expected inside environment

Here is my terraform module:

resource "aws_lambda_function" "lambda" {
  function_name = var.lambda_filename
  description = var.description
  runtime = "python3.6"
  environment {
    variables = var.variables
  }
}

This error is thrown when var.variables is set to null.

How can I fix it?

I am using terraform 0.12.6 and aws provider 2.25.0

like image 712
Anthony Kong Avatar asked Aug 28 '19 06:08

Anthony Kong


1 Answers

I find a solution: Use dynamic in the latest version of terrafrom

resource "aws_lambda_function" "lambda" {
  function_name = var.lambda_filename
  description = var.description
  runtime = "python3.6"

  dynamic "environment" {
    for_each = local.environment_map
    content {
      variables = environment.value
    }
  }
}

The environment_map is created this way:

locals {
  environment_map = var.variables == null ? [] : [var.variables]
}
like image 173
Anthony Kong Avatar answered Sep 18 '22 17:09

Anthony Kong