wassgren
wassgren

Reputation: 19201

Terraform lists in version 0.12+

I am upgrading from terraform 0.11.4 to 0.12.2. I have straightened out most of the stuff by following this guide but I can't seem to find a solution for the following list expression.

variable "subnetIds" {
  type = "list"
}

resource "aws_lb" "main" {
  name            = "myload-balancer"
  subnets         = ["${var.subnetIds}"] # <-- problem row
  security_groups = ["${var.securityGroupIds}"]
}

The error message is:

  on main.tf line 10, in resource "aws_lb" "main":
  10:   subnets         = ["${var.subnetIds}"]

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

If I remove the brackets [] I get the following error:

  on main.tf line 10, in resource "aws_lb" "main":
  10:   subnets         = "${var.subnetIds}"

Inappropriate value for attribute "subnets": incorrect set element type:
string required.

What is the correct way to express subnets for resource aws_lb using terraform 0.12+?

Upvotes: 1

Views: 2927

Answers (2)

Matthew Schuchard
Matthew Schuchard

Reputation: 28739

Although you do not show a typical value for var.securityGroupIds and var.subnetIds, I will assume they are both lists. In Terraform < 0.12, you were required sometimes to put redundant array notation for parameter arguments and did not have first class support for variables. In Terraform >= 0.12, you should no longer put redundant array notation and you do have first class support for variables. You can update your code to handle both via:

resource "aws_lb" "main" {
  name            = "myload-balancer"
  subnets         = var.subnetIds
  security_groups = var.securityGroupIds
}

Thus the redundant [] are removed and the unnecessary string interpolation is also removed.

Upvotes: 2

wassgren
wassgren

Reputation: 19201

Found the problem. The subnetIds were passed from the output of a previous layer (remote state).

subnetIds = ["${data.terraform_remote_state.eventsource.outputs.subnetIds}"]

Basically this means that the subnetIds variable became a list of lists.

After changing to this it all works:

subnetIds = data.terraform_remote_state.eventsource.outputs.subnetIds

Upvotes: 1

Related Questions