gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
7.94k stars 966 forks source link

Issues with lists/sets in 0.19 #740

Closed wesleykonderla closed 5 years ago

wesleykonderla commented 5 years ago

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.

ekini commented 5 years ago

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)
}
wesleykonderla commented 5 years ago

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!

ekini commented 5 years ago

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

brikis98 commented 5 years ago

+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!

wesleykonderla commented 5 years ago

That definitely fixed the issue. Thanks guys!