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

Syntax error when calculating count value #4334

Closed
conorgil opened this issue Dec 16, 2015 · 12 comments
Closed

Syntax error when calculating count value #4334

conorgil opened this issue Dec 16, 2015 · 12 comments

Comments

@conorgil
Copy link

I am creating a module to setup VPC peering between any two given VPCs. The module defines the following:

resource "aws_route" "vpc_to_peer_vpc" {
  count = "${length(split(",", var.vpc_route_table_ids))}"

  route_table_id = "${element(split(",", var.vpc_route_table_ids), count.index)}"
  destination_cidr_block = "${var.peer_vpc_cidr}"
  vpc_peering_connection_id = "${aws_vpc_peering_connection.main.id}"
}

resource "aws_route" "peer_vpc_to_vpc" {
  count = "${length(split(",", var.peer_vpc_route_table_ids))}"

  route_table_id = "${element(split(",", var.peer_vpc_route_table_ids), count.index)}"
  destination_cidr_block = "${var.vpc_cidr}"
  vpc_peering_connection_id = "${aws_vpc_peering_connection.main.id}"
}

When I run terraform plan, I get the following error:

There are warnings and/or errors related to your configuration. Please
fix these before continuing.

Errors:

  * strconv.ParseInt: parsing "${length(split(\",\", var.peer_vpc_route_table_ids))}": invalid syntax
  * strconv.ParseInt: parsing "${length(split(\",\", var.vpc_route_table_ids))}": invalid syntax

I cannot for the life of me figure out the syntax error (if there even is one). I checked the Interpolation docs and as far as I can tell, it follows the examples.

Is this just a problem because of count and a very poor error message? Any advice appreciated! Thanks.

@jen20
Copy link
Contributor

jen20 commented Dec 16, 2015

Hi @conorgil! Thanks for opening this issue - this looks like a bug to me. I have two further questions to be able to reproduce this:

  1. Which version of Terraform are you using?
  2. Do you have variable "vpc_route_table_ids" { } defined somewhere in your configuration?

@conorgil
Copy link
Author

@jen20

  1. Terraform v0.6.8
  2. Yes, I have the variable defined. Sorry I didn't include that, but I define all of the variables at the top of the file. See the entire module in this gist.

@phinze
Copy link
Contributor

phinze commented Dec 16, 2015

Hi @conorgil I just had a go at reproducing this, but everything seems to be working when I try out your gist.

I filled in some dummy values for your variables like so:

variable "vpc_id" {
  description = "The id of the VPC"
  default = "vpc-abc123"
}

variable "vpc_cidr" {
  description = "The CIDR block of the VPC"
  default = "10.0.1.0/24"
}

variable "vpc_route_table_ids" {
  description = "A comma separated string of the IDs of the route tables in the VPC which should be updated to have a route for the VPC peering connection."
  default = "rtb-abc123,rtb-def-456"
}

variable "peer_vpc_id" {
  description = "The CIDR block of the peer VPC."
  default = "vpc-cde234"
}

variable "peer_vpc_route_table_ids" {
  description = "A comma separated string of the IDs of the route tables in the peer VPC which should be updated to have a route for the VPC peering connection."
  default = "rtb-ghi678,rtb-jkl789"
}

variable "peer_vpc_cidr" {
  description = "The CIDR block of the peer VPC."
  default = "10.0.2.0/24"
}

variable "peer_owner_id" {
  description = "The AWS account ID which owns the peer VPC."
  default = "9724397343"
}

And my Terraform v0.6.8 produces a sane looking plan:

+ aws_route.peer_vpc_to_vpc.0
    destination_cidr_block:     "" => "10.0.1.0/24"
    destination_prefix_list_id: "" => "<computed>"
    instance_owner_id:          "" => "<computed>"
    origin:                     "" => "<computed>"
    route_table_id:             "" => "rtb-ghi678"
    state:                      "" => "<computed>"
    vpc_peering_connection_id:  "" => "${aws_vpc_peering_connection.main.id}"

+ aws_route.peer_vpc_to_vpc.1
    destination_cidr_block:     "" => "10.0.1.0/24"
    destination_prefix_list_id: "" => "<computed>"
    instance_owner_id:          "" => "<computed>"
    origin:                     "" => "<computed>"
    route_table_id:             "" => "rtb-jkl789"
    state:                      "" => "<computed>"
    vpc_peering_connection_id:  "" => "${aws_vpc_peering_connection.main.id}"

+ aws_route.vpc_to_peer_vpc.0
    destination_cidr_block:     "" => "10.0.2.0/24"
    destination_prefix_list_id: "" => "<computed>"
    instance_owner_id:          "" => "<computed>"
    origin:                     "" => "<computed>"
    route_table_id:             "" => "rtb-abc123"
    state:                      "" => "<computed>"
    vpc_peering_connection_id:  "" => "${aws_vpc_peering_connection.main.id}"

