Terraform: Modulausgang

Oct 30 2020

Wir haben drei Arbeitsbereiche in app.terraform.io,

  • PRÜFUNG
  • DEV
  • PROFI

Diese Arbeitsbereiche haben Variablen.

Ich bekomme den Wert von mit diesen Variablen mit ...

variable "environment" {}

Ich möchte diese Variable verwenden, um auszuwählen, welche Ressourcen oder Module geladen werden.

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

Das funktioniert aber ...

Die übliche Ausgabe für diese Ressource ist ...

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
}

Aber wenn ich die Zählung atribute verwende, schlägt die Ausgabe mit ... fehl.

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]

Was wäre die richtige Ausgabe?

Ich habe es versucht:

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

und:

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

Funktioniert aber nicht (auch nicht)

  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.

Ich habe zum Beispiel versucht zu sehen, wie es gemacht wird 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]
}

damit ...

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

aber ...

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.

Antworten

3 BinaryMonster Oct 30 2020 at 09:49

Nun, nichts Komplexes, es ist nur so, dass die Ausgabe nach der Ressource sucht, die nicht erstellt wurde, da Terraform immer noch auf die Ressource verweist, aufgrund derer sie einen Fehler auslöst.

Es gibt verschiedene Möglichkeiten, um dieses Problem zu beheben. Eine davon besteht darin, alle Ressourcen in einem Schritt als Liste zu verketten und ein einzelnes Element mithilfe der in Terraform integrierten Funktionen abzurufen.

Ihre Ausgabe sollte ungefähr so ​​aussehen, wenn Sie count als Bedingung zum Erstellen von Ressourcen verwenden. Wenn die Ressource erstellt wird, ist die Ausgabe die ID der Ressource. Wenn der Zähler Null ist, ist die Ausgabe ohne Fehler leer.

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