Terraform으로 서비스 사용량 리소스 관리

이 문서에서는 Terraform을 사용하여 할당량 재정의와 같은 서비스 사용량 리소스를 프로비저닝하는 방법을 설명합니다.

소비자 할당량 재정의를 관리하려면 google_service_usage_consumer_quota_override 스키마를 사용하여 Terraform 구성 파일에서 리소스를 지정하고 Terraform 명령어를 실행하여 변경사항을 적용할 수 있습니다.

시작하기 전에

이 튜토리얼에서 사용되는 용어를 설명하는 서비스 할당량 모델을 검토합니다.

Terraform 및 사용자 인증 정보를 구성합니다.

  1. Terraform을 설치합니다.

  2. Google Cloud SDK를 설치합니다.

  3. 애플리케이션 기본 사용자 인증 정보를 만듭니다.

    gcloud auth application-default login
    

소비자 할당량 재정의 생성

  1. 구성 파일을 만듭니다.

    1. 작업 디렉터리로 이동합니다.

    2. 다음 콘텐츠를 복사하여 main.tf라는 새 파일에 붙여넣습니다.

    resource "google_service_usage_consumer_quota_override" "regional_override" {
      provider       = google-beta
      dimensions = {
        region = "us-central1"
      }
      project        = PROJECT_ID
      service        = "libraryagent.googleapis.com"
      metric         = "libraryagent.googleapis.com%2Fread_requests_regional"
      limit          = "%2Fmin%2Fproject%2Fregion"
      override_value = "8"
      force          = true
    }
    
    resource "google_service_usage_consumer_quota_override" "cd_override" {
      provider       = google-beta
      dimensions = {
        author = "larry"
      }
      project        = PROJECT_ID
      service        = "libraryagent.googleapis.com"
      metric         = "libraryagent.googleapis.com%2Fborrows"
      limit          = "%2Fauthor%2Fproject"
      override_value = "8"
      force          = true
    }
    

    main.tf 파일에 배치한 콘텐츠에는 "regional_override""cd_override"라는 두 개의 소비자 재정의에 대한 구성 정보가 포함됩니다. 각 필드에 대한 설명은 인수 참조를 참조하세요.

  2. 작업 디렉터리를 초기화합니다.

    terraform init -upgrade
    
  3. Terraform 구성을 적용합니다.

    terraform apply
    

    Terraform은 현재 상태를 검사하고 구성 파일의 정보와 비교합니다. 비교가 완료되면 Terraform은 변경사항 목록을 인쇄한 다음 변경사항 적용을 진행하거나 취소할 수 있는 옵션을 제공합니다.

    GoSM: Setting up security policy for terraform binary.
    GoSM: Setting up security policy for terraform binary.
    google_service_usage_consumer_quota_override.cd_override: Refreshing state... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=]
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      + create
    
    Terraform will perform the following actions:
    
      # google_service_usage_consumer_quota_override.cd_override will be created
      + resource "google_service_usage_consumer_quota_override" "cd_override" {
          + dimensions     = {
              + "author" = "larry"
            }
          + force          = true
          + id             = (known after apply)
          + limit          = "%2Fauthor%2Fproject"
          + metric         = "libraryagent.googleapis.com%2Fborrows"
          + name           = (known after apply)
          + override_value = "8"
          + project        = "loas-linweic"
          + service        = "libraryagent.googleapis.com"
        }
    
      # google_service_usage_consumer_quota_override.regional_override will be created
      + resource "google_service_usage_consumer_quota_override" "regional_override" {
          + dimensions     = {
              + "region" = "us-central1"
            }
          + force          = true
          + id             = (known after apply)
          + limit          = "%2Fmin%2Fproject%2Fregion"
          + metric         = "libraryagent.googleapis.com%2Fread_requests_regional"
          + name           = (known after apply)
          + override_value = "8"
          + project        = "loas-linweic"
          + service        = "libraryagent.googleapis.com"
        }
    
    Plan: 2 to add, 0 to change, 0 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
    

    yes를 입력합니다.

    Enter a value: yes
    
    google_service_usage_consumer_quota_override.regional_override: Creating...
    google_service_usage_consumer_quota_override.cd_override: Creating...
    google_service_usage_consumer_quota_override.cd_override: Still creating... [10s elapsed]
    google_service_usage_consumer_quota_override.regional_override: Still creating... [10s elapsed]
    google_service_usage_consumer_quota_override.cd_override: Creation complete after 12s [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=]
    google_service_usage_consumer_quota_override.regional_override: Creation complete after 12s [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=]
    
    Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
    

소비자 할당량 재정의 수정

  1. 재정의를 수정하려면 main.tf 파일을 수정하고 override_value 필드의 값을 변경합니다.
  2. Terraform 구성을 적용합니다.

    terraform apply
    