+ aws_route.vpc_to_peer_vpc.1
    destination_cidr_block:     "" => "10.0.2.0/24"
    destination_prefix_list_id: "" => "<computed>"
    instance_owner_id:          "" => "<computed>"
    origin:                     "" => "<computed>"
    route_table_id:             "" => "rtb-def-456"
    state:                      "" => "<computed>"
    vpc_peering_connection_id:  "" => "${aws_vpc_peering_connection.main.id}"

+ aws_vpc_peering_connection.main
    accept_status: "" => "<computed>"
    auto_accept:   "" => "1"
    peer_owner_id: "" => "9724397343"
    peer_vpc_id:   "" => "vpc-cde234"
    vpc_id:        "" => "vpc-abc123"

Do you see anything I might be missing here? If you fill in those same defaults do you get the same error?

@conorgil
Copy link
Author

@phinze I just tried your sample and it did indeed successfully produce a plan for me too. I'm trying to reproduce the error in a small generic example. I'll report back after some tinkering.

@conorgil
Copy link
Author

In case it adds context, when I was calling the vpc_peering module, I was grabbing values from several terraform_remote_state and calling it like:

module "vpc_peering_common_vpc_and_staging_vpc" {
  source = "../modules/vpc_peering"

  vpc_id = "${terraform_remote_state.common.output.vpc_id}"
  vpc_cidr = "${terraform_remote_state.common.output.vpc_cidr}"
  vpc_route_table_ids = "${format("%s,%s", terraform_remote_state.common.output.public_subnet_route_table_ids, terraform_remote_state.common.output.private_subnet_route_table_ids)}"
  peer_vpc_id = "${terraform_remote_state.staging.output.vpc_id}"
  peer_vpc_route_table_ids = "${format("%s,%s", terraform_remote_state.staging.output.public_subnet_route_table_ids, terraform_remote_state.staging.output.private_subnet_route_table_ids)}"
  peer_vpc_cidr = "${terraform_remote_state.staging.output.vpc_cidr}"
  peer_owner_id = "9724397343"
}

Initially, I thought that there might be a problem if the vpc_route_table_ids or peer_vpc_route_table_ids variables had only a single item (essentially, no comma in the string), but it blew up with the syntax error on a case where I know there were 5 route table ids in the string.

My workaround was to modify the vpc_peering module to take 2 new params vpc_route_table_count and peer_vpc_route_table_count, which I hardcode for the time being. Clearly, that sucks, but it works and I think that demonstrates that the value of the vpc_route_table_ids string is correctly formatted since it it correctly iterated over.

@phinze
Copy link
Contributor

phinze commented Dec 16, 2015

@conorgil ah yes, that additional context helped. It is a limitation of count currently that only variable interpolations are allowed. Apparently we're not properly raising the appropriate error message in this particular case.

You should be getting an error something like this:

aws_route.peer_vpc_to_vpc: resource count can't reference resource variable: terraform_remote_state.common.output

We can look into why that error message isn't showing up here. As for the feature to expand the capability of count interpolations, I believe there is another thread you can track for that enhancement, though my searches are not turning it up at the moment. I'll link it here if I find it.

@conorgil
Copy link
Author

@phinze Dang. I was afraid of that :(

I think you're referring to #953, which I am also eagerly watching. I'll throw a party once the count module can correctly use interpolation 👍

Thanks for the prompt replies on this issue. Getting the error message updated would have made this much easier for me to blame on #953.

@tylerFowler
Copy link

+1 On this, randomly getting this error but if I keep running the command eventually it will accept it.

@Gary-Armstrong
Copy link

Seeing the strconv.ParseInt as well. After some experimentation, it seems to happen once per var.count interpolation with the plan failing/exiting at each occurrence. I can't really say each occurrence, unfortunately, since it still appears inconsistent, but it does resemble a pattern of value cache misses. I'd love some output of the var value as well as type (int, str, etc) in these case.

@alexander-balashov
Copy link

Sometimes I get same error on Jenkins CI
terraform 0.6.11

@mitchellh
Copy link
Contributor

This seems to be fixed with 0.7.x. Specifically the ParseInt issue.

I tried with null resources to ease repro:

variable "files" {
  default = "a,b,c"
}

resource "null_resource" "web" {
  count = "${length(split(",", var.files))}"
}

I did the following:

$ terraform apply
...

$ terraform taint null_resource.web.1
...

$ terraform plan
...

$ terraform apply
...

All worked as expected.

If you can get a repro still please open a new issue. Thanks!

@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.

@hashicorp hashicorp 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