Terraform:模块输出



我们在app.terraform.io、中有三个工作区

  • 测试
  • DEV
  • 专业

这些工作区有变量。

我得到的值与这些变量。。。

variable "environment" {}

我想使用这个变量来选择加载什么资源或模块。

resource "aws_directory_service_directory" "ds" {
count = "${var.environment == "TEST" ? 0 : 1}"

name       = "example.com"
short_name = "example"
password   = "******"
type       = "MicrosoftAD"
}

这是有效的,但是。。。

此资源的通常输出是。。。

output id {
description = "The ID of the directory"
value       = aws_directory_service_directory.ds.id
}
output access_url {
description = "The access URL for the directory"
value       = aws_directory_service_directory.ds.access_url
}
output dns_ip_addresses {
description = "A list of IP addresses of the DNS servers for the directory or connector"
value       = aws_directory_service_directory.ds.dns_ip_addresses
}
output security_group_id {
description = "The ID of the security group created by the directory"
value       = aws_directory_service_directory.ds.security_group_id
}

但如果我使用计数矩阵,输出会失败。。。

Because aws_directory_service_directory.ds has "count" set, its attributes
must be accessed on specific instances.
For example, to correlate with indices of a referring resource, use:
aws_directory_service_directory.ds[count.index]
mixture of literal strings and interpolations. This deprecation applies only
to templates that consist entirely of a single interpolation sequence.

Error: Missing resource instance key
on outputs.tf line 51, in output "security_group_id":
51:   value       = aws_directory_service_directory.ds.security_group_id
Because aws_directory_service_directory.ds has "count" set, its attributes
must be accessed on specific instances.
For example, to correlate with indices of a referring resource, use:
aws_directory_service_directory.ds[count.index]

正确的输出是什么?

我试过了:

output id {
description = "The ID of the directory"
value       = aws_directory_service_directory.ds[0].id
}

和:

output id {
description = "The ID of the directory"
value       = aws_directory_service_directory.ds[1].id
}

但不起作用(两者都不起作用(

on outputs.tf line 36, in output "id":
36:   value       = aws_directory_service_directory.ds[0].id
|----------------
| aws_directory_service_directory.ds is empty tuple
The given key does not identify an element in this collection value.
Error: Invalid index
on outputs.tf line 36, in output "id":
36:   value       = aws_directory_service_directory.ds[1].id
|----------------
| aws_directory_service_directory.ds is tuple with 1 element
The given key does not identify an element in this collection value.

例如,我试着看看它是如何做到的https://github.com/terraform-aws-modules/terraform-aws-vpc

output "vpc_id" {
description = "The ID of the VPC"
value       = concat(aws_vpc.this.*.id, [""])[0]
}

所以。。。

output id {
description = "The ID of the directory"
value       = concat(aws_directory_service_directory.*.id, [""])[0]
}

但是。。。

Error: Invalid reference
on outputs.tf line 36, in output "id":
36:   value       = concat(aws_directory_service_directory.*.id, [""])[0]
A reference to a resource type must be followed by at least one attribute
access, specifying the resource name.

没有什么复杂的,只是输出在寻找未创建的资源,因为terraform仍然引用该资源,因此它抛出了一个错误。

虽然有几种方法可以解决这个问题,但其中之一是在一个步骤中将所有资源连接为列表,并在构建的函数中使用terraform检索单个元素。

当您使用计数作为创建资源的条件时,您的输出应该是这样的。如果创建了资源,则输出将是资源的id。当计数为零时,输出将为空,没有任何错误。

output id {
description = "The ID of the directory"
value       = element(concat(aws_directory_service_directory.ds.*.id, list("")), 0)
}

相关内容

  • 没有找到相关文章

最新更新