terraform에서 전역변수를 사용할 때 보통은 terraform.tfvars 를 사용한다
terraform.tfvars 를 사용하게 되면 apply시에 자동으로 파일을 읽어 들여 전역변수를 정의하게 된다.
만약 tfvars 파일이 terraform.tfvars 가 아니라 global.tfvars 일 경우 읽어들이지 못한다
그럼 방법이 없는가?
아니다 global.auto.tfvars 를 사용하면 된다
허나 이 역시 문제는 있다
apply 하는 디렉토리 위치에 존재해야만 읽어들일 수 있다
그럼 global.tfvars가 외부 디렉토리에 있다면 어떻게 해야 할까
apply 명령어 뒤에 -var-file="../global.tfvars" 를 통해 상대경로 혹은 절대경로의 global.tfvars 파일 경로를 입력 하면 된다.
매번 ....
사실 github action 이나 gitlab-runner를 사용하면 되는걸 알고 있지만 나는 터미널에서 자주 사용하기 때문에
터미널에서 이 귀찮음을 해소할 수 있는 방법을 찾기로 했다
바로 커맨드랩핑...
커맨드 랩핑의 원리는 간단하다.
alias를 사용하여 terraform 명령어가 실행 되었을 때 특정 커맨드로 바꾸면 된다
하여 나는 이렇게 구성했다
terraform 명령어가 실행되면 ~/.zshrc 의 alias 함수를 통해 특정 스크립트를 동작시킨다.
그리고 그 특정 스크립트에서 명령을 처리 하도록 했다
아래는 ~/.zshrc 에 등록한 함수 이다
find_from_main() {
local file_to_find=$1
local current_dir=$(pwd)
# main 디렉토리로 이동
while [[ "$current_dir" != "/" ]]; do
if [[ "$current_dir" =~ /main$ ]]; then
break
fi
current_dir=$(dirname "$current_dir")
done
# main 디렉토리에서 파일 찾기 시작
if [[ "$current_dir" =~ /main$ ]]; then
local found=$(find "$current_dir" -type f -name "$file_to_find" -print -quit)
if [[ -n "$found" ]]; then
echo "$found"
return 0
fi
fi
return 1
}
unalias terraform 2>/dev/null
# Terraform 실행 함수 정의
terraform() {
# 찾을 파일 이름 설정
local filename="wrapper.sh"
# 파일 검색
WRAPPER_PATH=$(find_from_main "$filename")
# 파일이 존재하는 경우 실행
if [[ -f "$WRAPPER_PATH" ]]; then
echo "find run wrapper.sh"
"$WRAPPER_PATH" "$@"
else
echo "find not wrapper.sh"
command terraform "$@"
fi
}
- terraform 명령어가 실행되면 명령어를 실행한 디렉토리에서 최상위 디렉토리로 이동한다
최상위 디렉토리는 main 으로 고정 했다 main 디렉토리에서 wrapper.sh 스크립트를 찾아 실행한다
그 다음은 wrapper.sh 에서 처리하게 된다
아래는 wrapper.sh 의 구성 내용이다
#!/bin/bash
BACKEND_CONF="backend.conf"
VAR_FILE="global.tfvars"
find_from_main() {
local file_to_find=$1
local current_dir=$(pwd)
# main 디렉토리로 이동
while [[ "$current_dir" != "/" ]]; do
if [[ "$current_dir" =~ /main$ ]]; then
break
fi
current_dir=$(dirname "$current_dir")
done
# main 디렉토리에서 파일 찾기 시작
if [[ "$current_dir" =~ /main$ ]]; then
local found=$(find "$current_dir" -type f -name "$file_to_find" -print -quit)
if [[ -n "$found" ]]; then
echo "$found"
return 0
fi
fi
return 1
}
# Terraform 명령어 래핑 스크립트
case "$1" in
init)
# init 명령어에 -backend-config 옵션 추가
BACKEND_CONFIG_PATH=$(find_from_main "$BACKEND_CONF")
if [ -n "$BACKEND_CONFIG_PATH" ] && [ -f "$BACKEND_CONFIG_PATH" ]; then
command terraform init -backend-config="$BACKEND_CONFIG_PATH" "${@:2}"
else
command terraform init "${@:2}"
fi
;;
plan|apply|destroy|refresh|import)
# 기타 명령어에 -var-file 옵션 추가
VAR_FILE_PATH=$(find_from_main "$VAR_FILE")
if [ -n "$VAR_FILE_PATH" ] && [ -f "$VAR_FILE_PATH" ]; then
command terraform "$1" -var-file="$VAR_FILE_PATH" "${@:2}"
else
command terraform "$1" "${@:2}"
fi
;;
*)
command terraform "$@"
;;
esac
echo "wrapper running"
- wrapper.sh가 실행되면 각 명령어 뒤에 붙게 될 global.tfvars 파일을 찾아 절대경로를 -var-file 에 적용하여 명령을 실행한다. 이 역시 apply 를 하는 디렉토리의 최상위 main 폴더에서부터 global.tfvars 파일을 찾게된다.
이렇게 하면 terraform apply 시 따로 -var-file 을 넣어주지 않아도 자동으로 실행된다
'클라우드 > Azure-Terraform' 카테고리의 다른 글
aks 생성시 helm 과 yaml 배포 (0) | 2024.06.18 |
---|---|
k8s 관련 modules 구성 (0) | 2024.06.18 |
Azure terraform provisioning 구현 (0) | 2024.06.18 |
Azure terraform modules 구성 (0) | 2024.06.17 |