ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Terraform Init

    IaC/Terraform 기초 2023. 8. 31. 08:33

     

    그럼 이제 테라폼의 기본적인 명령어를 살펴보겠습니다.

    커맨드창에 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
      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
      test          Experimental support for module integration testing
      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 은 terraform 뒤에 명령어를 붙이고 그 다음에 옵션을 붙여서 사용합니다.

    주요 명령어를 위주로 어떤 역할을 하는 명령어인지 살펴보겠습니다.

     

    terraform init

     

    terraform init을 우선 실행해야 다른 메인 Command (ex. apply, plan, ...)를 실행할 수 있습니다.

    terraform init을 통해 기본적인 테라폼 코드가 실행될 환경을 세팅하는 것이지요.

    terraform init을 하면 다음과 같은 4가지 환경이 세팅됩니다.

     

      1. Backend Initialization
      2. Provider Plugin Installation
      3. Module Installation
      4. State Initialization

     

     

      1. Backend Initialization

     

    먼저, Backend에 대한 환경을 설정합니다. 그런데 여기서 Backend 가 무엇일까요?

    앞선 글에서 Terraform 에서 state 파일을 관리하는 것이 중요하다고 말씀드렸죠.

    이 중요한 state 파일의 위치가 바로 Backend입니다.

     

    테라폼의 state 파일은 일반적으로 AWS S3나 GCP의 GCS와 같은 클라우드의 Storage 서비스를 이용하여 보관 관리합니다.

    왜냐하면 Remote 환경에서 저장해 두어야 모두가 같은 state 파일을 공유할 수 있기 때문입니다.

    state 파일을 각각의 클라우드 Storage 서비스에 저장하기 위해서는 backend.tf 파일을 만들어서

    state파일을 어디서 관리할지 정할 수 있습니다.

    이 부분은 나중에 간단하게 클라우드별 저장소를 설정하는 예시로 설명을 드리겠습니다.

     

     

      2. Provider Plugin Installation

     

    terraform init을 하면 Provider Plugin을 설치합니다.

    Provider는 테라폼이 어떤 클라우드 혹은 어떤 서비스에 대해서 인프라를 구성할 것인가에 대해서 설정하는 부분입니다.

    Provider를 AWS로 설정하면 AWS에 테라폼을 실행하여 리소스를 생성/관리하겠다는 의미이고

    이 때 테라폼은 AWS에서 제공하는 API를 사용하여 이를 수행합니다.

     

     

    3. Module Installation

     

    module을 설치하고 읽습니다. module은 테라폼 코드의 묶음으로 보면 됩니다.

    이를테면 AWS에서 VPC를 생성할 때 서브넷, 보안그룹 등 다양한 설정이 가능하고 이를 위해서는 여러 테라폼 리소스가 필요합니다.

    이러한 VPC를 생성하기 위한 여러 테라폼 코드를 묶어서 하나의 모듈로 만들고 그 모듈을 실행하면

    미리 정해둔 모듈에 따라 VPC를 반복적으로 생성하는 것이 가능해집니다.

     

     

    4. State Initialization

     

    State 파일의 초기 환경을 세팅합니다.

    만약 클라우드의 원격 저장소에 저장된 state 파일도 지정되어 있지 않고, 로컬에도 state 파일이 없는 상태라면

    테라폼은 현재 로컬(혹은 backend.tf 에서 지정된 저장소)에 새로운 terraform state 파일을 만듭니다. 

    그리고 새로 만들어진 state와 현재 테라폼 코드에 작성된 리소스들을 비교하여 클라우드에 리소스들을 생성하게 됩니다.

     

    다음에는 테라폼에서 다른 중요한 명령어 중 하나인 terraform plan과  apply에 대해서 설명해보겠습니다.

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

    Terraform Apply  (0) 2023.09.05
    Terraform State  (0) 2023.09.03
    Terraform Plan  (0) 2023.09.03
    Terraform Install  (0) 2023.08.31
    Terraform이란?  (0) 2023.08.31
Designed by Tistory.