TheKoguryo's 기술 블로그

Version 2023.04.29

14.2.5 다중 환경을 위한 workspace 사용하기

OCI는 여러 Region을 제공하고 있습니다. 필요에 따라 동일 자원을 여러 Region에 한꺼번에 만들어야 하는 경우가 있습니다. 하지만 앞서 실습에서 잠깐 보았겠지만, terraform 설정이 실행되면 기본으로 해당 폴더에 terraform.tfstate로 상태를 관리하게되어 동일 Terraform 설정을 그대로 사용할 경우 상태파일이 충돌나서 원하는 데로 동작하지 않게 됩니다. Terraform 설정 폴더 자체를 통채로 복사해서 대상 Region 별로 사용해도 되지만, 관리가 번거롭게 됩니다. 그래서 Terraform에서는 workspace만 개념을 제공하고, 기본적으로 default workspace를 사용하게 됩니다.

Terraform Configuration 파일

이전 실습에서 사용한 vcn 만들기 설정을 그대로 사용합니다.

Workspace 만들기

  • terraform workspace 구문

    # workspace 조회하기
    terraform workspace list
    
    # 새로운 workspace 만들기
    terraform workspace new {이름}
    
    # 해당 worksapce 삭제하기
    terraform workspace delete {이름}
    
    # 현재 workspace를 전환하기
    terraform workspace select {이름}
    
  1. 새로운 workspace를 만듭니다.

    terraform workspace new ap-seoul-1
    terraform workspace new ap-chuncheon-1
    
    • 예시

      [opc@bastion-host example_vcn]$ terraform workspace list
      * default
      
      [opc@bastion-host example_vcn]$ terraform workspace new ap-seoul-1
      Created and switched to workspace "ap-seoul-1"!
      
      You're now on a new, empty workspace. Workspaces isolate their state,
      so if you run "terraform plan" Terraform will not see any existing state
      for this configuration.
      [opc@bastion-host example_vcn]$ terraform workspace new ap-chuncheon-1
      Created and switched to workspace "ap-chuncheon-1"!
      
      You're now on a new, empty workspace. Workspaces isolate their state,
      so if you run "terraform plan" Terraform will not see any existing state
      for this configuration.
      [opc@bastion-host example_vcn]$ terraform workspace list
        default
      * ap-chuncheon-1
        ap-seoul-1
      
      [opc@bastion-host example_vcn]$
      

