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

strconv.ParseInt: parsing invalid syntax error for simple var #4738

Closed
jemmyw opened this issue Jan 19, 2016 · 8 comments
Closed

strconv.ParseInt: parsing invalid syntax error for simple var #4738

jemmyw opened this issue Jan 19, 2016 · 8 comments

Comments

@jemmyw
Copy link

jemmyw commented Jan 19, 2016

I have a variable defined as:

variable "live_stages" {
  description = "Stages not including development, i.e. production and staging"
  default = 2
}

So it is simple and not calculated or over-ridden. I'm using it for the count in a number of places. When I run terraform plan on my local computer it works fine. When I run it on my control machine I get the following error:

Error refreshing state: 2 error(s) occurred:

* Error reading aws_iam_role.staged-ecs-container-instance-role count: strconv.ParseInt: parsing "${var.live_stages}": invalid syntax
* Error reading aws_ecs_cluster.cluster count: strconv.ParseInt: parsing "${var.live_stages}": invalid syntax

This is odd for 2 reasons. First of all I'm using the same variable in other places in the same file just fine. If i replace the variable with a value it works ok. And secondly it runs fine on my local machine, and I'm using the same docker container in both places to run terraform.

I'm at my wits end with it really, I can't figure out what I should do to debug it.

Here is a non-working resource:

resource "aws_iam_role" "staged-ecs-container-instance-role" {
  count = "${var.live_stages}"
  name = "${lookup(var.stage-names, count.index)}-ecs-container-instance-role"
  assume_role_policy = "${template_file.ec2_assume_role_policy.rendered}"
}

and here is a working one in the same file!

resource "aws_iam_instance_profile" "staged-ecs-instance-profile" {
  count = "${var.live_stages}"
  name = "${lookup(var.stage-names, count.index)}-ecs-instance-profile"
  path = "/"
  roles = ["${element(aws_iam_role.staged-ecs-container-instance-role.*.name, count.index)}"]
}
@jen20
Copy link
Contributor

jen20 commented Jan 19, 2016

Hi @jemmyw! I feel you're likely running into the issue of not currently being able to store lists in variables - this is a feature on our roadmap. In the absence of this, you'd need to do something like this:

variable "live_stages" {
    description = "Stages not including development, i.e. production and staging"
    default = 2
}

variable "stage_names" {
    description = "Names of stages"
    default = "stage1,stage2"
}

resource "aws_iam_role" "staged-ecs-container-instance-role" {
    count = "${var.live_stages}"
    name = "${lookup(split(",", var.stage_names), count.index)}-ecs-container-instance-role"
    assume_role_policy = "${template_file.ec2_assume_role_policy.rendered}"
}

If it is generated by another resource or the output from a module, you can construct such a list by using the join interpolation function.

I'll go ahead and close this issue for now as the underlying problem is tracked in #57. If you have further questions on this please feel free to reopen it!

@jen20 jen20 closed this as completed Jan 19, 2016
@jemmyw
Copy link
Author

jemmyw commented Jan 19, 2016

Hi @jen20, I can't really see how the stage names is related to the count = "${var.live_stages}" variable not working. The stage names are working fine, I'm using a map for them. In the above code it works fine if I replace with count = 2 which has nothing to do with the stage names at all!

@jemmyw
Copy link
Author

jemmyw commented Jan 19, 2016

@jen20 just to make sure, I did change my var.stage_names to be a comma delimited string, and the same error still occurs:

Error refreshing state: 2 error(s) occurred:

* Error reading aws_ecs_cluster.cluster count: strconv.ParseInt: parsing "${var.live_stages}": invalid syntax
* Error reading aws_iam_role.staged-ecs-container-instance-role count: strconv.ParseInt: parsing "${var.live_stages}": invalid syntax

And note that I still only get that error on my control machine running on EC2, it doesn't occur on my local machine, and if I change it from count = "${var.live_stages}" to count = 2 in those 2 resources then it works fine. But I don't need to change it in the other resources that use the same variable.... so what is special about those ones?

@jemmyw
Copy link
Author

jemmyw commented Jan 19, 2016

@jen20 I can tell you what is special about those 2 resources. An output attribute of those 2 is passed in to a module.

@gwilym
Copy link

gwilym commented Feb 3, 2016

Rather than a problem with your config syntax, I'd check that the var isn't somehow getting set to a value which can't be parsed to an int. I'm just not sure how.

See, I ran into a similar problem today where I had a reference like ...

resource "aws_security_group" "..." {
  count = "${var.foo.x}"

... with a default setup like ...

variable "foo" {
  default = {}
}

... and a .tfvars file like ...

foo.x = 0

This constantly failed with the same error as yours. However, when I populated the defaults like ...

variable "foo" {
  default = {
    x = 0
  }
}

... the problem went away. I suspect some funny treatment of zero values here.

So, the causes here might not be the same but I'd suggest that the value of your variable is potentially an issue. Or even the default value.

HTH at least.

@dennybaa
Copy link

dennybaa commented Aug 5, 2016

+1

@mitchellh
Copy link
Contributor

This is fixed or is covered by #3888

@ghost
Copy link

ghost commented Apr 19, 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 19, 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

5 participants