Terraform: เอาท์พุทโมดูล

Oct 30 2020

เรามีพื้นที่ทำงานสามแห่งใน 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
}

แต่ถ้าฉันใช้จำนวน atribute เอาต์พุต hte จะล้มเหลวด้วย ...

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.

คำตอบ

3 BinaryMonster Oct 30 2020 at 09:49

ไม่มีอะไรซับซ้อนเพียงแค่เอาต์พุตกำลังมองหาทรัพยากรที่ไม่ได้สร้างขึ้นเนื่องจาก Terraform ยังคงอ้างถึงทรัพยากรนั้นเนื่องจากมีข้อผิดพลาด

แม้ว่าจะมีหลายวิธีในการแก้ไขปัญหานี้ แต่หนึ่งในนั้นคือการเชื่อมต่อทรัพยากรทั้งหมดเป็นรายการในขั้นตอนเดียวและดึงข้อมูลองค์ประกอบเดียวโดยใช้ฟังก์ชันที่สร้างขึ้นในพื้น

ผลลัพธ์ของคุณควรเป็นแบบนี้เมื่อคุณใช้การนับเป็นเงื่อนไขในการสร้างทรัพยากร หากทรัพยากรถูกสร้างผลลัพธ์จะเป็น id ของทรัพยากร เมื่อการนับเป็นศูนย์เอาต์พุตจะว่างเปล่าโดยไม่มีข้อผิดพลาดใด ๆ

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