Terragrunt: Issues with lists/sets in 0.19

Created on 12 Jun 2019  路  5Comments  路  Source: gruntwork-io/terragrunt

Howdy!

First off, thanks guys for all the effort in pushing 0.19 out soo fast. I've been following along and just finished moving some _working_ 0.12 TF code over to terragrunt but am encountering an issue with using functions or expressions on sets/lists.

I have encountered the following errors:

Error: Invalid function argument

  on local.tf line 26, in locals:
  24: 
  25: 
  26:     var.tgw_route_propagation_cidrs
  27: 
    |----------------
    | var.tgw_route_propagation_cidrs is "[\"10.0.0.0/8\",\"172.16.0.0/12\",\"192.168.0.0/16\"]"

Invalid value for "sets" parameter: a set or a list is required.


Error: Invalid index

  on tgwa.tf line 10, in resource "aws_ec2_transit_gateway_route_table_propagation" "tgw_propagation":
  10:   transit_gateway_route_table_id = var.tgw_route_table_ids_to_propagate[count.index]
    |----------------
    | count.index is 20
    | var.tgw_route_table_ids_to_propagate is "[\"tgw-rtb-xxxxxxxxxxxxxxxxx\"]"

This value does not have any indices.

Files referenced:

# tgwa.tf:0
resource "aws_ec2_transit_gateway_vpc_attachment" "this" {
  subnet_ids         = [module.vpc.public_subnets[0]]
  transit_gateway_id = var.tgw_id
  vpc_id             = module.vpc.vpc_id
}

resource "aws_ec2_transit_gateway_route_table_propagation" "tgw_propagation" {
  count = length(var.tgw_route_table_ids_to_propagate)
  transit_gateway_attachment_id  = aws_ec2_transit_gateway_vpc_attachment.this.id
  transit_gateway_route_table_id = var.tgw_route_table_ids_to_propagate[count.index]
}
# local.tf:24
locals {
  public_tgw_propagation_routes = setproduct(
    module.vpc.public_route_table_ids,
    var.tgw_route_propagation_cidrs
  )
}
# terragrunt.hcl
inputs = {
  tgw_route_propagation_cidrs = ["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]
  tgw_route_table_ids_to_propagate = ["tgw-rtb-xxxxxxxxxxxxxxxxx"]
}

_Note: The module.vpc being referenced is the terraform-aws-vpc module._

Without digging too deep into this, I suspect it might have to do with how the values are being interpolated. I've tried this with the new "for" expressions as well and encountered similar errors. I have tested the code with just terraform 0.12 prior to moving to terragrunt 0.19 and it worked, if that helps.

question

Most helpful comment

I think it's a side effect of converting input to env vars for terraform.

It's like double conversion: in inputs the variables have types, then they get converted to env vars like

TF_VAR_tgw_route_propagation_cidrs='["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]'

and then terraform converts them back assigning the right type, which I assume string by default.

You didn't show the variables declaration, but it appears, you just need to set the type in the variable declaration like so:

variable "tgw_route_propagation_cidrs" {
  type = set(string)
}

or

variable "tgw_route_propagation_cidrs" {
  type = list(string)
}

All 5 comments

I think it's a side effect of converting input to env vars for terraform.

It's like double conversion: in inputs the variables have types, then they get converted to env vars like

TF_VAR_tgw_route_propagation_cidrs='["10.0.0.0/8", "172.16.0.0/12", "192.168.0.0/16"]'

and then terraform converts them back assigning the right type, which I assume string by default.

You didn't show the variables declaration, but it appears, you just need to set the type in the variable declaration like so:

variable "tgw_route_propagation_cidrs" {
  type = set(string)
}

or

variable "tgw_route_propagation_cidrs" {
  type = list(string)
}

Makes sense. Based on my understanding of what you said, I guess it was working before without terragrunt because terraform was able to infer the correct type? I will double check tomorrow when I get back in the office that it works and I can close this issue. Thanks!

Yes, because if you have it set in a .tfvars file, then terraform correctly guesses the type.

+1 on @ekini's idea. We pass the variables using env vars, and Terraform most likely needs you to specify a type to parse those env vars correctly. Let us know if that works!

That definitely fixed the issue. Thanks guys!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Tensho picture Tensho  路  3Comments

geekifier picture geekifier  路  3Comments

antonbabenko picture antonbabenko  路  3Comments

GauntletWizard picture GauntletWizard  路  3Comments

jtai-omniex picture jtai-omniex  路  3Comments