IaC/Terraform 기초
Terraform Plan
Cloudy Allen
2023. 9. 3. 09:10
terraform plan
이번엔 terraform plan 명령어에 대해서 알아보겠습니다.
terraform plan을 한마디로 정의하면 terraform apply의 결과를 미리 보여주는 명령어라고 할 수 있습니다.
즉, 테라폼 코드를 실행했을 때, 그 결과를 미리보기로 보여주는 명령어라고 이해하면 됩니다.
그러면 바로 terraform apply를 하면 되지 왜 terraform plan을 해야 하나요? 라는 질문을 할 수도 있을 겁니다.
terraform plan을 하면 terraform apply를 통해 생기는 변화를 미리 확인하고 테라폼 코드를 수정할 수 있습니다.
예를 들면, terraform plan을 통해 미리 결과를 찍었더니
내가 원하지 않는 리소스 삭제가 일어난다 혹은 내가 원하지 않는 리소스의 수정이 일어난다면
테라폼 코드를 수정하고 apply를 할 수 있는 것이죠.
그리고 terraform plan을 찍었을 때 테라폼 코드에 문법적인 에러가 있다면, plan에서 미리 확인할 수 있습니다.
(사실 terraform plan을 하지 않아도 terraform apply를 하면 apply하기 전에 미리 결과를 보여주고 승인을 요청하는 페이지가 뜨긴 합니다.)
terraform plan 명령어에서 추가할 수 있는 option들은 다음과 같습니다.
Usage: terraform [global options] plan [options]
Generates a speculative execution plan, showing what actions Terraform
would take to apply the current configuration. This command will not
actually perform the planned actions.
You can optionally save the plan to a file, which you can then pass to
the "apply" command to perform exactly the actions described in the plan.
Plan Customization Options:
The following options customize how Terraform will produce its plan. You
can also use these options when you run "terraform apply" without passing
it a saved plan, in order to plan and apply in a single command.
-destroy Select the "destroy" planning mode, which creates a plan
to destroy all objects currently managed by this
Terraform configuration instead of the usual behavior.
-refresh-only Select the "refresh only" planning mode, which checks
whether remote objects still match the outcome of the
most recent Terraform apply but does not propose any
actions to undo any changes made outside of Terraform.
-refresh=false Skip checking for external changes to remote objects
while creating the plan. This can potentially make
planning faster, but at the expense of possibly planning
against a stale record of the remote system state.
-replace=resource Force replacement of a particular resource instance using
its resource address. If the plan would've normally
produced an update or no-op action for this instance,
Terraform will plan to replace it instead. You can use
this option multiple times to replace more than one object.
-target=resource Limit the planning operation to only the given module,
resource, or resource instance and all of its
dependencies. You can use this option multiple times to
include more than one object. This is for exceptional
use only.
-var 'foo=bar' Set a value for one of the input variables in the root
module of the configuration. Use this option more than
once to set more than one variable.
-var-file=filename Load variable values from the given file, in addition
to the default files terraform.tfvars and *.auto.tfvars.
Use this option more than once to include more than one
variables file.
Other Options:
-compact-warnings If Terraform produces any warnings that are not
accompanied by errors, shows them in a more compact form
that includes only the summary messages.
-detailed-exitcode Return detailed exit codes when the command exits. This
will change the meaning of exit codes to:
0 - Succeeded, diff is empty (no changes)
1 - Errored
2 - Succeeded, there is a diff
-input=true Ask for input for variables if not directly set.
-lock=false Don't hold a state lock during the operation. This is
dangerous if others might concurrently run commands
against the same workspace.
-lock-timeout=0s Duration to retry a state lock.
-no-color If specified, output won't contain any color.
-out=path Write a plan file to the given path. This can be used as
input to the "apply" command.
-parallelism=n Limit the number of concurrent operations. Defaults to 10.
-state=statefile A legacy option used for the local backend only. See the
local backend's documentation for more information.
여기서 몇 가지 옵션만 간단하게 살펴보겠습니다.
terraform plan -destroy
terraform destroy 를 했을 때 어떤 리소스들이 삭제되는지 결과를 미리 보여줍니다.
terraform plan -target=resource
terraform apply를 했을 때 특정 리소스가 어떻게 변하는지 결과를 미리 보여줍니다.
terraform plan -var-file=filename(or file path)
terraform apply를 할 때 특정 variable 파일을 적용한 결과를 미리 보여줍니다.
terraform plan -lock=false
테라폼 실행 시 lock이 걸렸을 때에도 이를 무시하고 plan 을 실행하여 결과를 보여줍니다.
terraform plan -lock-timeout=00s
테라폼 실행 시 lock이 걸렸을 때 해당 시간만큼 대기하다가 중간에 lock이 풀리면 plan을 실행합니다.
terraform plan -out=path
terraform plan을 파일로 해당 경로에 저장합니다. plan 파일은 이 plan 파일대로 apply에 적용할 때 활용될 수 있습니다.
terraform plan -parallelism=n
terraform plan을 실행하는 병렬실행의 수를 조절합니다.
병렬실행의 수를 늘리면 성능이 일부 개선될 수 있지만,
그만큼 메모리를 많이 사용하는 이슈가 있기 때문에 잘 조절해야합니다. 기본값은 10입니다.
terraform plan -state=statefile
클라우드의 저장소에 원격으로 state파일을 관리하고 있는 상태에서 이 옵션으로 로컬에 있는 특정 state 파일을 terraform plan에서 읽는 state 파일로 지정할 수 있습니다. legacy 옵션입니다.
각 옵션들은
terraform plan -var-file='/path_to_var' -lock-timeout=300s -parallelism=30
이렇게 여러 옵션들을 혼합하여 사용할 수 있습니다.
여기까지 terraform plan 명령어에 대해서 살펴봤습니다.