Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count parameter throws error with split interpolation function and map variable #3884

Closed
Deserved opened this issue Nov 12, 2015 · 9 comments

Comments

@Deserved
Copy link

The snippet of code below will cause an error:

variable "api_public_subnets" {
  default = {
    cidrs = "a,b,c"
  }
}
resource "aws_subnet" "api_public_subnets" {
  ....
  count = "${length(split(",", var.api_public_subnets.cidrs))}"
  ....
}

Errors:

  • strconv.ParseInt: parsing "${length(split(",", var.api_public_subnets.cidrs))}": invalid syntax

If I use not mapping it will work as expected, snippet of code below will work:

variable "api_public_subnets" {
  default = "a,b,c"

}
resource "aws_subnet" "api_public_subnets" {
  ....
  count = "${length(split(",", var.api_public_subnets))}"
  ....
}

For instance this code will work as-well, despite it uses mapping:

variable "api_public_subnets" {
  default = {
    cidrs = "a,b,c"
  }
}
resource "aws_subnet" "api_public_subnets" {
  ....
  tags {
    Application = "${length(split(",", var.api_public_subnets.cidrs))}"
  }
  ....
}

Is it a bug?

@Deserved Deserved changed the title Count parameter works incorrectly with split interpolation function and map variable Count parameter throws error with split interpolation function and map variable Nov 12, 2015
@jen20
Copy link
Contributor

jen20 commented Nov 12, 2015

Hi @Deserved! Similarly to #3888, I can no longer reproduce this on master, using the following minimal reproduction:

provider "aws" {
  region = "us-west-2"
}

variable "api_public_subnets" {
  default = {
    cidrs = "a,b,c"
  }
}

resource "aws_subnet" "api_public_subnets" {
  cidr_block = "10.0.0.0/16"
  count      = "${length(split(",", var.api_public_subnets.cidrs))}"
}

Which version of Terraform are you using.

@Deserved
Copy link
Author

Hi @jen20 I am using 0.6.6

I've just checked, it is more than 300 commits since 0.6.6 released to master. So probably it was fixed. Could you please try with 0.6.6 and see if you can replicate, if not it means problem in somewhere else.

@jrnt30
Copy link
Contributor

jrnt30 commented Feb 11, 2016

@phinze @jen20 This is still occurring for me in 0.6.11 as well. As was alluded to in previous comments and in some of the related issues, it appears that this is related to the interpolation of a count parameter that is using a module's output.

I took the "hashicorp/best-practices" and extended it to provide a reproducible issue. Essentailly we were added an additional "peering" module which leverages outputs of the "public_subnets" and "private_subnets" modules. During the interpolation of the "count" parameter for the "aws_route" resource, we recieve the following * strconv.ParseInt: parsing "${length(split(\",\", var.pub_route_table_ids))}": invalid syntax

If I were to comment out the resource itself and take a look at the "interpolation_debug" output in the state, it is the appropriate value (and the plan proceeds as expected)

Diff: hashicorp/best-practices@master...jrnt30:GH-3884-Module-interpolation-issues

Erroring Line: hashicorp/best-practices@master...jrnt30:GH-3884-Module-interpolation-issues#diff-a40ed86341ef14520b793a2a198f68f6R31

Full debug log: https://gist.github.com/jrnt30/a55960ce3108639723bf

@jrnt30
Copy link
Contributor

jrnt30 commented Feb 11, 2016

As a note, I made an attempt at "pre-computing" the length value in the "parent" module and passing in a set of "count" variables. In doing so, ran across #2301

I can provide another branch for that derivation of this issue if it's helpful

@coolguydork
Copy link

Running into this issue also in Terraform v0.6.14

@norcalli
Copy link

norcalli commented May 9, 2016

And also on v0.6.15

@igor-pinchuk
Copy link

The same in Terraform v0.6.16

@mitchellh
Copy link
Contributor

This works on master now with 0.7:

variable "foo" {
  default = {
    values = "a,b,c"
  }
}

resource "null_resource" "foo" {
  count = "${length(split(",", var.foo["values"]))}"
}

I think this is good to close.

@ghost
Copy link

ghost commented Apr 22, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants