Terraform CLI 개요
Terraform CLI 개요
이 문서는 Terraform 커맨드 라인 인터페이스(CLI)가 무엇이고, 어떤 하위 명령들이 있는지 개괄적으로 설명해 드려요. terraform 명령이 어떻게 생겼는지, 기본 사용법과 몇 가지 유용한 글로벌 옵션까지 함께 살펴볼게요.
출처: 문서
본문
소개 (Introduction)
Terraform에 접근하는 커맨드 라인 인터페이스는 terraform 명령이며, terraform init이나 terraform plan 같은 다양한 하위 명령(subcommand)을 받아들여요.
문서에서는 terraform 커맨드 라인 도구를 "Terraform CLI"라고 부르는데, 이 용어는 Terraform 제품군에서 함께 쓰이는 다른 구성 요소, 예를 들어 HCP Terraform이나 여러 Terraform 프로바이더와 구분하기 위해 자주 사용돼요. 이들은 Terraform CLI와 별도로 개발되고 배포되어요.
현재 Terraform 버전에서 사용할 수 있는 명령 목록을 보려면 인자 없이 terraform을 실행하면 돼요:
Usage: terraform [global options] <subcommand> [args]
The available commands for execution are listed below.
The primary workflow commands are given first, followed by
less common or more advanced commands.
Main commands:
init Prepare your working directory for other commands
validate Check whether the configuration is valid
plan Show changes required by the current configuration
apply Create or update infrastructure
destroy Destroy previously-created infrastructure
All other commands:
console Try Terraform expressions at an interactive command prompt
fmt Reformat your configuration in the standard style
force-unlock Release a stuck lock on the current workspace
get Install or upgrade remote Terraform modules
graph Generate a Graphviz graph of the steps in an operation
import Associate existing infrastructure with a Terraform resource
login Obtain and save credentials for a remote host
logout Remove locally-stored credentials for a remote host
metadata Metadata related commands
modules Show all declared modules in a working directory
output Show output values from your root module
providers Show the providers required for this configuration
refresh Update the state to match remote systems
show Show the current state or a saved plan
state Advanced state management
taint Mark a resource instance as not fully functional
untaint Remove the 'tainted' state from a resource instance
version Show the current Terraform version
workspace Workspace management
Global options (use these before the subcommand, if any):
-chdir=DIR Switch to a different working directory before executing the
given subcommand.
-help Show this help output, or the help for a specified subcommand.
-version An alias for the "version" subcommand.
(여러분의 현재 Terraform 버전 출력은 위 예시와 다를 수 있어요.)
특정 명령에 대한 도움말을 보려면 해당 하위 명령과 함께 -help 옵션을 사용하면 돼요. 예를 들어 "validate" 하위 명령에 대한 도움말을 보려면 terraform validate -help라고 실행하면 돼요.
Terraform CLI에 내장된 인라인 도움말은 각 명령의 가장 중요한 특징을 설명해 줘요. 더 자세한 내용은 각 명령의 문서 페이지를 참고하세요.
-chdir로 작업 디렉터리 전환하기
Terraform을 실행하는 일반적인 방법은 먼저 루트 모듈의 .tf 파일이 있는 디렉터리로 이동(예: cd 명령)한 후, 추가 인자 없이도 Terraform이 그 파일들을 자동으로 찾도록 하는 거예요.
하지만 특히 Terraform을 자동화 스크립트로 감싸는 경우에는 루트 모듈 디렉터리가 아닌 다른 디렉터리에서 Terraform을 실행하는 게 편리할 수 있어요. 이를 위해 Terraform은 실행하려는 하위 명령 이름 앞에 넣을 수 있는 글로벌 옵션 -chdir=...을 지원해요:
terraform -chdir=environments/production apply
chdir 옵션은 주어진 하위 명령을 실행하기 전에 Terraform의 작업 디렉터리를 지정된 디렉터리로 변경하라고 지시해요. 즉, Terraform이 보통 현재 작업 디렉터리에서 읽거나 쓰는 모든 파일을 대신 지정된 디렉터리에서 읽거나 쓰게 돼요.
-chdir=...을 지정해도 Terraform이 원래 작업 디렉터리를 사용하는 두 가지 예외가 있어요:
- CLI 구성의 설정은 특정 하위 명령을 위한 것이 아니며, Terraform은
-chdir옵션에 반응하기 전에 그것들을 처리해요. - 구성의 일부로 원래 작업 디렉터리의 파일을 사용해야 하는 경우, 구성에서
path.cwd를 참조하면 재정의된 작업 디렉터리 대신 원래 작업 디렉터리가 생성돼요. 루트 모듈 디렉터리를 얻으려면path.root를 사용하세요.
셸 탭 자동 완성 (Shell Tab-completion)
bash나 zsh를 명령 셸로 사용한다면 Terraform은 모든 명령 이름과 일부 명령 인자에 대해 탭 자동 완성을 제공할 수 있어요.
셸 프로파일에 필요한 명령을 추가하려면 다음 명령을 실행하세요:
terraform -install-autocomplete
설치 후에는 완성 기능이 활성화되기 전에 셸을 재시작하거나 프로파일 스크립트를 다시 읽어야 해요.
셸 프로파일에서 수동으로 수정하지 않았다고 가정할 때 완성 훅을 제거하려면 다음 명령을 실행하세요:
terraform -uninstall-autocomplete
업그레이드 및 보안 공지 확인
Terraform CLI 명령은 HashiCorp 서비스인 Checkpoint와 상호작용하여 새 버전 가용성과 현재 버전에 대한 중요한 보안 공지를 확인해요.
이 효과를 볼 수 있는 곳 중 하나가 terraform version인데, 기본적으로 출력에서 더 새로운 버전이 있을 때 표시해 줘요.
Checkpoint에는 사용자나 호스트를 식별할 수 없는 익명 정보만 전송돼요. 경고 메시지의 중복을 줄이는 데 도움이 되는 익명 ID가 전송되며, 익명 ID와 checkpoint 사용 자체는 모두 완전히 선택 사항이고 비활성화할 수 있어요.
CHECKPOINT 자체는 CHECKPOINT_DISABLE 환경 변수에 비어 있지 않은 값을 설정하면 모든 HashiCorp 제품에 대해 완전히 비활성화할 수 있어요.
또는 CLI 구성 파일의 설정을 사용해 checkpoint 기능을 비활성화할 수 있어요. 이 파일에서 지원되는 checkpoint 관련 설정은 다음과 같아요:
- disable_checkpoint -
true로 설정하면 checkpoint 호출을 완전히 비활성화해요. 위에서 설명한CHECKPOINT_DISABLE환경 변수와 비슷해요. - disable_checkpoint_signature -
true로 설정하면 checkpoint 요청에서 익명 서명 사용을 비활성화해요. 이렇게 하면 Terraform이 보안 공지를 확인하지만 요청에 익명 서명은 보내지 않아요.
Terraform이 사용하는 Checkpoint 클라이언트 코드는 관심 있는 누구든 검토할 수 있도록 공개되어 있어요.