1번째 workspace: ap-seoul-1에서 terraform 실행하기

  1. workspace를 먼저 전환하고 vcn을 만드는 terraform을 실행합니다.

    [opc@bastion-host example_vcn]$ terraform workspace select ap-seoul-1
    Switched to workspace "ap-seoul-1".
    [opc@bastion-host example_vcn]$ terraform apply -var="region=ap-seoul-1"
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # oci_core_virtual_network.vcn1 will be created
      + resource "oci_core_virtual_network" "vcn1" {
          + cidr_block               = "10.0.0.0/16"
          + cidr_blocks              = (known after apply)
          + compartment_id           = "ocid1.compartment.oc1..aaaaaaaa54ryitndueosfezrxvxvcuosutofi2d6f53rbgwz2dpqrgeci7lq"
          + default_dhcp_options_id  = (known after apply)
          + default_route_table_id   = (known after apply)
          + default_security_list_id = (known after apply)
          + defined_tags             = (known after apply)
          + display_name             = "vcn1"
          + dns_label                = "vcn1"
          + freeform_tags            = (known after apply)
          + id                       = (known after apply)
          + ipv6cidr_blocks          = (known after apply)
          + is_ipv6enabled           = (known after apply)
          + state                    = (known after apply)
          + time_created             = (known after apply)
          + vcn_domain_name          = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Changes to Outputs:
      + vcn1_ocid = [
          + (known after apply),
        ]
    
    Do you want to perform these actions in workspace "ap-seoul-1"?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    oci_core_virtual_network.vcn1: Creating...
    oci_core_virtual_network.vcn1: Creation complete after 1s [id=ocid1.vcn.oc1.ap-seoul-1.amaaaaaavsea7yiamf4ktoajkby4sh45zx4e52cwscm6xljilibl5o3prpkq]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    vcn1_ocid = [
      "ocid1.vcn.oc1.ap-seoul-1.amaaaaaavsea7yiamf4ktoajkby4sh45zx4e52cwscm6xljilibl5o3prpkq",
    ]
    [opc@bastion-host example_vcn]$
    
  2. 결과 확인

    OCI 콘솔에서 서울 Region으로 변경하면 VCN이 생성된 것을 확인할 수 있습니다.

    image-20220118215349370

2번째 workspace: ap-chuncheon-1에서 terraform 실행하기

  1. 동일한 위치에서 workspace를 먼저 전환하고 vcn을 만드는 terraform을 동일하게 실행합니다.

    [opc@bastion-host example_vcn]$ terraform workspace select ap-chuncheon-1
    Switched to workspace "ap-chuncheon-1".
    [opc@bastion-host example_vcn]$ terraform apply -var="region=ap-chuncheon-1"
    
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # oci_core_virtual_network.vcn1 will be created
      + resource "oci_core_virtual_network" "vcn1" {
          + cidr_block               = "10.0.0.0/16"
          + cidr_blocks              = (known after apply)
          + compartment_id           = "ocid1.compartment.oc1..aaaaaaaa54ryitndueosfezrxvxvcuosutofi2d6f53rbgwz2dpqrgeci7lq"
          + default_dhcp_options_id  = (known after apply)
          + default_route_table_id   = (known after apply)
          + default_security_list_id = (known after apply)
          + defined_tags             = (known after apply)
          + display_name             = "vcn1"
          + dns_label                = "vcn1"
          + freeform_tags            = (known after apply)
          + id                       = (known after apply)
          + ipv6cidr_blocks          = (known after apply)
          + is_ipv6enabled           = (known after apply)
          + state                    = (known after apply)
          + time_created             = (known after apply)
          + vcn_domain_name          = (known after apply)
        }
    
    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Changes to Outputs:
      + vcn1_ocid = [
          + (known after apply),
        ]
    
    Do you want to perform these actions in workspace "ap-chuncheon-1"?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value: yes
    
    oci_core_virtual_network.vcn1: Creating...
    oci_core_virtual_network.vcn1: Creation complete after 0s [id=ocid1.vcn.oc1.ap-chuncheon-1.amaaaaaavsea7yiamplordafqo25ulky46sjoz74r724wxnd5dyplya3ptoq]
    
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    vcn1_ocid = [
      "ocid1.vcn.oc1.ap-chuncheon-1.amaaaaaavsea7yiamplordafqo25ulky46sjoz74r724wxnd5dyplya3ptoq",
    ]
    [opc@bastion-host example_vcn]$
    
  2. 결과 확인

    OCI 콘솔에서 춘천 Region으로 변경하면 VCN이 생성된 것을 확인할 수 있습니다.

    image-20220118215527892

  3. Terraform State 확인

    workspace를 사용하는 경우 아래와 같이 terraform.tfstate.d 폴더 밑에 workspace 별 폴더 밑에 terrafrom.tfstate 파일이 생성되어 workspace 별로 상태를 관리하는 것을 알 수 있습니다.

    [opc@bastion-host example_vcn]$ ls terraform.tfstate.d/ -la
    total 4
    drwxr-xr-x. 4 opc opc   46 Jan 18 12:39 .
    drwxrwxr-x. 4 opc opc 4096 Jan 18 12:47 ..
    drwxr-xr-x. 2 opc opc   31 Jan 18 12:51 ap-chuncheon-1
    drwxr-xr-x. 2 opc opc   31 Jan 18 12:47 ap-seoul-1
    [opc@bastion-host example_vcn]$ ls terraform.tfstate.d/ap-seoul-1/ -la
    total 4
    drwxr-xr-x. 2 opc opc   31 Jan 18 12:47 .
    drwxr-xr-x. 4 opc opc   46 Jan 18 12:39 ..
    -rw-rw-r--. 1 opc opc 2147 Jan 18 12:47 terraform.tfstate
    [opc@bastion-host example_vcn]$ ls terraform.tfstate.d/ap-chuncheon-1/ -la
    total 4
    drwxr-xr-x. 2 opc opc   31 Jan 18 12:51 .
    drwxr-xr-x. 4 opc opc   46 Jan 18 12:39 ..
    -rw-rw-r--. 1 opc opc 2168 Jan 18 12:51 terraform.tfstate
    [opc@bastion-host example_vcn]$
    


이 글은 개인으로서, 개인의 시간을 할애하여 작성된 글입니다. 글의 내용에 오류가 있을 수 있으며, 글 속의 의견은 개인적인 의견입니다.

Last updated on 10 May 2023