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

Objects incorrectly cast to null when used in a conditional with null #21395

Closed
JustinGrote opened this issue May 23, 2019 · 3 comments · Fixed by #21957
Closed

Objects incorrectly cast to null when used in a conditional with null #21395

JustinGrote opened this issue May 23, 2019 · 3 comments · Fixed by #21957

Comments

@JustinGrote
Copy link

Congrats on 0.12 release

Terraform Version

Terraform v0.12.0
+ provider.azurerm v1.28.0
+ provider.local v1.2.2

Terraform Configuration Files

variable "switch" {
    type = bool
    default = "true"
}

#Simple example of an "object"
resource "local_file" "example" {
    filename = "mytestfile.txt"
    content = "This is a test file"
}

data "local_file" "example" {
    filename = "main.tf"
}

data "local_file" "example2" {
    filename = local_file.example.filename
}

output "testDatasource" {
    value = data.local_file.example2
}

output "testAttribute" {
    value = local_file.example.filename
}

#An example showing nulls don't show in terraform plan output at all, you wont see this in the result, not sure if this is a bug or feature
output "nullOutputsDontShowInPlanOutput" {
    value = null
}


#This only happens with objects as far as I can tell, other basic types work fine
output "testAttributeComesBackCorrect" {
    value = var.switch == true ? local_file.example.filename : null
}

#Object by itself returns fine
output "testObject" {
    value = local_file.example
}

#Object in a conditional with another object returns just fine. Set -var='switch=false' and it works to return the other object
output "testObjectReturnsConditional" {
    value = var.switch == true ? data.local_file.example2 : data.local_file.example
}

#Same object in a conditional doesn't return if null is there, meaning it probably got "cast" to null
output "testObjectDoesntReturn" {
    value = var.switch == true ? local_file.example : null
}

#Same result for a datastore object
output "testDataStoreDoesntReturn" {
    value = var.switch == true ? data.local_file.example : null
}

Debug Output

https://gist.github.com/JustinGrote/2c7b9fc4aec47e14ddae975d3065c4cc

Expected Behavior

Anywhere that a conditional with a null in it should have resulted in an output with that object

Actual Behavior

No output was shown. Assumption is that it cast to "null".

Steps to Reproduce

  1. terraform init
  2. terraform apply
  3. terraform apply -var 'switch=false'
@JustinGrote
Copy link
Author

JustinGrote commented May 23, 2019

Workaround

At least for outputs, if you reference a resource object in an output and it is specified in the config but has has count=0, then it just comes back as an empty list rather than erroring out, so for this circumstance of "optional resources", doing a condition check isn't necessary. It will still error if you specify a resource that doesn't exist in the config.

resource "local_file" "example" {
    count = 0
    filename = "mytestfile.txt"
    content = "This is a test file"
}

#Returns empty list instead of erroring (which is nice)
output "test" {
    value = local_file.example
}

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

Outputs:

test = []

@kinghuang
Copy link

I've also run into this problem in my configs. I have several object outputs that keep giving null in a true-false expression.

output "autoscaling_group" {
  description = "Auto scaling group for node instances."
  value       = var.create_nodes ? aws_autoscaling_group.this[0] : null
}

In my case, I worked around it by coalesce the conditional resource with a list containing a null, and selecting the first element.

output "autoscaling_group" {
  description = "Auto scaling group for node instances."
  value       = coalescelist(aws_autoscaling_group.this, [null])[0]
}

@ghost
Copy link

ghost commented Aug 13, 2019

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 Aug 13, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants