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

Any Way to Check If Resource or Data Has Attributes? #17881

Closed
hippothewild opened this issue Apr 17, 2018 · 3 comments · Fixed by #19514
Closed

Any Way to Check If Resource or Data Has Attributes? #17881

hippothewild opened this issue Apr 17, 2018 · 3 comments · Fixed by #19514

Comments

@hippothewild
Copy link

Terraform Version

Terraform v0.11.6
+ provider.aws v1.14.1
+ provider.template v1.0.0

Terraform Configuration Files

# Just for example.
 
provider "aws" {
  region = "us-east-1"
}

variable "image_index" {
  default = "d"
}

locals {
  image_name = {
    a = "ami-ed14c790" # Windows Server 2016
    b = "ami-1853ac65" # Amazon Linux 2017.09.1
  }
}

data "aws_ami" "ec2_ami" {
  most_recent = true

  filter {
    name = "image-id"

    values = ["${lookup(local.image_name, var.image_index, local.image_name["a"])}"]
  }
}

data "template_file" "ec2_userdata" {
  template = "${data.aws_ami.ec2_ami.platform == "windows" ? file("${path.module}/userdata_windows.tpl") : file("${path.module}/userdata_linux.tpl")}"
}

Expected Behavior

data.aws_ami.ec2_ami should have attribute platform

Actual Behavior

For non-windows image like ami-1853ac65, data.aws_ami.ec2_ami does not have attribute platform.

Error: Error running plan: 1 error(s) occurred:
* data.template_file.ec2_userdata: 1 error(s) occurred:
* data.template_file.ec2_userdata: Resource 'data.aws_ami.ec2_ami' does not have attribute 'platform' for variable 'data.aws_ami.ec2_ami.platform'

Steps to Reproduce

Copy the configuration code to empty directory, and

terraform init
terraform apply

Additional Context

The document says that some values are not always set, and the implementation also suppresses attributes that doesn't returned by AWS query.
However, to prevent different behavior depending on the data source, it should be able to have empty value( "") for the nontrivial attributes, or Terraform should have a way to check existence of attribute on resources and data variables.

@apparentlymart
Copy link
Contributor

Hi @hippothewild! Sorry for this strange behavior, and for the late response.

We've been working on some significant changes to how Terraform deals with configuration and provider results in preparation for the forthcoming v0.12.0 release. One of the changes in that release is that when a provider declares an attribute in its schema but does not set a value for it then it should appear in expressions has having the value null, rather than returning an error as before.

However, when I tried this in the v0.12.0-alpha1 prerelease build I saw that there is some sort of issue still remaining here. I simplified the example configuration a little just to avoid the need for those template files:

provider "aws" {
  region = "us-east-1"
}

variable "image_index" {
  default = "d"
}

locals {
  image_name = {
    a = "ami-ed14c790" # Windows Server 2016
    b = "ami-1853ac65" # Amazon Linux 2017.09.1
  }
}

data "aws_ami" "ec2_ami" {
  most_recent = true

  filter {
    name = "image-id"

    values = ["${lookup(local.image_name, var.image_index, local.image_name["a"])}"]
  }
}

data "template_file" "ec2_userdata" {
  template = data.aws_ami.ec2_ami.platform == "windows" ? "windows" : "linux"
}

It worked as expected with the Windows AMI, as before:

$ terraform apply -auto-approve
data.aws_ami.ec2_ami: Refreshing state...
data.template_file.ec2_userdata: Refreshing state...

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

...but sadly after switching to the Linux AMI I see a new error, rather than success:

$ terraform apply -auto-approve
data.aws_ami.ec2_ami: Refreshing state...

Error: configuration for data.template_file.ec2_userdata still contains unknown values during apply (this is a bug in Terraform; please report it!)

This error suggests that data.aws_ami.ec2_ami.platform is being returned as unknown when not set, rather than null as expected. This is likely to be a bug in the code that translates the old provider implementations to the new protocol, where it is not handling correctly the case where an attribute is not set for a data source.

I'm going to mark this issue as being part of the v0.12.0 milestone so we'll look at it again before final release and fix this remaining problem and make this work correctly.

Once this is fixed, the template should render as "linux" in the linux case, and it should also be possible to test data.aws_ami.ec2_ami.platform != null to see if the attribute is set at all.

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

Hi @apparentlymart ,

Me and my team will happily be waiting for release of v0.12.0 which has lots of changes and improvements. Thank you so much for addressing the issue and telling about the future plan!

@ghost
Copy link

ghost commented Mar 30, 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 30, 2020
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.

2 participants