【问题标题】:Handling variable not found in Terraform在 Terraform 中找不到处理变量
【发布时间】:2018-02-12 04:30:22
【问题描述】:

我在 Terraform 中有两个 Launch Config 创建资源:一个用于现货定价,一个用于保留定价 - 使用基于“use_spot_pricing”布尔变量的选择。我需要从使用的任何资源返回启动配置 ID。问题是条件爆炸说找不到未创建的资源的启动配置 ID。

我的代码如下:

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 }"
}

这会导致错误(使用现货定价资源时第一个错误,使用储备定价资源时第二个错误):

* 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'

这个解决方法失败了:

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

这个解决方法也失败了:

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

还尝试使用 1 代替 true,没有运气:

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 }"
}

如果 Hashicorp 的任何人正在阅读,如果值 not selected 未定义,则条件不应失败。不需要检查,只需检查通过的条件的值。

【问题讨论】:

  • 这行得通吗? output "launch_config_id" { value = "${var.use_spot_pricing == 1 ? element(concat(aws_launch_configuration.launch_config_spot_pricing.*.id, list("")), 0) : element(concat(aws_launch_configuration.launch_config_reserved_pricing.*.id, list("")), 0)}" }

标签: amazon-web-services terraform


【解决方案1】:

虽然在这种特殊情况下 Terraform 不会快捷方式并且不会评估条件的错误方面可能很烦人,但您不需要它,因为您可以将现货价格默认为空字符串,然后如果不是只要您获得按需实例启动配置。

因此,您不必做您当前正在做的事情,而是可以做:

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}"
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-10
    • 2021-09-11
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多