Home > Net >  pass list(strings) as a terraform variable
pass list(strings) as a terraform variable

Time:07-20

While defining my module, I do this:

module "activate_alarms" {
  source = "../_modules/aws/.."
  config = module.config
  alarm_arns = ["arn:aws:cloudwatch:eu-central-123:test-alarm"]
}
variable "alarm_arns" {
  type = list(string)
}
resource "aws_lambda_function" "processing_lambda" {
  filename         = data.archive_file.lambda_zip.output_path
  function_name    = local.processing_lambda_name
  handler          = "enable_disable_alarms.lambda_handler"
  source_code_hash = data.archive_file.lambda_zip.output_base64sha256
  role             = aws_iam_role.lambda_role.arn

  runtime = "python3.9"
  tags = var.config.tags
  environment {
    variables = {
      alarm_arns = local.alarm_arns
    }
  }
}

However, I get an error:

 Error: Incorrect attribute value type
│ 
│   on ../_modules/aws/enable_disable_alarms/processing_lambda.tf line 20, in resource "aws_lambda_function" "processing_lambda":
│   20:     variables = {
│   23:       alarm_arns = local.alarm_arns
│   30:     }
│     ├────────────────
│     │ local.alarm_arns is a list of string, known only after apply
│ 
│ Inappropriate value for attribute "variables": element "alarm_arns": string required.

What's the correct way to pass a list of strings?

Edit:

locals {
  config = var.config

  alarm_arns = "${var.alarm_arns}"
}

CodePudding user response:

You can't pass list(string) directly as environment into lambda. You have to make it into string first, e.g.:

  environment {
    variables = {
      alarm_arns = jsonencode(var.alarm_arns)
    }
  }

Then in your lambda you would have to convert it back to list of strings according to your programming language.

  • Related