◇ 공부 기록용으로 작성하였으니 틀린점, 피드백 주시면 감사하겠습니다 ◇
Terraform 입문자로써.....🙁
Terraform 코드에서 비슷한 파일을 발견했다. variables.tf
파일과 *.tfvars
파일이다.
비슷하게 생겼는데 무슨 차이 인지 모르겠다.
결론
variables.tf
파일은 Terraform 코드에 사용할 변수를 정의하는 데 사용되고, *.tfvars
파일은 이러한 변수에 실제 값을 할당하는데 사용된다.
두 파일은 함께 연동되게 사용하여 Terraform 코드를 유연하고 재사용 가능하게 만들어준다.
아래 예시를 보는게 글을 읽는 것보다 이해하기 쉽다.
variables.tf 파일
변수를 정의하는 사용
이 파일은 Terraform 모듈 내에서 사용할 변수를 정의하는 데 사용된다.
모듈 내에서 공통으로 사용되는 변수를 정의하는 데 주로 사용됩니다.
*.tfvars 파일
변수에 값을 할당하기 위해 사용
이 파일은 Terraform 변수에 값을 할당하는 데 사용된다. 주로 환경별로 다른 값을 변수에 할당하기 위해 사용된다.
여러 환경 (예: 개발, 스테이지, 프로덕션)에서 사용할 변수 값들을 지정하는 데 사용됩니다.
결국에는 variables.tf 파일의 변수에 값을 유연하게 할당하기 위해 *.tfvars 파일이 있는 것같다.
*.tfvars 파일 없이 variables.tf 파일로만 변수 값을 지정할 수도 있다.
예시
variables.tf 파일 예시
variable "instance_type" {
description = "EC2 instance type"
type = string
}
variable "region" {
description = "AWS region"
type = string
}
variable "availability_zone" {
description = "AWS availability zone"
type = string
}
.tfvars 파일 예시
개발 환경 (dev.tfvars):
instance_type = "t2.micro"
region = "us-west-2"
availability_zone = "us-west-2a"
스테이지 환경 (stag.tfvars):
instance_type = "t2.micro"
region = "us-west-2"
availability_zone = "us-west-2a"
프로덕션 환경 (prod.tfvars):
instance_type = "t2.large"
region = "eu-central-1"
availability_zone = "eu-central-1a"
Terraform을 실행할 때 각 환경에 맞는 .tfvars 파일을 사용하여 실행할 수 있다.
예를 들어:
terraform apply -var-file=dev.tfvars
terraform apply -var-file=stag.tfvars
terraform apply -var-file=prod.tfvars
참고자료)
https://www.reddit.com/r/Terraform/comments/yt8hag/variablestf_vs_terraformtfvars_whats_the/
From the Terraform community on Reddit
Explore this post and more from the Terraform community
www.reddit.com
https://spacelift.io/blog/terraform-tfvars
Terraform .tfvars files: Variables Management with Examples
.tfvars files are the best and most common way to manage variables in Terraform. Learn how to use them effectively in your IaC projects.
spacelift.io
'Terraform' 카테고리의 다른 글
terraform fmt란? (0) | 2024.04.22 |
---|---|
[Terraform] terraform.lock.hcl 파일이란 (.terraform 파일) (1) | 2024.03.19 |
[Terraform] 테라폼의 "Backend"란? (tfstate 상태 파일을 저장하는 곳) (0) | 2024.03.11 |
[Terraform #02] 테라폼의 기본 구성과 용어 쉽게 정리 (Provider, Resource, Variable, Local, State, Output, Provisioner, Module, Data) (1) | 2023.12.31 |
[Terraform #01] 테라폼 쉽게 개념 정리 & 설명 (테라폼 기본 명령어, IaC, Infrastructure as Code) (1) | 2023.12.17 |