Gary Leong
Gary Leong

Reputation: 215

Set resource if not null

I'm sure it's simple, but is there a way to set a Terraform resource variable ONLY if not null? I used the tertiary operator, but lambda function resource does not accept "null" or "" as an acceptable value. So I only want to set the variable if is not null.

Here is my Terraform template:

resource "aws_lambda_function" "default" {
  function_name                  = var.lambda_name
  layers                         = var.lambda_layers != null ? split(",",var.lambda_layers) : null
  memory_size                    = var.memory_size
  timeout                        = var.lambda_timeout
  s3_bucket                      = var.s3_bucket
  s3_key                         = var.s3_key
  role                           = aws_iam_role.lambda.arn
  handler                        = var.handler
  runtime                        = var.runtime
  publish                        = "true"
  depends_on                     = [aws_iam_role_policy_attachment.default]
    
}

Upvotes: 0

Views: 6203

Answers (1)

Mark B
Mark B

Reputation: 200476

The layers parameter expects a list. You just need to pass it an empty list:

layers = var.lambda_layers != null ? split(",",var.lambda_layers) : []

Upvotes: 3

Related Questions