0
votes

Upgraded to terraform v0.12.2 and most of the things stopped working straight way. (my bad, I didn't check this page: https://www.terraform.io/upgrade-guides/0-12.html prior to upgrade)

Anyway, fixed most of the things but the variable interpolation of type map still not working. in pre-v012.x, this would work perfectly:

security_groups.tf

// SecurityGroup: default access
resource "aws_security_group" "default" {
  count        = "${length(var.s_zones)}"
  vpc_id       = "${element(aws_vpc.vpcs.*.id, count.index)}"
  name         = "${var.vpc_names[count.index]}-default"

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["${var.vpn_cidrs["lan"]}",
                   "${var.vpn_cidrs["ovp"]}"]
    description = "SSH from vpn"
  }
}

vars.tf

// Variable: vpn_cidrs
variable "vpn_cidrs" {
  type = "map"
  default = {
    lan = ["10.0.11.0/28", "10.0.12.0/28"]
    ovp = ["10.1.13.0/28", "10.1.14.0/28"]
  }
}

With v0.12.2, it returns:

Error: Incorrect attribute value type

on ../../modules/vpc/security_groups.tf line 55, in resource "aws_security_group" "default": 55: cidr_blocks = ["${var.vpn_cidrs["lan"]}",

Inappropriate value for attribute "cidr_blocks": element 0: string required.

I tried using the new constructor:

// Variable: vpn_cidrs
variable "vpn_cidrs" {
  type = map(string)
  default = {
    lan1 = "10.0.11.0/28",
    lan2 = "10.0.12.0/28",
    ovp1 = "10.1.13.0/28",
    ovp2 = "10.1.14.0/28",
  }
}

But then I cannot figure out how to use that for SG rules in the module. Anyone knows what am I missing or how to fix that? Really stuck since Friday evening. Any pointed, example-code would be highly appreciated.

-S

1
nope, it doesn't work, simply just removing the [] either from the variable: vpn_cidrs or the ingress{} rule. Already tried that. Unless I'm terribly missing something here.MacUsers

1 Answers

1
votes

"${var.vpn_cidrs["lan"]}" returns the list ["10.0.11.0/28", "10.0.12.0/28"], but a string is required as the error message states.

Try using "${join(", ", var.vpn_cidrs["lan"])}" to generate the string 10.0.11.0/28, 10.0.12.0/28.

To generate a combined list of the two lists vpn_cidrs["lan"] and vpn_cidrs["ovp"], use the concat() function (see here).

cidr_blocks = "${concat(var.vpn_cidrs["lan"], var.vpn_cidrs["ovp"])}"

This generates ["10.0.11.0/28", "10.0.12.0/28", "10.1.13.0/28", "10.1.14.0/28"]