App Lifecycle Manager を使用してサービス インスタンスをデプロイする

App Lifecycle Manager を使用してサービス インスタンスをデプロイする方法について説明します。このクイックスタートの例では、サービス インスタンスは VM です。

始める前に

  1. Google アカウントにログインします。

    Google アカウントをまだお持ちでない場合は、新しいアカウントを登録します。

  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the App Lifecycle Manager, Artifact Registry, Infrastructure Manager, Developer Connect, Cloud Build, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    7. Click Continue.
    8. Click Done to finish creating the service account.

  6. Google Cloud CLI をインストールします。

  7. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  8. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  9. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  10. Verify that billing is enabled for your Google Cloud project.

  11. Enable the App Lifecycle Manager, Artifact Registry, Infrastructure Manager, Developer Connect, Cloud Build, and Cloud Storage APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  12. Create a service account:

    1. Ensure that you have the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). Learn how to grant roles.
    2. In the Google Cloud console, go to the Create service account page.

      Go to Create service account
    3. Select your project.
    4. In the Service account name field, enter a name. The Google Cloud console fills in the Service account ID field based on this name.

      In the Service account description field, enter a description. For example, Service account for quickstart.

    5. Click Create and continue.
    6. Grant the Project > Owner role to the service account.

      To grant the role, find the Select a role list, then select Project > Owner.

    7. Click Continue.
    8. Click Done to finish creating the service account.

  13. Google Cloud CLI をインストールします。

  14. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  15. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init

SaaS ランナー サービス アカウントに権限を付与する

App Lifecycle Manager は複数のサービス アカウントを使用します。前のセクションでサービス アカウントを作成しましたが、App Lifecycle Manager API を有効にすると、App Lifecycle Manager は別のサービス アカウントを作成します。

App Lifecycle Manager によって作成されたサービス アカウントは service-PROJECT-NUMBER@gcp-sa-saasservicemgmt.iam.gserviceaccount.com と呼ばれます。ここで、PROJECT-NUMBER はプロジェクト番号です。

次の操作を行って、このサービス アカウントに必要な権限を付与します。

  1. [App Lifecycle Manager] > [Overview] > [Get Started] ページを開きます。

    App Lifecycle Manager の利用を開始する

  2. App Lifecycle Manager アカウントに必要な権限が付与されていません」というメッセージが表示されたバナーで、[権限を付与] をクリックします。

Terraform 構成を使用してサービス インスタンスを定義する

Terraform 構成を使用して、サービス インスタンスにデプロイするインフラストラクチャを定義する必要があります。このクイックスタートでは、VM をデプロイします。

VM を定義する Terraform 構成を作成するには:

  1. ローカル マシンに terraform-vm という名前のディレクトリを作成します。

  2. terraform-vm 内に次の 4 つの Terraform ファイルを作成します。

    1. 次の内容の versions.tf という名前のファイル:

      terraform {
        required_version = "1.5.7"
        required_providers {
          google = {
            source  = "hashicorp/google"
            version = "~> 4.0"
          }
        }
      }
      
    2. 次の内容の outputs.tf という名前のファイル:

      output "instance_name" {
        description = "Name of the instance"
        value       = google_compute_instance.vm_instance.name
      }
      
      output "instance_external_ip" {
        description = "External IP of the instance"
        value       = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
      }
      
      output "instance_self_link" {
        description = "Self-link of the instance"
        value       = google_compute_instance.vm_instance.self_link
      }
      
    3. 次の内容の variables.tf という名前のファイル:

      variable "region" {
        description = "The Google Cloud region"
        type        = string
        default     = "us-central1"
      }
      
      variable "zone" {
        description = "The Google Cloud zone"
        type        = string
        default     = "us-central1-a"
      }
      
      variable "instance_name" {
        description = "Name for the Compute Engine instance"
        type        = string
        default = "saas-runtime-vm-instance"
      }
      
      variable "machine_type" {
        description = "Machine type for the Compute Engine instance"
        type        = string
        default = "e2-medium"
      }
      
      variable "disk_size" {
        description = "Boot disk size in GB"
        type        = number
        default = 10
      }
      
      variable "actuation_sa" {
        description = "The email of the Actuation Service Account"
        type        = string
      }
      
      variable "tenant_project_id" {
        description = "The project ID of the tenant project"
        type        = string
      }
      
      variable "tenant_project_number" {
        description = "The project number of the tenant project"
        type        = number
      }
      
    4. 次の内容の main.tf という名前のファイル:

      resource "google_compute_instance" "vm_instance" {
        project      = var.tenant_project_id
        name         = var.instance_name
        machine_type = var.machine_type
        zone         = var.zone
      
        boot_disk {
          initialize_params {
            image = "debian-cloud/debian-11"
            size  = var.disk_size
          }
        }
      
        network_interface {
          network = "default"
          access_config {
            # Ephemeral public IP - empty block is okay here
          }
        }
      
        tags = ["allow-ssh"]
      }
      
  3. これらの 4 つの Terraform 構成ファイルを含む zip アーカイブを作成するには、ターミナルで terraform-vm ディレクトリに移動し、次のコマンドを実行します。

    zip terraform-files.zip main.tf outputs.tf variables.tf versions.tf
    

    これで、4 つの Terraform 構成ファイルすべてを含む terraform-files.zip という名前の zip 形式のアーカイブが作成されました。

