From 57db3cfe5164404c0e353fda7596364911868417 Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Thu, 16 Nov 2017 10:15:11 -0800 Subject: [PATCH] website: 0.11 upgrade guidance on resources with count = 0 This is a genre of invalid output expression that we've seen quite commonly while testing with 0.11.0-rc1, so we'll call it out specifically in the upgrade guide and suggest how to fix it. --- website/upgrade-guides/0-11.html.markdown | 29 +++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/website/upgrade-guides/0-11.html.markdown b/website/upgrade-guides/0-11.html.markdown index 890d7df3f..9961f49c0 100644 --- a/website/upgrade-guides/0-11.html.markdown +++ b/website/upgrade-guides/0-11.html.markdown @@ -282,3 +282,32 @@ Terraform 0.11 will require adjusting the configuration to avoid the error. **Action:** If any existing output value expressions contain errors, change these expressions to fix the error. + +### Referencing Attributes from Resources with `count = 0` + +A common pattern for conditional resources is to conditionally set count +to either `0` or `1` depending on the result of a boolean expression: + +```hcl +resource "aws_instance" "example" { + count = "${var.create_instance ? 1 : 0}" + + # ... +} +``` + +When using this pattern, it's required to use a special idiom to access +attributes of this resource to account for the case where no resource is +created at all: + +```hcl +output "instance_id" { + value = "${element(concat(aws_instance.example.*.id, list("")), 0)}" +} +``` + +Accessing `aws_instance.example.id` directly is an error when `count = 0`. +This is true for all situations where interpolation expressions are allowed, +but previously _appeared_ to work for outputs due to the suppression of the +error. Existing outputs that access non-existent resources must be updated to +use the idiom above after upgrading to 0.11.0.