소비자 할당량 재정의 삭제

  1. 소비자 할당량 재정의를 삭제하려면 main.tf 파일에서 해당 리소스 블록을 삭제합니다. 여러 재정의를 삭제할 수 있습니다. 재정의를 모두 삭제하려면 main.tf 파일이 비어 있는지 확인합니다.

  2. Terraform 구성을 적용합니다.

    terraform apply
    

    예를 들어 이전에 프로비저닝한 리전 재정의를 삭제하려면 다음을 실행합니다. 리소스를 참조하는 블록을 삭제합니다.

    resource "google_service_usage_consumer_quota_override" "regional_override" {
      ...
    }
    

    terraform apply 후 출력은 다음과 같습니다.

    GoSM: Setting up security policy for terraform binary.
    GoSM: Setting up security policy for terraform binary.
    google_service_usage_consumer_quota_override.regional_override: Refreshing state... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=]
    google_service_usage_consumer_quota_override.cd_override: Refreshing state... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=]
    
    An execution plan has been generated and is shown below.
    Resource actions are indicated with the following symbols:
      - destroy
    
    Terraform will perform the following actions:
    
      # google_service_usage_consumer_quota_override.regional_override will be destroyed
      - resource "google_service_usage_consumer_quota_override" "regional_override" {
          - dimensions     = {
              - "region" = "us-central1"
            } -> null
          - force          = true -> null
          - id             = "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=" -> null
          - limit          = "%2Fmin%2Fproject%2Fregion" -> null
          - metric         = "libraryagent.googleapis.com%2Fread_requests_regional" -> null
          - name           = "Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=" -> null
          - override_value = "8" -> null
          - project        = "loas-linweic" -> null
          - service        = "libraryagent.googleapis.com" -> null
        }
    
    Plan: 0 to add, 0 to change, 1 to destroy.
    
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
    
      Enter a value:
    

    yes를 입력하여 계속 진행합니다.

    Enter a value: yes
    
    google_service_usage_consumer_quota_override.regional_override: Destroying... [id=projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=]
    google_service_usage_consumer_quota_override.regional_override: Still destroying... [id=projects/loas-linweic/services/librarya...JyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=, 10s elapsed]
    google_service_usage_consumer_quota_override.regional_override: Destruction complete after 11s
    
    Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
    

상태 파일

작업 디렉터리에 terraform.tfstateterraform.tfstate이라는 두 개의 파일이 생성됩니다. 첫 번째 파일은 최신 terraform apply을 실행한 후 인프라의 현재 상태를 기록합니다. 두 번째 파일은 최신 terraform apply 이전의 상태를 기록합니다. Terraform 상태에 대한 자세한 내용은 해당 공식 문서를 참조하세요.

지금까지 이 페이지의 단계를 수행했다면 다음 콘텐츠는 terraform.tfstate 파일에 있고 cd_override는 인프라에 남아 있습니다.

  {
    "version": 4,
    "terraform_version": "0.12.31",
    "serial": 49,
    "lineage": "9dfbb2cc-7014-10ec-b8e1-ec9f36ea1acc",
    "outputs": {},
    "resources": [
      {
        "mode": "managed",
        "type": "google_service_usage_consumer_quota_override",
        "name": "cd_override",
        "provider": "provider.google-beta",
        "instances": [
          {
            "schema_version": 0,
            "attributes": {
              "dimensions": {
                "author": "larry"
              },
              "force": true,
              "id": "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "limit": "%2Fauthor%2Fproject",
              "metric": "libraryagent.googleapis.com%2Fborrows",
              "name": "Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "override_value": "8",
              "project": "loas-linweic",
              "service": "libraryagent.googleapis.com",
              "timeouts": null
            },
            "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
          }
        ]
      }
    ]
  }

terraform.tfstate.backup 파일에는 프로비저닝 해제 이전의 두 재정의가 모두 포함됩니다.

  {
    "version": 4,
    "terraform_version": "0.12.31",
    "serial": 47,
    "lineage": "9dfbb2cc-7014-10ec-b8e1-ec9f36ea1acc",
    "outputs": {},
    "resources": [
      {
        "mode": "managed",
        "type": "google_service_usage_consumer_quota_override",
        "name": "cd_override",
        "provider": "provider.google-beta",
        "instances": [
          {
            "schema_version": 0,
            "attributes": {
              "dimensions": {
                "author": "larry"
              },
              "force": true,
              "id": "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fborrows/limits/%2Fauthor%2Fproject/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "limit": "%2Fauthor%2Fproject",
              "metric": "libraryagent.googleapis.com%2Fborrows",
              "name": "Cg1RdW90YU92ZXJyaWRlGg8KBmF1dGhvchIFbGFycnk=",
              "override_value": "8",
              "project": "loas-linweic",
              "service": "libraryagent.googleapis.com",
              "timeouts": null
            },
            "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
          }
        ]
      },
      {
        "mode": "managed",
        "type": "google_service_usage_consumer_quota_override",
        "name": "regional_override",
        "provider": "provider.google-beta",
        "instances": [
          {
            "schema_version": 0,
            "attributes": {
              "dimensions": {
                "region": "us-central1"
              },
              "force": true,
              "id": "projects/loas-linweic/services/libraryagent.googleapis.com/consumerQuotaMetrics/libraryagent.googleapis.com%2Fread_requests_regional/limits/%2Fmin%2Fproject%2Fregion/consumerOverrides/Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=",
              "limit": "%2Fmin%2Fproject%2Fregion",
              "metric": "libraryagent.googleapis.com%2Fread_requests_regional",
              "name": "Cg1RdW90YU92ZXJyaWRlGhUKBnJlZ2lvbhILdXMtY2VudHJhbDE=",
              "override_value": "8",
              "project": "loas-linweic",
              "service": "libraryagent.googleapis.com",
              "timeouts": null
            },
            "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjoyNDAwMDAwMDAwMDAsImRlbGV0ZSI6MjQwMDAwMDAwMDAwLCJ1cGRhdGUiOjI0MDAwMDAwMDAwMH19"
          }
        ]
      }
    ]
  }