Artifact Registry にリポジトリを作成する

App Lifecycle Manager を使用するには、Artifact Registry にリポジトリが必要です。このリポジトリを作成する手順は次のとおりです。

  1. コンソールで、[Artifact Registry] に移動します。

    Artifact Registry に移動

  2. [リポジトリを作成] をクリックします。

  3. [名前] に「vm-quickstart-repo」と入力します。

  4. [形式] は [Docker] を選択したままにします。

  5. [リージョン] には us-central1 (Iowa) を選択します。

  6. [作成] をクリックします。

SaaS サービスの作成

デプロイする VM を定義する Terraform 構成とリポジトリがある。App Lifecycle Manager を使用して、デプロイ単位をモデル化し、VM をデプロイできるようになりました。

SaaS サービス リソースを作成する

  1. コンソールで、[App Lifecycle Manager] > [SaaS Offering] に移動します。

    [SaaS サービス] に移動

  2. [作成] をクリックします。

  3. [Name of the SaaS] フィールドに「vm-quickstart-saas-offering」と入力します。

  4. [リージョン] フィールドでリージョン us-central1 を選択し、[OK] をクリックします。

  5. [作成] をクリックします。

SaaS サービス用に選択したリージョンは、SaaS サービスのデプロイがホストされる場所です。このクイックスタートの例では、SaaS サービスは単一の VM であるため、これらのリージョンは VM がプロビジョニングされてホストされる場所です。

エンドユーザーがこれらの VM にアクセスする場合、ここで指定したリージョンにデプロイされた VM にアクセスします。

デプロイの単位をモデル化する

SaaS サービスをモデル化するには、ユニットの種類というコンポーネントを作成します。ユニットの種類によって、デプロイして管理するサービス内のコンポーネントが決まります。たとえば、VM のユニットの種類が 1 つあり、その VM で実行されているアプリケーションのユニットの種類がもう 1 つある場合があります。

このクイックスタートでは、VM 用の単位種類を 1 つ作成します。

ユニットの種類を作成するには:

  1. コンソールで、[App Lifecycle Manager] > [Unit Kinds] に移動します。

    [単位の種類] に移動

  2. [作成] を選択します。

  3. [ブループリントの作成] ページで、次の操作を行います。

    1. [アップロード] を選択します。
    2. VM を定義する Terraform 構成をアップロードするには、次の操作を行います。
      1. [ファイル選択ツール] フィールドで、[参照] を選択します。
      2. 前に作成した zip アーカイブ ファイルである terraform-files.zip に移動して選択します。
      3. [開く] を選択します。
    3. [次へ: ブループリントを構成] をクリックします。
  4. [ブループリントの保存] ページで、次の操作を行います。

    1. Artifact Registry を選択します。
      1. [Artifact Registry からリポジトリを選択] フィールドで、[参照] を選択します。
      2. 以前に作成したリポジトリである vm-quickstart-repo を選択します。
      3. [選択] をクリックします。
    2. [Artifact image name] フィールドに「vm-quickstart-blueprint」と入力します。
    3. [Infrastructure Manager の Terraform バージョン] で、[1.5.7] を選択します。
    4. [Cloud Build サービス アカウント] で、始める前にのセクションで作成したサービス アカウントを選択します。
    5. [次へ: ユニットの種類の詳細] をクリックします。
  5. [ユニットの種類のプロパティを構成する] ページで、次の操作を行います。

    1. [単位の種類名] に「vm-quickstart-unit-kind」と入力します。
    2. [SaaS サービス] で、前に作成した SaaS サービス リソースである vm-quickstart-saas-offering を選択します。
    3. [次へ: リリース構成] をクリックします。
  6. [リリース名] に「vm-quickstart-first-release」と入力します。

  7. [作成] をクリックします。

