---
title: "３つの方法でGoogle Compute Engineでインスタンスを作成する | grasys blog"
url: "https://blog.grasys.io/post/kyouhei/createinstances/"
description: "grasys 加藤です。 入社するまでGoogle Cloud (GCP) Platformに触ったこともなく、インフラエンジニアでもなかったのですが、入ってからは息をするように毎日触っています。 今回は最初の一歩、Google Compute Engineでインスタンスを作成する３つのパターンをご紹介します。 -…"
---

# ３つの方法でGoogle Compute Engineでインスタンスを作成する

-   ![](/_astro/noicon.CTHOhNiB_1HMs6A.webp)[kyouhei](/authors/kyouhei/)
-   公開日：2016年9月15日
-   カテゴリー：[Tech](/categories/tech/)
-   タグ：[#Compute Engine](/tags/compute-engine/)[#Google Cloud](/tags/google-cloud/)[#Terraform](/tags/terraform/)

![３つの方法でGoogle Compute Engineでインスタンスを作成する](/_astro/og-image.DDSiu2Ny_XHpDd.webp)

grasys 加藤です。

入社するまでGoogle Cloud (GCP) Platformに触ったこともなく、インフラエンジニアでもなかったのですが、入ってからは息をするように毎日触っています。

今回は最初の一歩、Google Compute Engineでインスタンスを作成する３つのパターンをご紹介します。

-   Google Cloud (GCP) Console
-   gcloudコマンド
-   Terraform

* * *

## 1.Google Cloud (GCP) Consule

Webの画面からGUI操作でインスタンスを作成することができます。

恐らく一番わかりやすい方法です。[Quick Start](https://cloud.google.com/compute/docs/quickstart-linux)でも紹介されています。

1.メニューから **Compute Engine** 画面に移動します。

![Google CloudコンソールのメニューからCompute Engineを選択する画面](/_astro/01-1_1-ffc80169.BqTmzEOD_LibPh.webp)

2.**インスタンスを作成** を選択します。

![Compute Engineのインスタンス一覧でインスタンスの作成を選択する画面](/_astro/02-1_2-2add5ef4.B73_aN0r_Z2fSG9c.webp)

3.必要な項目を入力して、 **作成** でインスタンスを作り始めます！

![Compute Engineの新規インスタンスに必要な項目を入力する画面](/_astro/03-1_3-0683125d.BZOltTx9_ZBYMId.webp)

* * *

インスタンス起動は他社クラウドと比較するとかなり高速です。

ただしGoogle Cloud (GCP) Consoleは若干のタイムラグがあるのであまり速さに気が付かないかも・・・ 他の２つの方法だと実感できるかと思います。

## 2.gcloudコマンド

GUIは入力コストが高く、同一構成作ろうとして選択ミスもします。やはりコマンド入力が間違いありません。Google Cloud (GCP) Platform は **gcloudコマンド** が用意されています。

さきほどのGUIと同じ構成の場合は次のコマンドになります。

Plain textcontent\_copy

```
gcloud compute instances create instance-1 \
    --boot-disk-size 10GB \
    --boot-disk-type pd-standard \
    --image-project debian-cloud \
    --machine-type n1-standard-1 \
    --scopes cloud-platform \
    --zone us-east1-c
```

む、 `--image debian-8-jessie-v20160803` 指定だと見つからないとエラーがでます。(2016/9/15)

image-project/familyが最近指定できるようになったからかな？

運用ではimageは自分で作ったものを指定することが多いですね。

Plain textcontent\_copy

```
$ time gcloud compute instances create instance-1 \
>    --boot-disk-size 10GB \
>    --boot-disk-type pd-standard \
>    --image-project debian-cloud \
>    --machine-type n1-standard-1 \
>    --scopes cloud-platform \
>    --zone us-east1-c
WARNING: You have selected a disk size of under [200GB]. This may result in poor I/O performance. For more information, see: https://developers.google.com/compute/docs/disks#pdperformance.
Created [https://www.googleapis.com/compute/v1/projects/your-project/zones/us-east1-c/instances/instance-1].
NAME        ZONE        MACHINE_TYPE   PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP     STATUS
instance-1  us-east1-c  n1-standard-1               10.240.0.15  104.196.172.61  RUNNING

real    0m28.828s
user    0m0.429s
sys     0m0.086s
```

実際にコマンドを発行してみると起動にかかるのは30秒弱。

この後すぐにSSHして使いはじめることができます。速い！

* * *

これでgcloudシェルでwrapしてループさせれば100台でも作れます？

時間かかりすぎで待っていられません。あくまで1台パッと立てる時専用です。

そこで便利な [Terraform](https://www.terraform.io/) です。

## 3.Terraform

プロビジョニングツールとしてHashiCorpがリリースしている **Terraform** を使用します。 [インストール](https://www.terraform.io/intro/getting-started/install.html)して、[ドキュメント](https://www.terraform.io/docs/providers/google/index.html)を参考にJSON形式のconfigを作成します。

**`main.tf`**

Plain textcontent\_copy

```
provider "google" {
  project      = "your-project"
  region       = "us-east1"
}

resource "google_compute_instance" "default" {
  name         = "${format("instance-%01d", count.index+1)}"
  count        = "1"
  machine_type = "n1-standard-1"
  zone         = "us-east1-c"

  disk {
    image = "debian-8-jessie-v20160803"
    size  = "10"
  }

  network_interface {
    network = "default"
    access_config {
      // Ephemeral IP
    }
  }

  service_account {
    scopes = [ "cloud-platform" ]
  }
}
```

この設定ファイルがあるディレクトリでコマンドを実行するとインスタンスを作成してくれます。

Plain textcontent\_copy

```
$ time terraform apply
provider.google.credentials
  Enter a value:

google_compute_instance.default: Creating...
  can_ip_forward:                                      "" => "0"
  disk.#:                                              "" => "1"
  disk.0.auto_delete:                                  "" => "1"
  disk.0.image:                                        "" => "debian-8-jessie-v20160803"
  disk.0.size:                                         "" => "10"
  machine_type:                                        "" => "n1-standard-1"
  metadata_fingerprint:                                "" => "<computed>"
  name:                                                "" => "instance-1"
  network_interface.#:                                 "" => "1"
  network_interface.0.access_config.#:                 "" => "1"
  network_interface.0.access_config.0.assigned_nat_ip: "" => "<computed>"
  network_interface.0.address:                         "" => "<computed>"
  network_interface.0.name:                            "" => "<computed>"
  network_interface.0.network:                         "" => "default"
  self_link:                                           "" => "<computed>"
  service_account.#:                                   "" => "1"
  service_account.0.email:                             "" => "<computed>"
  service_account.0.scopes.#:                          "" => "1"
  service_account.0.scopes.1733087937:                 "" => "https://www.googleapis.com/auth/cloud-platform"
  tags_fingerprint:                                    "" => "<computed>"
  zone:                                                "" => "us-east1-c"
google_compute_instance.default: Creation complete

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

The state of your infrastructure has been saved to the path
below. This state is required to modify and destroy your
infrastructure, so keep it safe. To inspect the complete state
use the `terraform show` command.

State path: terraform.tfstate

real    0m25.430s
user    0m0.129s
sys     0m0.050s
```

ここで複数台を起動したい時には `count = "1"` を書き換えます。試しに `count = "100"` としてみましょう。もちろん100台起動する設定です。

実際に起動させるとちょっと課金が気になるのでテストコマンドで確認します。

Plain textcontent\_copy

```
$ terraform plan
~99台分のログ~
Plan: 99 to add, 0 to change, 0 to destroy.
```

簡単にたくさんのインスタンスを立てられそうですね。

Google Compute Engineは分単位の課金(10分から)なのですぐに止めれば100台でも17台✕1時間の課金で済みますｗ

### お掃除

1.  削除アイコンを押す。
2.  `gcloud compute instances delete instance1 --zone=us-east1-c` を実行する。
3.  `terraform destory` を実行して `yes` とタイプする。

* * *

この他にもPythonやRESTからAPIを呼んでインスタンスを作成することも可能ですし、terraform以外にもGoogle Cloud (GCP) Platformに対応しているプロビジョニングツールはありますが機会があれば。

## この記事を書いた人

[![](/_astro/noicon.CTHOhNiB_Z24aAgM.webp)](/authors/kyouhei/)

### [kyouhei](/authors/kyouhei/)

kyouheiのプロフィールと執筆記事をご覧いただけます。

[プロフィールと記事一覧](/authors/kyouhei/)