【Google Cloud (GCP)】Compute Engine をCLIで作成

プログラミング

はじめに

この記事では Google Cloud (GCP) の Compute Engine の作成を gcloud CLI を用いておこないます。

Compute Engine の作成は、コンソール画面からGUIで作成することも可能ですが、gcloud CLI を用いることでスクリプトの管理や他の自動化と連携でき便利です。

gcloud CLI の実行は、Cloud Shell で行えますし、またインストールすることでローカル環境でも実行することができます。

gcloud CLI をインストールする  |  Google Cloud CLI のドキュメント

gcloud で Compute Engine を作成する

今回作成する Compute Engine インスタンスの OS は Ubuntu を用います。

まずは使用可能な公開イメージのリストを次のコマンドで確認しましょう。

$ gcloud compute images list | grep ubuntu
## 出力
NAME                                                  PROJECT              FAMILY                             DEPRECATED  STATUS
ubuntu-1804-bionic-arm64-v20221015                    ubuntu-os-cloud      ubuntu-1804-lts-arm64                          READY
ubuntu-1804-bionic-v20221015                          ubuntu-os-cloud      ubuntu-1804-lts                                READY
ubuntu-pro-1604-xenial-v20220810                      ubuntu-os-pro-cloud  ubuntu-pro-1604-lts                            READY
ubuntu-pro-1804-bionic-v20221006                      ubuntu-os-pro-cloud  ubuntu-pro-1804-lts                            READY
ubuntu-pro-2004-focal-v20221014                       ubuntu-os-pro-cloud  ubuntu-pro-2004-lts                            READY
ubuntu-pro-2204-jammy-v20221014                       ubuntu-os-pro-cloud  ubuntu-pro-2204-lts                            READY
ubuntu-pro-fips-1804-bionic-v20221013                 ubuntu-os-pro-cloud  ubuntu-pro-fips-1804-lts                       READY
ubuntu-pro-fips-2004-focal-v20221014                  ubuntu-os-pro-cloud  ubuntu-pro-fips-2004-lts                       READY
ubuntu-2004-focal-arm64-v20221015                     ubuntu-os-cloud      ubuntu-2004-lts-arm64                          READY
ubuntu-2004-focal-v20221015                           ubuntu-os-cloud      ubuntu-2004-lts                                READY
ubuntu-2204-jammy-arm64-v20221014                     ubuntu-os-cloud      ubuntu-2204-lts-arm64                          READY
ubuntu-2204-jammy-v20221014                           ubuntu-os-cloud      ubuntu-2204-lts                                READY
ubuntu-minimal-1804-bionic-arm64-v20221015            ubuntu-os-cloud      ubuntu-minimal-1804-lts-arm64                  READY
ubuntu-minimal-1804-bionic-v20221015                  ubuntu-os-cloud      ubuntu-minimal-1804-lts                        READY
ubuntu-minimal-2004-focal-arm64-v20221014             ubuntu-os-cloud      ubuntu-minimal-2004-lts-arm64                  READY
ubuntu-minimal-2004-focal-v20221014                   ubuntu-os-cloud      ubuntu-minimal-2004-lts                        READY
ubuntu-minimal-2204-jammy-arm64-v20221014             ubuntu-os-cloud      ubuntu-minimal-2204-lts-arm64                  READY
ubuntu-minimal-2204-jammy-v20221014                   ubuntu-os-cloud      ubuntu-minimal-2204-lts                        READY

現時点で最新の ubuntu-2204-lts を使用することにします。

それでは、Compute Engine を作成する shell script を以下に記載します。

#!/bin/bash

VM_NAME='demo-vm'
ZONE='asia-northeast1-a'
MACHINE_TYPE='e2-micro'
IMAGE_PROJECT='ubuntu-os-cloud'
IMAGE_FAMILY='ubuntu-2204-lts'
BOOT_DISK_TYPE='pd-balanced'
BOOT_DISK_SIZE='10GB'


