2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Cloud Deployment Managerを使ってみる

Posted at

AWSでいうところのCloudFormationが、GCPではCloud Deployment Managerというサービスだそうなので、触ってみました。

まずはテンプレートファイル作成です。CloudFormationと同じくYAMLで書けます。GCEのインスタンス1台を立てるテンプレートです。

sample.yaml
resources:
  - name: instance1
    type: compute.v1.instance
    properties:
      zone: asia-northeast1-b
      machineType: zones/asia-northeast1-b/machineTypes/n1-standard-1
      disks:
      - deviceName: instance1
        boot: true
        initializeParams:
          sourceImage: projects/ubuntu-os-cloud/global/images/family/ubuntu-2004-lts
      networkInterfaces:
        - network: projects/xxxxxxxx/global/networks/default # xxxxxxxxのところはプロジェクトIDを入れる

デプロイする前に、プレビューができるようです。 gcloud deployment-manager deployments create コマンドに --preview を付けます。CloudFormationよりも便利な予感。

$ gcloud --project=xxxxxxxx deployment-manager deployments create sample1 --config sample.yaml --preview
...
NAME       TYPE                 STATE       ERRORS  INTENT
instance1  compute.v1.instance  IN_PREVIEW  []      CREATE_OR_ACQUIRE

テンプレートファイルを変更したら、再度プレビューを見るには、 gcloud deployment-manager deployments update コマンドに変わるらしいです。 --preview を付けるのは変わらず。

$ gcloud --project=xxxxxxxx deployment-manager deployments update sample1 --config sample.yaml --preview
NAME       TYPE                 STATE       ERRORS  INTENT
instance1  compute.v1.instance  IN_PREVIEW  []      CREATE_OR_ACQUIRE

なぜ update にしないといけないのか。 --preview を付けるとリソースの作成はされないものの、CloudFormationでいうところのスタックは作られるということなのだろうか。この時点で describe というコマンドで様子を確認できました。

$ gcloud --project=xxxxxxxx deployment-manager deployments describe sample1
---
...
NAME       TYPE                 STATE       INTENT
instance1  compute.v1.instance  IN_PREVIEW  CREATE_OR_ACQUIRE

STATEIN_PREVIEW となっています。

--preview を外すと、実際にリソースが作成されます。この時はテンプレートファイル名を指定しないようです。

$ gcloud --project=xxxxxxxx deployment-manager deployments update sample1
...
NAME       TYPE                 STATE      ERRORS  INTENT
instance1  compute.v1.instance  COMPLETED  []

もう一度 describe

$ gcloud --project=xxxxxxxx deployment-manager deployments describe sample1
---
...
NAME       TYPE                 STATE      INTENT
instance1  compute.v1.instance  COMPLETED

gcloud compute instances list コマンドで、テンプレートファイルに指定した instance1 という名前のGCEインスタンスが生成されているのが確認できます。

次は、削除。

$ gcloud --project=xxxxxxxx deployment-manager deployments delete sample1
The following deployments will be deleted:
- sample1

Do you want to continue (y/N)?  y
...
Delete operation operation-1621866485901-5c31437c3e6ca-c264c3ba-250a94a1 completed successfully.

GCEインスタンスも削除されました。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?