0

我的模块将 apossibly-empty-list作为输入,如果该列表不为空,则创建一些资源并返回我在模块之外需要的特定属性,如下所示:

variable contexts {
    type = "list"
}

resource "pagerduty_service" "p1" {
    count = "${length(var.contexts)}"
    name                    = "p1-${element(var.contexts, count.index)}"
    description             = "p1-${element(var.contexts, count.index)}"
    auto_resolve_timeout    = 14400
    acknowledgement_timeout = 1800
    escalation_policy       = "${pagerduty_escalation_policy.p1.id}"
    alert_creation          = "create_alerts_and_incidents"
    incident_urgency_rule {
        type    = "constant"
        urgency = "high"
    }
}

data "pagerduty_vendor" "cloudwatch" {
    name = "Cloudwatch"
}

resource "pagerduty_service_integration" "p1_cloudwatch" {
    count   = "${length(var.contexts)}"
    name    = "Amazon Cloudwatch"
    vendor  = "${data.pagerduty_vendor.cloudwatch.id}"
    service = "${element(pagerduty_service.p1.*.id, count.index)}"
}

output "integration_keys" {
    value = "${pagerduty_service_integration.*.integration_keys}"
}

我遇到的麻烦是,当这个模块首先使用非空列表运行时,从而创建资源,它工作正常。如果我再次运行它,它会失败并出现以下异常:

* module.pagerduty.output.integration_keys: Resource 'pagerduty_service_integration.possibly_empty_resource_list' does not have attribute 'integration_key' for variable 'pagerduty_service_integration.possibly_empty_resource_list.*.integration_key'

如果为空,我想不出一个好方法让它output返回一个空列表possibly_empty_resource_list

有任何想法吗?

编辑:

我尝试对输出执行三元检查,但由于某种原因,不支持使用列表,因此这不起作用,但我希望它说明我正在尝试做的事情:

"${length(var.contexts) > 0 ? pagerduty_service_integration.*.integration_keys : list()}"

解决方案:

output "instance_id" { 
  value = "${element(concat(aws_instance.example.*.id, list("")), 0)}"
}
4

1 回答 1

1

在 terraform 升级到 0.11 指南的最底部有一个部分:https ://www.terraform.io/upgrade-guides/0-11.html显示了我用于计算资源的内容

前任: output "instance_id" { value = "${element(concat(aws_instance.example.*.id, list("")), 0)}" }

(从评论移过来)

于 2018-02-13T16:44:52.127 回答