[기본 구성 요소] Output
2024. 11. 26. 02:37ㆍTerraform/[인프런] 아키텍처와 함께하는 Terraform
1. Output
- 실행 결과를 출력하는 블록이다.
- 리소스 생성 후 ID, IP 주소, ARN 등을 출력하여 사용자에게 제공하거나 데이터 전달하는 데 사용된다.
2. Output 주요 특징
- 결과 정보 제공 : Terraform 실행 결과로 생성된 리소스의 속성 값을 사용자에게 출력한다.
- 모듈 간 데이터 전달
- 가시성 제어 : sensitive 속성을 통해 민감한 데이터를 숨길 수 있다.
- CLI에서 확인
3. Output 예제
- Output 정의 예제 (outputs.tf)
* 예제
# 생성된 EC2 인스턴스의 고유 ID 출력
output "instance_id" {
# 추가 설명
description = "The ID of the created EC2 instance"
# 출력할 값을 지정
value = aws_instance.example.id
}
# 생성된 EC2 인스턴스 퍼블릭 IP 출력
output "instance_public_ip" {
description = "The public IP address of the EC2 instance"
value = aws_instance.example.public_ip
}
# 인스턴스에 할당된 태그 출력
output "tags" {
description = "The tags assigned to the instance"
value = aws_instance.example.tags
}
- Output 실행 결과 예제
* 예제
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
instance_id = "i-0abc123456def7890"
instance_public_ip = "203.0.113.25"
tags = {
"Name" = "ExampleInstance"
}
- sensitive 속성 사용 예제
- 민감한 데이터를 보호할 때 유용하다.
* 예제
output "db_password" {
value = aws_db_instance.example.password
# 출력 값을 터미널에 표시하지 않는다.
sensitive = true
description = "The password of the database instance"
}
- 모듈 간 데이터 전달 예제
* 예제
* 하위 모듈(modules/vpc/outputs.tf)
output "vpc_id" {
value = aws_vpc.example.id
}
* 상위 구성(main.tf)
module "vpc" {
source = "./modules/vpc"
}
resource "aws_subnet" "example" {
# 하위 모듈의 Output 참조
vpc_id = module.vpc.vpc_id
cidr_block = "10.0.1.0/24"
}
- 값 타입 지정 예제
* 예제
output "instance_ips" {
value = [aws_instance.example1.public_ip, aws_instance.example2.public_ip]
# 리스트 타입으로 출력
type = list(string)
}
'Terraform > [인프런] 아키텍처와 함께하는 Terraform' 카테고리의 다른 글
[기본 구성 요소] Modules (0) | 2024.11.26 |
---|---|
[기본 구성 요소] state 파일 (0) | 2024.11.26 |
[기본 구성 요소] Variables (0) | 2024.11.26 |
[기본 구성 요소] Resource (0) | 2024.11.26 |
[기본 구성 요소] Provider (0) | 2024.11.25 |