Terraform:模块输出

时间:2020-10-30 09:02:55

标签: terraform terraform-provider-aws

app.terraform.io中有三个工作区,

  • 测试
  • DEV
  • PRO

这些工作区具有变量。

我用...获得那个变量的值...

platforms\android\app\build\intermediates\project_dex_archive\release\out\de\ranger\telekomwholebuyapp\BuildConfig.dex: 
D8: Type de.ranger.telekomwholebuyapp.BuildConfig is defined multiple times: 
platforms\android\app\build\intermediates\project_dex_archive\release\out\de\ranger\telekomwholebuyapp\BuildConfig.dex,
platforms\android\CordovaLib\build\.transforms\6151e869b0c05b3495507d84c6fe7023\classes\classes.dex                                                                                              
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:

我想使用此变量来选择加载哪些资源或模块。

variable "environment" {}

这可行,但是...

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

resource "aws_directory_service_directory" "ds" {
  count = "${var.environment == "TEST" ? 0 : 1}"
  
  name       = "example.com"
  short_name = "example"
  password   = "******"
  type       = "MicrosoftAD"
}

但是如果我使用count属性,则hte输出将失败,并显示...

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.

我试图查看它是如何完成的,例如https://github.com/terraform-aws-modules/terraform-aws-vpc

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.

所以...

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

1 个答案:

答案 0 :(得分:3)

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

虽然有多种方法可以解决此问题,但其中一种方法是一步将所有资源连接为列表,并使用terraform内置函数检索单个元素。

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

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