gcloud compute instances create ${VM_NAME} \
    --zone ${ZONE} \
    --machine-type ${MACHINE_TYPE} \
    --image-project ${IMAGE_PROJECT} \
    --image-family ${IMAGE_FAMILY} \
    --boot-disk-type ${BOOT_DISK_TYPE} \
    --boot-disk-size ${BOOT_DISK_SIZE}

上記のスクリプトを実行することで、Compute Engine が作成されます。

次のコマンドで、その確認ができます。

$ gcloud compute instances list
## 出力
NAME     ZONE               MACHINE_TYPE  PREEMPTIBLE  INTERNAL_IP  EXTERNAL_IP    STATUS
demo-vm  asia-northeast1-a  e2-micro                   10.146.0.13  34.84.151.204  RUNNING

ssh を使用して Compute Engine に接続する

Compute Engine の作成が完了したら、そのインスタンスに ssh 接続してみましょう。

gcloud compute ssh コマンドで簡単に ssh 接続することができます。初めて gcloud compute ssh をおこなう場合は、次の出力のように自動でキーペアを生成してくれます。

$ VM_NAME='demo-vm'
$ gcloud compute ssh ${VM_NAME} 
## 出力
WARNING: The private SSH key file for gcloud does not exist.
WARNING: The public SSH key file for gcloud does not exist.
WARNING: You do not have an SSH key for gcloud.
WARNING: SSH keygen will be executed to generate a key.
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /Users/your_username/.ssh/google_compute_engine
Your public key has been saved in /Users/your_username/.ssh/google_compute_engine.pub
The key fingerprint is:
SHA256:/rERfJtI+/l0SernCMvW5pSEMToewQi7XK0OqiLTA4k your_username@mac.local
The key's randomart image is:
+---[RSA 3072]----+
|     .           |
|      o +        |
|     . o + o     |
|    . o ..o +    |
|..   + .S++... . |
|E   . o...o=.o+ .|
| o .   ...=.++...|
|+ +      ..*==.o |
|oo .      +o==+. |
+----[SHA256]-----+
Updating project ssh metadata...⠧Updated 
https://www.googleapis.com/compute/v1/projects/your_project_id
. Updating project ssh metadata...done. Waiting for SSH key to propagate. Warning: Permanently added 'compute.6775122058618849794' (ED25519) to the list of known hosts. Welcome to Ubuntu 22.04.1 LTS (GNU/Linux 5.15.0-1019-gcp x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage ... your_username@demo-vm:~$

このように ssh 接続することができます。キーペアは ~/.ssh に作成されます。


また、VSCode の Remote SSH 等で ssh 接続する場合は、gcloud compute config-ssh コマンドが便利です。

$ gcloud compute config-ssh
## 出力
You should now be able to use ssh/scp with your instances.
For example, try running:

  $ ssh demo-vm.asia-northeast1-a.your_project_id

上記のように gcloud compute config-ssh コマンドを実行すると、~/.ssh/config に作成した Compute Engine インスタンスの情報が記載されます。

...
# Google Compute Engine Section
#
# The following has been auto-generated by "gcloud compute config-ssh"
# to make accessing your Google Compute Engine virtual machines easier.
#
# To remove this blob, run:
#
#   gcloud compute config-ssh --remove
#
# You can also manually remove this blob by deleting everything from
# here until the comment that contains the string "End of Google Compute
# Engine Section".
#
# You should not hand-edit this section, unless you are deleting it.
#
Host demo-vm.asia-northeast1-a.your_project_id
    HostName 34.84.151.204
    IdentityFile /Users/your_username/.ssh/google_compute_engine
    UserKnownHostsFile=/Users/your_username/.ssh/google_compute_known_hosts
    HostKeyAlias=compute.6775122058618849794
    IdentitiesOnly=yes
    CheckHostIP=no

# End of Google Compute Engine Section
...

これで VSCode の Remote SSH でインスタンスに接続ができます。

Compute Engine を削除する

Compute Engine の削除は以下のコマンドでおこなえます。

$ gcloud compute instances delete ${VM_NAME}


Compute Engine のその他の記事はこちら ▼

タイトルとURLをコピーしました