クイックスタート: Terraform を使用して VM インスタンスを作成する

このクイックスタートでは、Terraform を使用して Compute Engine 仮想マシン(VM)インスタンスを作成し、その VM インスタンスに接続する方法について説明します。

Hashicorp Terraform は、クラウド インフラストラクチャのプロビジョニングと管理に使用できる Infrastructure as Code(IaC)ツールです。Google Cloud 用の Terraform プロバイダ(Google Cloud プロバイダ)を使用すると、Google Cloud インフラストラクチャのプロビジョニングと管理を行えます。

始める前に

  1. gcloud CLI と Terraform で設定済みのオンライン ターミナルを使用するには、Cloud Shell を有効にします。

    このページの下部で Cloud Shell セッションが開始され、コマンドライン プロンプトが表示されます。セッションが初期化されるまで数秒かかることがあります。

  2. Create or select a Google Cloud project.

    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Compute Engine API:

    gcloud services enable compute.googleapis.com
  5. Grant roles to your user account. Run the following command once for each of the following IAM roles: roles/compute.instanceAdmin.v1

    gcloud projects add-iam-policy-binding PROJECT_ID --member="USER_IDENTIFIER" --role=ROLE

環境を準備する

  1. Terraform サンプルを含む GitHub リポジトリのクローンを作成します。

    git clone https://github.com/terraform-google-modules/terraform-docs-samples.git --single-branch
    
  2. クイックスタート サンプルがあるディレクトリに移動します。

    cd terraform-docs-samples/compute/quickstart/create_vm
    

Terraform ファイルを確認する

main.tf ファイルを確認します。このファイルには、作成する Google Cloud リソースが定義されています。

cat main.tf

出力は次のようになります。

resource "google_compute_instance" "default" {
  name         = "my-vm"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "ubuntu-minimal-2210-kinetic-amd64-v20230126"
    }
  }

  network_interface {
    network = "default"
    access_config {}
  }
}

このファイルには、Compute Engine VM インスタンスの Terraform リソースである google_compute_instance リソースが記述されています。google_compute_instance は、次のプロパティを持つように構成されています。

  • namemy-vm に設定されている。
  • machine_typen1-standard-1 に設定されている。
  • zoneus-central1-a に設定されている。
  • boot_disk は、インスタンスのブートディスクを設定します。
  • network_interface は、Google Cloud プロジェクトのデフォルト ネットワークを使用するように設定されています。

Compute Engine VM インスタンスを作成する

  1. Cloud Shell で次のコマンドを実行して、Terraform が使用できることを確認します。

    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
    
  2. 次のコマンドを実行して Terraform を初期化します。このコマンドは、Terraform が構成を適用できるようにワークスペースを準備します。

    terraform init
    

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

    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of hashicorp/google...
    - Installing hashicorp/google v5.35.0...
    - Installed hashicorp/google v5.35.0 (signed by HashiCorp)
    
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run "terraform init" in the future.
    
    Terraform has been successfully initialized!
    
  3. 次のコマンドを実行して、Terraform 構成を検証します。このコマンドは、次のアクションを実行します。

    • main.tf の構文が正しいことを検証する。
    • 作成されるリソースのプレビューを表示する。
    terraform plan
    

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

    Plan: 1 to add, 0 to change, 0 to destroy.
    
    Note: You didn't use the -out option to save this plan, so Terraform can't
    guarantee to take exactly these actions if you run "terraform apply" now.
    
  4. 構成を適用して、main.tf ファイルに記述されているリソースをプロビジョニングします。

    terraform apply
    

    プロンプトが表示されたら、「yes」と入力します。

    Terraform が Google Cloud APIs を呼び出して、main.tf ファイルで定義された VM インスタンスを作成します。

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

    Apply complete! Resources: 1 added, 0 changed, 0 destroyed
    

VM インスタンスに接続する

次のコマンドを実行して、作成した VM インスタンスに接続します。

gcloud compute ssh --zone=us-central1-a my-vm

クリーンアップ

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

Cloud Shell で次のコマンドを実行して、Terraform リソースを削除します。

terraform destroy

プロンプトが表示されたら、「yes」と入力します。

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

Destroy complete! Resources: 1 destroyed.

次のステップ