Terraform-如何在条件运算符中使用条件创建的资源的输出?

时间:2019-05-22 12:47:22

标签: terraform terraform-provider-aws

在某些情况下,如果用户不提供vpc ID,则必须创建aws_vpc资源。之后,我应该使用该VPC创建资源。

现在,我在创建aws_vpc资源时应用条件。例如,仅在existing_vpc为假的情况下创建VPC:

count                = "${var.existing_vpc ? 0 : 1}"

接下来,例如,我必须在VPC中创建节点。如果existing_vpc为true,请使用var.vpc_id,否则请使用从aws_vpc资源中计算出的VPC ID。

但是,问题是,如果existing_vpc为真,aws_vpc将不会创建新资源,而三元条件总是试图检查是否正在创建aws_vpc资源或不。如果未创建,则将导致地形错误。

aws_subnet上使用条件运算符时的错误示例:

Resource 'aws_subnet.xyz-subnet' not found for variable 'aws_subnet.xyz-subnet.id'

导致错误的代码是:

subnet_id = "${var.existing_vpc ? var.subnet_id : aws_subnet.xyz-subnet.id}"

如果两者都相互依赖,我们如何创建条件资源并基于它们为其他配置分配值?

2 个答案:

答案 0 :(得分:1)

您可以按如下方式访问动态创建的模块和资源

output "vpc_id" {
  value = length(module.vpc) > 0 ? module.vpc[*].id : null
}
  • 如果count = 0,则输出为空
  • 如果计数 > 0,则输出是 vpc id 列表

如果 count = 1 并且您想接收单个 vpc id,您可以指定:

output "vpc_id" {
  value = length(module.vpc) > 0 ? one(module.vpc).id : null
}

答案 1 :(得分:0)

以下示例显示如何有选择地指定是否创建资源(使用条件运算符),并显示如何在未创建资源时处理返回的输出。这恰好是使用模块完成的,并且使用object变量的元素作为标志来指示是否应该创建资源。

但是要具体回答您的问题,可以按以下方式使用条件运算符:


    output "module_id" {
       value = var.module_config.skip == true ? null : format("%v",null_resource.null.*.id)
    }

并在调用main.tf中访问输出:

module "use_conditionals" {
    source = "../../scratch/conditionals-modules/m2" # << Change to your directory
    a = module.skipped_module.module_id # Doesn't exist, so might need to handle that.
    b = module.notskipped_module.module_id
    c = module.default_module.module_id
}

完整示例如下。注意:这是使用terraform v0.14.2


# root/main.tf
provider "null" {}

module "skipped_module" {
    source = "../../scratch/conditionals-modules/m1" # << Change to your directory
    module_config = { 
        skip = true         # explicitly skip this module.
        name = "skipped" 
    }
}

module "notskipped_module" {
    source = "../../scratch/conditionals-modules/m1" # << Change to your directory
    module_config = { 
        skip = false        # explicitly don't skip this module.
        name = "notskipped"
    }
}

module "default_module" {
    source = "../../scratch/conditionals-modules/m1" # << Change to your directory
    # The default position is, don't skip. see m1/variables.tf
}

module "use_conditionals" {
    source = "../../scratch/conditionals-modules/m2" # << Change to your directory
    a = module.skipped_module.module_id
    b = module.notskipped_module.module_id
    c = module.default_module.module_id
}

# root/outputs.tf
output skipped_module_name_and_id {
    value = module.skipped_module.module_name_and_id
}

output notskipped_module_name_and_id {
    value = module.notskipped_module.module_name_and_id
}

output default_module_name_and_id {
    value = module.default_module.module_name_and_id
}

模块


# m1/main.tf
resource "null_resource" "null" {
    count = var.module_config.skip ? 0 : 1 # If skip == true, then don't create the resource.

    provisioner "local-exec" {
        command = <<EOT
        #!/usr/bin/env bash
        echo "null resource, var.module_config.name: ${var.module_config.name}"
EOT
    }
}

# m1/variables.tf
variable "module_config" {
    type = object ({ 
        skip = bool, 
        name = string 
    })
    default = {
        skip = false
        name = "<NAME>"
        }
}

# m1/outputs.tf
output "module_name_and_id" {
   value = var.module_config.skip == true ? "SKIPPED" : format(
      "%s id:%v",
      var.module_config.name, 
      null_resource.null.*.id
   )
}

output "module_id" {
      value = var.module_config.skip == true ? null : format("%v",null_resource.null.*.id)
}