Terraform: đầu ra mô-đun

Oct 30 2020

Chúng tôi có ba không gian làm việc trong app.terraform.io,

  • KIỂM TRA
  • DEV
  • CHUYÊN NGHIỆP

Các không gian làm việc đó có các biến.

Tôi nhận được giá trị của các biến đó với ...

variable "environment" {}

Tôi muốn sử dụng biến này để chọn tài nguyên hoặc mô-đun tải.

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

Điều này hoạt động nhưng ...

Đầu ra thông thường cho tài nguyên này là ...

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
}

Nhưng nếu tôi sử dụng thuộc tính số đếm, đầu ra hte không thành công với ...

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]

Đầu ra nghiêm ngặt sẽ là gì?

Tôi đã thử:

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

và:

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

Nhưng không hoạt động (không)

  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.

Tôi đã cố gắng xem nó được thực hiện như thế nào, chẳng hạn như 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]
}

vì thế ...

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

nhưng ...

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.

Trả lời

3 BinaryMonster Oct 30 2020 at 09:49

Chà không có gì phức tạp, chỉ là đầu ra đang tìm kiếm tài nguyên không được tạo, vì terraform vẫn đang tham chiếu đến tài nguyên đó do đó nó đang gặp lỗi.

Trong khi có một số cách để giải quyết vấn đề này, một trong số đó là nối tất cả các tài nguyên dưới dạng danh sách trong một bước và truy xuất một phần tử duy nhất bằng cách sử dụng các hàm tích hợp sẵn của terraform.

Đầu ra của bạn phải giống như thế này khi bạn đang sử dụng số đếm làm điều kiện để tạo tài nguyên. Nếu tài nguyên được tạo, đầu ra sẽ là id của tài nguyên. Khi số đếm bằng 0, đầu ra sẽ trống mà không có bất kỳ lỗi nào.

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