일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- 낙관적락
- 스프링 폼
- 연관관계
- CHECK OPTION
- dfs
- shared lock
- 데코레이터
- querydsl
- FetchType
- PS
- 다대일
- 비관적락
- 유니크제약조건
- eager
- 즉시로딩
- JPQL
- 동적sql
- fetch
- 힙
- 다대다
- execute
- exclusive lock
- 연결리스트
- SQL프로그래밍
- 스토어드 프로시저
- 이진탐색
- 일대다
- BOJ
- 지연로딩
- 백트래킹
- Today
- Total
흰 스타렉스에서 내가 내리지
Terraform variables - 코드를 효율적으로 쓰기 본문
# 결론
Terraform 에서 변수를 사용하기 위해서는,
1. 변수 정의 파일
2. 변수 값 주입 파일
을 따로 생성해야 한다.
# 개요
Terraform 은 HCL Syntax 를 가진 언어이다. 언어적 특성을 가지고 있기 때문에 변수를 정의하고, 주입해서 사용할 수 있다.
(HCL 은 json syntax 와 비슷하다)
# Variable Types
- string
- number
-boot
# Complex variable types
- list()
- set()
- map()
- object( { = , ... } )
- tuple ( [ , ... ] )
# 변수를 정의하기 - variables.tf
tf 어느 파일에서나 변수를 정의할 수는 있지만, 일반적으로는 variables.tf 파일을 만들어서 정의한다.
variable "image_id" {
type = string
}
variable "availability_zone_names" {
type = list(string)
default = ["us-west-1a"]
}
variable "ami_id_maps" {
type = map
default = {}
}
# 변수를 주입하기 - terraform.tfvars
정의한 변수에 값을 주입하기 위해 가장 일반적인 방법은 terraform.tfvars 파일을 생성하는 것이다.
Variable = Value 형태로 정의한다.
image_id = "ami-064c81ce3a290fde1"
availability_zone_names = ["us-west-1a","us-west-1b","us-west-1c"]
ami_id_maps = {
ap-northeast-2 = {
amazon_linux2 = "ami-010bf43fe22f847ed"
ubuntu_18_04 = "ami-061b0ee20654981ab"
}
us-east-1 = {
amazon_linux2 = "ami-0d29b48622869dfd9"
ubuntu_18_04 = "ami-0d324124b7b7eec66"
}
}
# tf 파일에서 변수를 사용하기
# provider.tf
provider "aws" {
region = var.aws_region
}
++
!info
terraform.tfvars 가 아닌 다른 방법은?
1. module block 주입
2. 사용자 stdin 주입
# 실습
선언
vim variables.tf
variable "aws_region" {
description = "region for aws."
}
주입
vim terraform.tfvars
aws_region = "ap-northeast-2"
사용
vim provider.tf
provider "aws" {
region = var.aws_region
}
# devops_gruop.tf 를 변수를 사용하게끔 바꿔보기
기존 devops_gruop.tf
resource "aws_iam_group" "joos_devops_group" {
name = "joos_devops_group"
}
resource "aws_iam_group_membership" "joos_devops_group_membership" {
name = aws_iam_group.joos_devops_group.name
users = [
aws_iam_user.joos-devops-user.name
]
group = aws_iam_group.joos_devops_group.name
}
vim variables.tf
variable "aws_region" {
description = "region for aws."
}
variable "iam_user_list" {
type = list(string)
}
vim terraform.tfvars
aws_region = "ap-northeast-2"
iam_user_list = ["joos-devops-user"]
변수 사용 후 devops_group.tf
resource "aws_iam_group" "joos_devops_group" {
name = "joos_devops_group"
}
resource "aws_iam_group_membership" "joos_devops_group_membership" {
name = aws_iam_group.joos_devops_group.name
users = var.iam_user_list
group = aws_iam_group.joos_devops_group.name
}
변수를 사용했으므로, 코드는 변경되었으나, `terraform plan` 명령어 실행 시 No changes 문구를 볼 수 있을 것이다.
# 추가 실습
vim variables.tf
variable "aws_region" {
description = "region for aws."
}
variable "iam_user_list" {
type = list(string)
}
variable "image_id" {
type = string
}
variable "availability_zone_names" {
type = list(string)
default = ["us-west-1a"]
}
variable "ami_id_maps" {
type = map
default = {}
}
vim terraform.tfvars
aws_region = "ap-northeast-2"
iam_user_list = ["joos-devops-user"]
image_id = "ami-064c81ce3a290fde1"
availability_zone_names = ["us-west-1a","us-west-1b","us-west-1c"]
ami_id_maps = {
ap-northeast-2 = {
amazon_linux2 = "ami-010bf43fe22f847ed"
ubuntu_18_04 = "ami-061b0ee20654981ab"
}
us-east-1 = {
amazon_linux2 = "ami-0d29b48622869dfd9"
ubuntu_18_04 = "ami-0d324124b7b7eec66"
}
}
vim outputs.tf
output "devops_0_image_id" {
value = var.image_id
}
output "devops_0_availability_zone_names" {
value = var.availability_zone_names
}
output "devops_0_ami_id_maps" {
value = var.ami_id_maps
}
output 을 작성하고 apply 를 하면 실제 리소스에는 변경사항이 없고, state 파일에 변수를 저장한다.
내가 output 에 저장해 놓은 것을 tf state 파일에 기록을 한다.
terraform output 은 나중에 remote state 로 사용하는 방법도 있지만 이렇게 변수명과 값을 직접 보고싶을 떄도 사용한다.
output 이 뭔지 모르겠다면?
'DevOps' 카테고리의 다른 글
Terraform output - 리소스의 속성이나 값을 외부로 노출 (0) | 2024.09.20 |
---|---|
Terraform Functions - 테라폼에서 사용하는 함수 (1) | 2024.09.20 |
클라우드 엔지니어와 DevOps 엔지니어의 차이점은? (1) | 2024.09.16 |
tfstate 파일이 실제 인프라와 일치하지 않을 때 - import (1) | 2024.09.06 |
Terraform backend - Terraform state 를 관리 (1) | 2024.09.06 |