Skip to content

Using generic CI/CD tools for your IaC automation? πŸ€–βš™οΈ

Download the Build vs Buy Guide→

Storing Complex VariablesΒ»

Terraform supports a variety of variable types such as string, number, list, bool, and map. The full list of Terraform's variable types can be found in the Terraform documentation here.

When using "complex" variable types with Spacelift such as map and list you'll need to utilize Terraform's jsonencode function when storing these variables as an environment variable in your Spacelift Stack environment or context.

Usage ExampleΒ»

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
locals {
  map = {
    foo = "bar"
  }
  list = ["this", "is", "a", "list"]
}

resource "spacelift_context" "example" {
  description = "Example of storing complex variable types"
  name        = "Terraform Complex Variable Types Example"
}

resource "spacelift_environment_variable" "map_example" {
  context_id = spacelift_context.example.id
  name       = "map_example"
  value      = jsonencode(local.map)
  write_only = false
}

resource "spacelift_environment_variable" "list_example" {
  context_id = spacelift_context.example.id
  name       = "list_example"
  value      = jsonencode(local.list)
  write_only = false
}

Notice the use of the jsonencode function when storing these complex variable types. This will allow you to successfully store these variable types within Spacelift.

Consuming Stored VariablesΒ»

When consuming complex variable types in your environment, there is no need to use the jsondecode() function.