scrow
scrow

Reputation: 447

Terraform v0.12 no longer supports blocks in .tfvars files

Upgrading from terraform 11 to 12 and it seems that having blocks is not supported anymore in tfvars files. We had different tfvars files for each environment and some variables such as subnets were different based on environment.

We used to do a lookup based on the account number. Our UAT.tfvars had the following:

 global_vpc_subnets {
   prod    = "subnet-96eb8ae1,subnet-d54cf78c"
   nonprod = "subnet-badfc9de,subnet-dac2d5ac,subnet-0afc716f0974da5bc"
 }

These nonprod subnets were only used in this one environment and we specified this tfvar file when applying. I don't think it's exactly necessary; it's just something that's done.

Does anyone know if this can there is a way around terraform breaking. As after I have upgraded I now get this error:

Unexpected "global_vpc_subnets" block

  on uat.tfvars line 50:
  50: global_vpc_subnets {

Blocks are not allowed here.

if not I have to clear out subnets and move resources around which will just take time

Upvotes: 0

Views: 2529

Answers (1)

Martin Atkins
Martin Atkins

Reputation: 74064

The correct way to write this will depend on what type is expected for this variable.

If this variable expects a list of maps then you can write an expression to produce that list of maps, like this:

global_vpc_subnets = [
  {
    prod    = "subnet-96eb8ae1,subnet-d54cf78c"
    nonprod = "subnet-badfc9de,subnet-dac2d5ac,subnet-0afc716f0974da5bc"
  }
]

If the variable just expects a single map then you should instead write the following:

global_vpc_subnets = {
  prod    = "subnet-96eb8ae1,subnet-d54cf78c"
  nonprod = "subnet-badfc9de,subnet-dac2d5ac,subnet-0afc716f0974da5bc"
}

Block syntax was never intentionally supported in .tfvars files, but it happened to work by accident in Terraform v0.11 and earlier due to ambguity in the underlying language parser. Terraform v0.12 and later requires that you write an expression that unamiguously produces a value of the desired type.


While working on this upgrade I'd suggest also taking the opportunity to make the type constrant of this variable be specific, using the new Terraform v0.12 type constraint syntax.

If you intend to accept a list of maps:

variable "global_vpc_subnets" {
  type = list(map(string))
}

If you intend to accept only a single map:

variable "global_vpc_subnets" {
  type = map(string)
}

Given that these comma-separated strings seem to be representing sets of subnet ID strings themselves, you might also consider avoiding the need to split the strings by using map(set(string)) instead, but probably best to save that until after you've completed your initial v0.12 upgrade to avoid changing too many things at the same time.

Upvotes: 3

Related Questions