Attribute
provider를 통해 만들어진 리소스는 Attribute을 가지게 되는데, 이 값들은 서로 연관된 다른 리소스를 생성할 때 참조해야 할 때가 있다. 예를 들어, VPC는 vpc.id라는 Attribute을 가지고 있는데, subnet을 만들 때 vpc.id를 필수 값으로 반영해야 하기 때문에 참조할 데이터의 대상이 된다.
1. Attribute 참조 형식
# <provider_name>_<resource_type>.<resource_name>.<attribute_name>
aws_vpc.main.id
2. Attribute 적용 예시
resource "aws_vpc" "main" {
cidr_block = var.vpc_cidr
tags = merge(
{
"Name" = "Main"
},
var.default_tags
)
}
resource "aws_subnet" "public1" {
vpc_id = aws_vpc.main.id # Attribute 적용
cidr_block = "10.10.1.0/24"
tags = merge(
{
"Name" = "Public1"
},
var.default_tags
)
availability_zone = "ap-northeast-2a"
map_public_ip_on_launch = true
}
resource "aws_subnet" "public2" {
vpc_id = aws_vpc.main.id # Attribute 적용
cidr_block = "10.10.2.0/24"
tags = merge(
{
"Name" = "Public2"
},
var.default_tags
)
availability_zone = "ap-northeast-2b"
map_public_ip_on_launch = true
}
Output
terraform에 데이터를 입력하여 변수에 저장할 때는 variable을 사용하고, terraform apply를 통해 출력되는 결과들을 변수에 저장할 때는 output을 사용한다. terraform을 이용해 작업하는 과정에 생성한 리소스의 Attribute 값을 확인해야 할 때가 종종 있는데, 이럴 때 별도로 저장해 두지 않으면 Provider의 Console 화면에 접속해서 확인해야 한다. Output을 이용하면 지정한 Attribute 값을 tfstate 파일에 저장해 둘 수 있다.
1. 환경구성
output.tf 파일을 생성하고, 아래의 repository structure를 구성한다.
/root/terraform-starter/
├── backend.tf
├── environments
│ ├── dev.tfvars
│ ├── terraform.tfstate
│ └── tfplan
├── outputs.tf
├── provider.tf
├── tfplan
├── variables.tf
├── version.tf
└── vpc.tf
2. output.tf 파일 구성
output "main_vpc_id" {
value = aws_vpc.main.id
description = "The Id of the main VPC"
}
output "main_subnet_id1" {
value = aws_subnet.public1.id
description = "The Id of the public1 seubnet"
}
output "main_subnet_id2" {
value = aws_subnet.public2.id
description = "The Id of the public2 seubnet"
}
3. terraform 실행 후 출력 결과 확인
$ terraform plan -var-file=environments/dev.tfvars --out tfplan
$ terraform apply tfplan
aws_vpc.main: Creating...
aws_vpc.main: Creation complete after 1s [id=vpc-09aa0594e068b32e3]
aws_subnet.public1: Creating...
aws_subnet.public2: Creating...
aws_subnet.public1: Still creating... [10s elapsed]
aws_subnet.public2: Still creating... [10s elapsed]
aws_subnet.public1: Creation complete after 11s [id=subnet-0717449db12deb551]
aws_subnet.public2: Creation complete after 11s [id=subnet-0a737275d2d6186e2]
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Outputs:
main_subnet_id1 = "subnet-0717449db12deb551"
main_subnet_id2 = "subnet-0a737275d2d6186e2"
main_vpc_id = "vpc-09aa0594e068b32e3"
4. terrafotm state 값 확인
terraform 실행 결과 정보들은 state 파일에 저장된다.
이 정보는 다른 모듈에서 접근할 수 있고, 모듈 간에 공유해야 할 데이터를 구성하는데 활용된다.
$ terraform state show aws_vpc.main
# aws_vpc.main:
resource "aws_vpc" "main" {
arn = "arn:aws:ec2:ap-northeast-2:xxxxxxxxxxxx:vpc/vpc-09aa0594e068b32e3"
assign_generated_ipv6_cidr_block = false
cidr_block = "10.10.0.0/16"
default_network_acl_id = "acl-0e749908dd99f3c90"
default_route_table_id = "rtb-07ccf3f9ca94c53e1"
default_security_group_id = "sg-03c7d17739cb897ac"
dhcp_options_id = "dopt-069864bdefac47704"
enable_dns_hostnames = false
enable_dns_support = true
id = "vpc-09aa0594e068b32e3" #저장된 aws vpc id Attribute 값
instance_tenancy = "default"
main_route_table_id = "rtb-07ccf3f9ca94c53e1"
owner_id = "xxxxxxxxxxxx"
tags = {
"Name" = "Main"
"cost_centre" = "1234567890"
"environment" = "staging"
"owner" = "r&d"
}
tags_all = {
"Name" = "Main"
"cost_centre" = "1234567890"
"environment" = "staging"
"owner" = "r&d"
}
}반응형
'Programming > Terraform' 카테고리의 다른 글
| [Terraform] Data source & Terraform fucntion (0) | 2025.12.19 |
|---|---|
| [Terraform] Variable & Validation (0) | 2025.12.19 |
| [Terraform] Remote Backend 구성 (0) | 2025.12.18 |
| [Terraform] 주요 명령어 세 가지 (0) | 2025.12.18 |
| [Terraform] Terraform 개요 (0) | 2025.12.17 |