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
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]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With