サービス インスタンスをプロビジョニングする

ユニットの種類に属するリソースをプロビジョニングするには、ユニットを作成します。ユニットを作成すると、App Lifecycle Manager は、ユニットの種類に関連付けられている Terraform 構成で定義されたリソースをプロビジョニングします。リソースは、SaaS サービスの一部である各リージョンにプロビジョニングされます。

このクイックスタートの例では、VM はリージョン us-central1 にプロビジョニングされます。

  1. ユニットを作成します。

    1. コンソールで、[App Lifecycle Manager] > [ユニット] に移動します。

      [ユニット] に移動

    2. [作成] を選択します。

    3. [ユニットを作成] ページで、次の操作を行います。

      1. [単位名] に「vm-quickstart-unit」と入力します。
      2. [SaaS サービス] で、以前に作成した SaaS サービス リソース(vm-quickstart-saas-offering)を選択します。
      3. [リージョン] の下で us-central1 を選択します。
      4. [単位の種類] で、先ほど作成した単位の種類(vm-quickstart-unit-kind)を選択します。
      5. [作成] を選択します。
  2. VM をプロビジョニングするには:

    1. [ユニットの詳細] ページで、[プロビジョニング] を選択します。
    2. [リリース] フィールドで、vm-quickstart-first-release を選択します。
    3. [サービス アカウント] で、始める前にのセクションで作成したサービス アカウントを選択します。
    4. テナント プロジェクトを追加します。

      1. [テナント プロジェクト変数を追加] を選択します。
      2. このクイックスタートで使用している Google Cloud プロジェクトを選択します。App Lifecycle Manager が VM をデプロイすると、このプロジェクトにデプロイされます。
    5. [プロビジョニング] を選択します。

App Lifecycle Manager は、SaaS サービスで指定したリージョンに VM をプロビジョニングします。ユニットは、ユニットで指定したリージョンのいずれかに作成できます。このクイックスタートでは、1 つのリージョン(us-central1)を指定しました。このリージョンに VM がプロビジョニングされます。

デプロイされた VM を表示する

これで、App Lifecycle Manager を使用して VM をデプロイしました。

このクイックスタートでデプロイした VM を表示するには:

  1. Google Cloud コンソールで、[App Lifecycle Manager] > [Units] > [Unit details] ページに移動します。

    [ユニット] に移動

  2. ユニットの名前(vm-quickstart-unit)をクリックします。

  3. [Unit details] ページで、次の操作を行います。

    1. 状態が次のようになっていることを確認します。

      • VM がプロビジョニングされている場合は Ready
      • オペレーションがまだ進行中の場合は、プロビジョニング
    2. [変数] セクションを開きます。

    3. [出力変数] に、インスタンスにアクセスするために使用できる外部 IP が表示されます。

  4. Compute Engine で VM を表示することもできます。

    1. コンソールで、[Compute Engine] > [VM インスタンス] ページに移動します。

      [VM インスタンス] に移動

    2. [VM インスタンス] に VM が表示されます。

クリーンアップ

このページで使用したリソースについて、 Google Cloud アカウントに課金されないようにするには、次の手順を実施します。

プロジェクトを削除する

ソリューションを新しい Google Cloud プロジェクトにデプロイした後、そのプロジェクトが不要になった場合は、次の手順で削除します。

  1. Google Cloud コンソールで [リソースの管理] ページに移動します。

    [リソースの管理] に移動

  2. プロジェクト リストで、削除するプロジェクトを選択し、[削除] をクリックします。
  3. プロンプトでプロジェクト ID を入力し、[シャットダウン] をクリックします。

次のステップ