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

Silent Failure with "Cannot parse as an integer" when using derived value #18436

Closed
durkode opened this issue Jul 12, 2018 · 7 comments
Closed
Milestone

Comments

@durkode
Copy link

durkode commented Jul 12, 2018

Terraform silently fails with [WARN] Interpolation "count" failed: Error reading aws_instance.zk count: cannot parse "${var.instance_count}" as an integer.

Running a plan with a resource having a property of the count of another, the script silently fails. By that I mean the plan generates successfully (saying 3 resources will be created), terraform show outputs the plan correctly, however applying the plan exits as if successful, although the summary lists 0 resources created. Only when adding TF_LOG=DEBUG do you see the above warning

Terraform Version

v0.11.7

Terraform Configuration Files

Relevant sections

variable "instance_count" {
  default = 3
}

resource "aws_instance" "zk" {
  count = "${var.instance_count}"

  ... Other properties
}

resource "aws_route53_record" "zk" {
  count = "${aws_instance.zk.count}"
  
 ... Other properties
}

Replacing the route53 record count with "${var.instance_count}" fixes the issue

Debug Output

Gist Here

Crash Output

N/A

Expected Behavior

Plan applies as shown

Actual Behavior

Plan generates correctly. Apply exits successfully, but says no resources are created (when plan shows 3).

Steps to Reproduce

terraform plan
terraform apply

Additional Context

Using terraform via wrapper script, however terraform show shows the plan successfully and then it just runs an apply on the generated plan. Changing the route53 record count to use "${var.instance_count}" fixes the issue, so I don't think it is a problem with how I am running terraform.

References

Initially thought it was #17368, however that errors rather than silently failing.

@apparentlymart
Copy link
Contributor

Hi @durkode! Sorry for this odd behavior.

I think this is a bug that's already been addressed in the development branch for the next major release, since we've refactored a little how count works.

We still have some remaining work to do on the branch to get things ready to merge, but once we do we'll circle back here and confirm whether this is now fixed or if there's some additional work to do.

Thanks for reporting this!

@durkode
Copy link
Author

durkode commented Jul 12, 2018

Sounds good @apparentlymart, thanks for letting me know.

@aramalipoor
Copy link

aramalipoor commented Aug 5, 2018

Any quick workaround for this till 0.12 is out?

@durkode
Copy link
Author

durkode commented Aug 5, 2018

@aramalipoor What I did was use the variable that count is assigned to as the output value (rather than referencing count on the resource). If count is currently just a variable, make it a variable/local first. That should fix it.

@apparentlymart
Copy link
Contributor

Hi again @durkode! Sorry for the long silence here.

Now that the development work has merged into master, I verified this with the v0.12.0-alpha2 prerelease build using the following config that should be equivalent to yours for the purposes of this issue, but can be applied local-only:

variable "instance_count" {
  default = 3
}

resource "null_resource" "a" {
  count = "${var.instance_count}"
}

resource "null_resource" "b" {
  count = "${null_resource.a.count}"
}

Due to other changes in this release, this failed in an expected way:

$ terraform apply

Error: Unsupported attribute

  on count-acts-weird.tf line 11, in resource "null_resource" "b":
  11:   count = "${null_resource.a.count}"
    |----------------
    | null_resource.a is tuple with 3 elements

This value does not have any attributes.

In order to support using entire resources as objects in the language, we had to accept a breaking change here for the special count attribute that used to be treated as a special case. In the new implementation, a resource with count set evaluates to a sequence (a tuple) of instance objects, and so count can no longer be an attribute there. Instead, we need to rewrite this to use the same length function we'd use for any other sequence:

resource "null_resource" "b" {
  # (note that we no longer need the interpolation wrapper around this, either)
  count = length(null_resource.a)
}
$ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # null_resource.a[0] will be created
  + resource "null_resource" "a" {
      + id = (known after apply)
    }

  # null_resource.a[1] will be created
  + resource "null_resource" "a" {
      + id = (known after apply)
    }

  # null_resource.a[2] will be created
  + resource "null_resource" "a" {
      + id = (known after apply)
    }

  # null_resource.b[0] will be created
  + resource "null_resource" "b" {
      + id = (known after apply)
    }

  # null_resource.b[1] will be created
  + resource "null_resource" "b" {
      + id = (known after apply)
    }

  # null_resource.b[2] will be created
  + resource "null_resource" "b" {
      + id = (known after apply)
    }

Plan: 6 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

null_resource.a[0]: Creating...
null_resource.a[2]: Creating...
null_resource.a[1]: Creating...
null_resource.a[1]: Creation complete after 0s [id=2286986984335662890]
null_resource.a[0]: Creation complete after 0s [id=6403318210419861512]
null_resource.a[2]: Creation complete after 0s [id=7358292179497330710]
null_resource.b[1]: Creating...
null_resource.b[0]: Creating...
null_resource.b[2]: Creating...
null_resource.b[2]: Creation complete after 0s [id=1156130884422989986]
null_resource.b[0]: Creation complete after 0s [id=1500802818513873516]
null_resource.b[1]: Creation complete after 0s [id=1527626550781192653]

Apply complete! Resources: 6 added, 0 changed, 0 destroyed.

Since this fix is merged in master, I'm going to close this out. The fix will also be included in the forthcoming v0.12.0 release. Thanks again for reporting this, and sorry again for the delayed update.

@apparentlymart apparentlymart added this to the v0.12.0 milestone Oct 31, 2018
@durkode
Copy link
Author

durkode commented Oct 31, 2018

Thank you @apparentlymart, much appreciated.

@ghost
Copy link

ghost commented Mar 31, 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 Mar 31, 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

3 participants