Warm tip: This article is reproduced from serverfault.com, please click

Terraform does not work input and environment variables

发布于 2020-11-30 07:25:35

I have a glue job in aws. I made a loop.

In variables.tf

variable "list_of_jobs" {
  type = list(string)
  default = ["myjob1","myjob2","myjob3"]
}

In glue.tf

resource "aws_glue_job" "this" {
  for_each = toset(var.list_of_jobs)
  name     = each.value
  role_arn = var.role_arn

  command {
    name = "pythonshell"
    python_version = 3
    script_location = "s3://mybucket/${each.value}/run.py"
  }
}

In main.tf

variable "region" {}
variable "list_of_jobs" {}

module "my_glue" {
  source = "../terraform-glue"
  region = var.region
  list_of_jobs = var.list_of_jobs
}

This loop works fine, and I have 3 glue jobs after execution of terraform apply. The problem, when I am trying to make:

export TF_VAR_list_of_jobs='["myjob1","myjob2","myjob3"]'

In this case, when I am making terraform apply, I am receiving this:

Error: Invalid function argument

  on ../terraform-glue/glue.tf line 2, in resource "aws_glue_job" "this":
   2:   for_each = toset(var.list_of_jobs)
    |----------------
    | var.list_of_jobsis "[\"myjob1\",\"myjob2\",\"myjob3\"]"

Invalid value for "v" parameter: cannot convert string to set of any single
type.

Input variables, does not work too. Only variable from variables.tf. Could You help me please ? I am trying to resolve this during all night.

Questioner
Piduna
Viewed
0
Marcin 2020-11-30 16:13:18

It does not work because you need to provide type constrain for your complex variable if you want to pass it though env variables:

variable "list_of_jobs" {
  type    = list(string)
  default = ["myjob1","myjob2","myjob3"]
}