ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Terraform Apply

    IaC/Terraform 기초 2023. 9. 5. 02:58

     

    terraform apply

     

    이번엔  terraform apply에 대해서 정리하는 시간을 가져보겠습니다.

    terraform apply는 현재 테라폼 코드를 실행하여 리소스들을 생성/수정/삭제 하는 명령어입니다.

    테라폼은 어떤 파일이 중요하다고 했죠?

    바로 terraform state 파일!! 기억나시죠??^^

     

    terraform apply를 하게 되면 무지성으로 테라폼 코드를 실행시키는 것이 아니라

    현재 state 파일과 테라폼 코드를 비교하여 아래처럼 실행합니다.

     

    상태 비교 state 파일에 있음 state 파일에 없음
    테라폼 코드에 있음 Resource Update
    (혹은 No Change)
    Resource Create
    테라폼 코드에 없음 Resource Remove No Changes

     

    심플하죠?

    terraform apply --help를 치면 다음과 같이 terraform apply와 관련한 option들을 살펴볼 수 있습니다.

     

    Usage: terraform [global options] apply [options] [PLAN]
    
      Creates or updates infrastructure according to Terraform configuration
      files in the current directory.
    
      By default, Terraform will generate a new plan and present it for your
      approval before taking any action. You can optionally provide a plan
      file created by a previous call to "terraform plan", in which case
      Terraform will take the actions described in that plan without any
      confirmation prompt.
    
    Options:
    
      -auto-approve          Skip interactive approval of plan before applying.
    
      -backup=path           Path to backup the existing state file before
                             modifying. Defaults to the "-state-out" path with
                             ".backup" extension. Set to "-" to disable backup.
    
      -compact-warnings      If Terraform produces any warnings that are not
                             accompanied by errors, show them in a more compact
                             form that includes only the summary messages.
    
      -destroy               Destroy Terraform-managed infrastructure.
                             The command "terraform destroy" is a convenience alias
                             for this option.
    
      -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.
    
      -input=true            Ask for input for variables if not directly set.
    
      -no-color              If specified, output won't contain any color.
    
      -parallelism=n         Limit the number of parallel resource operations.
                             Defaults to 10.
    
      -state=path            Path to read and save state (unless state-out
                             is specified). Defaults to "terraform.tfstate".
    
      -state-out=path        Path to write state to that is different than
                             "-state". This can be used to preserve the old
                             state.
    
      If you don't provide a saved plan file then this command will also accept
      all of the plan-customization options accepted by the terraform plan command.
      For more information on those options, run:
          terraform plan -help

     

    terraform plan이랑 비슷한 옵션들이 보이시나요?

    겹치지만 몇 가지 옵션들을 살펴보겠습니다.

     

    terraform apply -auto-approve
    테라폼 코드를 실행하기 전에 결과를 미리 보여주고 yes를 입력해야 실행되는 것을 생략하고 바로 apply를 진행합니다.

    terraform apply -backup=path
    기존 state 파일을 저장해두면 이전 상태로 rollback 할 수 있는데 그 위치를 지정합니다.

    terraform apply -destroy
    현재 state 파일에 있는 리소스를 모두 지웁니다. terraform destroy 명령어로도 가능합니다.

    terraform apply -lock-timeout=60s
    terraform state lock 이 걸려있다면 해당 시간만큼 대기했다가 테라폼을 실행합니다.

    terraform apply -parallelism=10
    병렬로 테라폼을 수행합니다. 즉, 동시에 여러 스레드를 돌려 빠르게 테라폼 실행을 하도록 합니다.
    대신 숫자가 높으면 테라폼을 실행하는 동안 그만큼 메모리를 차지합니다.

    terraform apply -state=path
    특정 state 파일을 기준으로. terraform apply를 실행합니다.

     

    terraform apply의 옵션들도 terraform plan의 옵션들과 마찬가지로 여러 옵션들을 혼합하여 사용할 수 있습니다.

     

    terraform에는 apply 후에 따로 rollback을 하는 명령어는 없기 때문에

    state 파일을 잘 관리하면서 apply를 해야 실수를 했을 경우에도 이전 state 파일로 apply를 해서 돌아갈 수 있습니다.

     

    지금까지 terraform apply 명령어를 살펴보았습니다.

    'IaC > Terraform 기초' 카테고리의 다른 글

    Terraform Backend  (0) 2023.09.12
    Terraform Provider  (0) 2023.09.12
    Terraform State  (0) 2023.09.03
    Terraform Plan  (0) 2023.09.03
    Terraform Init  (0) 2023.08.31
Designed by Tistory.