Terraform を使用してワークフローを作成する

このクイックスタートでは、Terraform を使用して最初のワークフローを作成、デプロイ、実行する方法について説明します。Terraform は、コードを使用してクラウド インフラストラクチャを予想どおりに作成、変更、改善できる Infrastructure as Code ツールです。Terraform を使用して Google Cloud でインフラストラクチャをプロビジョニングする方法を学びます。

このクイックスタートのサンプル ワークフローは、リクエストを公開 API に送信してから、API のレスポンスを返します。

以下の手順を行います。

  1. Terraform を使用して Workflows API を有効にします。
  2. Terraform を使用してワークフロー用のサービス アカウントを作成します。
  3. Terraform を使用してワークフローを定義してデプロイします。
  4. Google Cloud CLI を使用してワークフローを実行します。

始める前に

組織で定義されているセキュリティの制約により、次の手順を完了できない場合があります。トラブルシューティング情報については、制約のある Google Cloud 環境でアプリケーションを開発するをご覧ください。

Cloud Shell には Terraform がすでに統合されています。Terraform をインストールする必要がある場合は、HashiCorp Terraform のドキュメントをご覧ください。

  1. Google Cloud アカウントにログインします。Google Cloud を初めて使用する場合は、アカウントを作成して、実際のシナリオでの Google プロダクトのパフォーマンスを評価してください。新規のお客様には、ワークロードの実行、テスト、デプロイができる無料クレジット $300 分を差し上げます。
  2. Google Cloud CLI をインストールします。
  3. gcloud CLI を初期化するには:

    gcloud init
  4. Google Cloud プロジェクトを作成または選択します

    • Google Cloud プロジェクトを作成します。

      gcloud projects create PROJECT_ID

      PROJECT_ID は、作成する Google Cloud プロジェクトの名前に置き換えます。

    • 作成した Google Cloud プロジェクトを選択します。

      gcloud config set project PROJECT_ID

      PROJECT_ID は、実際の Google Cloud プロジェクト名に置き換えます。

  5. Google Cloud プロジェクトで課金が有効になっていることを確認します

  6. Cloud Resource Manager and Identity and Access Management (IAM) API を有効にします。

    gcloud services enable cloudresourcemanager.googleapis.comiam.googleapis.com
  7. Google Cloud CLI をインストールします。
  8. gcloud CLI を初期化するには:

    gcloud init
  9. Google Cloud プロジェクトを作成または選択します

    • Google Cloud プロジェクトを作成します。

      gcloud projects create PROJECT_ID

      PROJECT_ID は、作成する Google Cloud プロジェクトの名前に置き換えます。

    • 作成した Google Cloud プロジェクトを選択します。

      gcloud config set project PROJECT_ID

      PROJECT_ID は、実際の Google Cloud プロジェクト名に置き換えます。

  10. Google Cloud プロジェクトで課金が有効になっていることを確認します

  11. Cloud Resource Manager and Identity and Access Management (IAM) API を有効にします。

    gcloud services enable cloudresourcemanager.googleapis.comiam.googleapis.com

Terraform 構成ファイルを作成する

main.tf という Terraform 構成ファイルを作成し、このクイックスタートで使用する Terraform の Google プロバイダ リソースを指定します。

