Todd
Todd

Reputation: 2999

Handling variable not found in Terraform

I have a two Launch Config creation resources in Terraform: one for spot pricing and one for reserve pricing - with the choice on which to use based on a "use_spot_pricing" boolean variable. I need to return the Launch Config ID from whichever resource was used. The problem is that the conditional blows up saying the Launch Config ID was not found for the resource that wasn't created.

My code looks like:

resource "aws_launch_configuration" "launch_config_reserved_pricing" {
  //  If use_spot_pricing is true (which translates to 1), this resource is not created (i.e. count = 0).
  count                = "${1 - var.use_spot_pricing}"
  name_prefix          = "${var.resource_name_prefix}${var.envSuffix}-"
  image_id             = "${var.generic_ami_id}"
  instance_type        = "${var.instance_type}"
  key_name             = "${var.key_name}"
  security_groups      = ["${var.vpc_security_group_ids}"]
  iam_instance_profile = "${var.iam_instance_profile}"
  user_data            = "${data.template_file.lc_user_data.rendered}"
}

resource "aws_launch_configuration" "launch_config_spot_pricing" {
  //  If use_spot_pricing is true (which translates to 1), this resource is created once.  Otherwise the previous one is.
  count                = "${var.use_spot_pricing}"
  name_prefix          = "${var.resource_name_prefix}${var.envSuffix}-"
  image_id             = "${var.generic_ami_id}"
  instance_type        = "${var.instance_type}"
  key_name             = "${var.key_name}"
  security_groups      = ["${var.vpc_security_group_ids}"]
  iam_instance_profile = "${var.iam_instance_profile}"
  user_data            = "${data.template_file.lc_user_data.rendered}"
  spot_price           = "${var.spot_price}"
}

output "launch_config_id" {
  value = "${ var.use_spot_pricing == true ? aws_launch_configuration.launch_config_spot_pricing.id : aws_launch_configuration.launch_config_reserved_pricing.id }"
}

This results in the errors (the first when I the spot pricing resource is used and the second when the reserve pricing resource is used):

* module.create_launch_configs.module.parser.output.launch_config_id: Resource 'aws_launch_configuration.launch_config_reserved_pricing' not found for variable 'aws_launch_configuration.launch_config_reserved_pricing.id'
* module.create_launch_configs.module.filter.output.launch_config_id: Resource 'aws_launch_configuration.launch_config_spot_pricing' not found for variable 'aws_launch_configuration.launch_config_spot_pricing.id'

This work around failed:

output "launch_config_id" {
  value = "${coalesce(aws_launch_configuration.launch_config_spot_pricing.id , aws_launch_configuration.launch_config_reserved_pricing.id ) }"
}

This work around failed too:

output "launch_config_id" {
  value = "${coalesce( join( "" , aws_launch_configuration.launch_config_spot_pricing.id ) , join( "" , aws_launch_configuration.launch_config_reserved_pricing.id ) ) }"
}

Also tried using 1 instead of true, no luck:

output "launch_config_id" {
  value = "${ var.use_spot_pricing == 1 ? aws_launch_configuration.launch_config_spot_pricing.id : aws_launch_configuration.launch_config_reserved_pricing.id }"
}

In case anyone from Hashicorp is reading, a conditional shouldn't fail if the value not selected is undefined. It doesn't need to be examined, only the value for the condition that passes.

Upvotes: 0

Views: 1174

Answers (1)

ydaetskcoR
ydaetskcoR

Reputation: 56859

While it can be annoying that Terraform won't shortcut and not evaluate the false side of a conditional in this particular case you don't need it because you can just default the spot price to an empty string and then if it's not provided you get an on demand instance launch config.

So instead of having to do what you're currently doing instead you can just do:

variable "spot_price" {
  default = ""
}

resource "aws_launch_configuration" "launch_config" {
  name_prefix          = "${var.resource_name_prefix}${var.envSuffix}-"
  image_id             = "${var.generic_ami_id}"
  instance_type        = "${var.instance_type}"
  key_name             = "${var.key_name}"
  security_groups      = ["${var.vpc_security_group_ids}"]
  iam_instance_profile = "${var.iam_instance_profile}"
  user_data            = "${data.template_file.lc_user_data.rendered}"
  spot_price           = "${var.spot_price}"
}

output "launch_config_id" {
  value = "${aws_launch_configuration.launch_config.id}"
}

Upvotes: 1

Related Questions