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
}

그러나 count 속성을 사용하면 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]

rigth 출력은 무엇입니까?

나는 시도했다 :

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이 오류를 발생시키는 리소스를 계속 참조하기 때문에 출력이 생성되지 않은 리소스를 찾는 것입니다.

이 문제를 해결하는 방법에는 여러 가지가 있지만 그중 하나는 모든 리소스를 한 번에 목록으로 연결하고 terraform 내장 함수를 사용하여 단일 요소를 검색하는 것입니다.

리소스를 생성하기위한 조건으로 count를 사용할 때 출력은 다음과 같아야합니다. 리소스가 생성되면 출력은 리소스의 ID가됩니다. 카운트가 0이면 출력은 오류없이 비어 있습니다.

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