'At least one field is expected inside environment' terraform 错误

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

当我尝试应用 terraform 来创建 lambda 函数时,出现了这个错误:

 Error: At least one field is expected inside environment

这是我的地形模块:

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

var.variables 设置为 null 时抛出此错误。

我该如何解决?

我正在使用 terraform 0.12.6 和 aws provider 2.25.0

我找到了解决方法:在最新版本的terraform

中使用dynamic
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
    }
  }
}

environment_map 是这样创建的:

locals {
  environment_map = var.variables == null ? [] : [var.variables]
}