参照変数、リソースの属性、関数の呼び出しなどの代入には補間を使用できます。

  1. ディレクトリを作成します。

    mkdir terraform
  2. terraform ディレクトリに移動します。

    cd terraform
  3. 新しいファイル main.tf をディレクトリに追加します。

    nano main.tf
  4. main.tf ファイルに次のリソースを追加します。

    1. プロジェクトの ID を割り当てます。

      provider "google" {
      project = "PROJECT_ID"
      }

      PROJECT_ID はプロジェクトの ID に置き換えます。

    2. Workflows API を有効にする

      # Enable Workflows API
      resource "google_project_service" "default" {
        service            = "workflows.googleapis.com"
        disable_on_destroy = false
      }

    3. ワークフローのサービス アカウントを作成します。

      # Create a dedicated service account
      resource "google_service_account" "default" {
        account_id   = "sample-workflows-sa"
        display_name = "Sample Workflows Service Account"
      }

    4. google_workflows_workflow リソースを使用してワークフローを定義します。

      # Create a workflow
      resource "google_workflows_workflow" "default" {
        name            = "sample-workflow"
        region          = "us-central1"
        description     = "A sample workflow"
        service_account = google_service_account.default.id
        labels = {
          env = "test"
        }
        user_env_vars = {
          url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam"
        }
        source_contents = <<-EOF
        # This is a sample workflow that you can replace with your source code
        #
        # The workflow does the following:
        # - Retrieves the current date from a public API and stores the
        #   response in `currentDate`
        # - Retrieves a list of Wikipedia articles from a public API related
        #   to the day of the week stored in `currentDate`
        # - Returns the list of articles in the workflow output
        #
        # Note that when you define workflows in Terraform, variables must be
        # escaped with two dollar signs ($$) and not a single sign ($)
      
        - getCurrentDate:
            call: http.get
            args:
                url: $${sys.get_env("url")}
            result: currentDate
        - readWikipedia:
            call: http.get
            args:
                url: https://en.wikipedia.org/w/api.php
                query:
                    action: opensearch
                    search: $${currentDate.body.dayOfWeek}
            result: wikiResult
        - returnOutput:
            return: $${wikiResult.body[1]}
      EOF
      
        depends_on = [google_project_service.default]
      }

      サンプル ワークフローでは、次の引数が使用されます。

      • name: ワークフローの名前。
      • region: ワークフローのロケーション
      • description: ワークフローの説明。
      • service_account: 最新のワークフロー バージョンに関連付けられたサービス アカウントのメールアドレスまたは一意の ID。このサービス アカウントはワークフローの ID を表し、ワークフローが持つ権限を決定します。ワークフローの作成時にサービス アカウントを指定しない場合、ワークフローは、デフォルトの Compute Engine サービス アカウントをその ID に対して使用します。詳細については、Google Cloud リソースにアクセスする権限をワークフローに付与するをご覧ください。
      • labels: このワークフローに割り当てる Key-Value ラベルペアのリスト。Google Cloud インスタンスの整理に役立てられます。詳細については、ラベルとはをご覧ください。
      • user_env_vars: このワークフロー リビジョンに関連付けられたユーザー定義の環境変数。詳細については、環境変数を使用するをご覧ください。
      • source_contents: 実行する Workflows コード。ファイルサイズの上限については、リソースの上限をご覧ください。

      その他のオプションの引数は次のとおりです。

      • crypto_key_name: 次の形式の Cloud Key Management Service 鍵のリソース ID。

        projects/PROJECT_NAME/locations/LOCATION/keyRings/KEY_RING/cryptoKeys/KEY_NAME

        詳細については、顧客管理の暗号鍵の使用をご覧ください。

      • call_log_level: このワークフローの実行中に呼び出しと呼び出しレスポンスに適用するロギングのレベル。指定できる値は次のとおりです。

        • CALL_LOG_LEVEL_UNSPECIFIED
        • LOG_ALL_CALLS
        • LOG_ERRORS_ONLY
        • LOG_NONE

        詳細については、コールロギングをご覧ください。

      • project: リソースが属するプロジェクトの ID。指定されていない場合は、プロバイダ プロジェクトが使用されます。

      • name_prefix: 指定された接頭辞で始まる一意の名前を作成します。これと name が指定されていない場合は、名前にはランダムな値が使用されます。

ワークフローを作成して実行する

Terraform リソースをデプロイしてワークフローを作成し、そのワークフローを実行します。

  1. ディレクトリで Terraform を初期化します。

    terraform init
    
  2. Terraform で示した変更が、想定されているプランと一致していることを確認します。

    terraform plan
    

    -out オプションを使用しないという注意事項は無視できます。

  3. ワークフローを作成します。

    terraform apply
    
  4. [Enter a value] プロンプトで、「yes」と入力してリソースの作成を続行します。

  5. ワークフローが作成されたことを確認します。

    gcloud workflows list --location us-central1
    

    出力例を以下に示します。

    NAME                                                                    STATE   REVISION_ID  UPDATE_TIME
    projects/project-name/locations/us-central1/workflows/sample-workflow   ACTIVE  000001-f9a   2024-02-24T13:38:58.353765906Z
    
  6. 必要に応じて、ワークフローを実行できます。

    gcloud workflows execute sample-workflow
    

クリーンアップ

このページで使用したリソースに対して Google Cloud アカウントで課金されないようにするには、Google Cloud プロジェクトとそのリソースを削除します。

  1. Terraform で作成したすべてのリソースを削除します。
    terraform destroy
  2. 作成したワークフローを削除します。
    gcloud workflows delete sample-workflow
    続行を確認するメッセージが表示されたら、「y」と入力します。
  3. また、Google Cloud プロジェクトを削除して、料金が発生しないようにすることもできます。Google Cloud プロジェクトを削除すると、そのプロジェクト内で使用されているすべてのリソースに対する課金が停止します。

      Google Cloud プロジェクトを削除します。

      gcloud projects delete PROJECT_ID

次のステップ