透過 YAML 在 Google Cloud 中使用 Pulumi

1. 總覽

在本實驗室中,您將瞭解如何使用 Pulumi 這項基礎架構即程式碼工具,佈建及管理 Google Cloud 資源。

學習目標

本實驗室中,您將瞭解如何執行下列工作:

  • 安裝及設定 Pulumi
  • 編寫 YAML 程式,在 Google Cloud 建立基礎架構模型
  • 使用 Pulumi 佈建及管理 Cloud 資源
  • 使用 pulumi convert 將 YAML 程式轉換為 Python 程式

2. 設定和需求

自修實驗室環境設定

  1. 登入 Google Cloud 控制台,建立新專案或重複使用現有專案。如果您沒有 Gmail 或 Google Workspace 帳戶,請建立帳戶。

b35bf95b8bf3d5d8.pnga99b7ace416376c4.pngbd84a6d3004737c5.png

  • 「專案名稱」是這項專案參與者的顯示名稱。這是一串 Google API 無法識別的字元。您隨時可以更新。
  • 專案 ID 在所有 Google Cloud 專案中都是不重複的,且設定後即無法變更。Cloud 控制台會自動產生一組不重複的字串,通常您不需要特別留意。在大多數程式碼實驗室中,您需要參照專案 ID (通常會標示為 PROJECT_ID)。如果您不喜歡系統產生的 ID,可以再產生一組隨機 ID。或者,您也可以嘗試使用自己的名稱,看看是否可用。這個步驟完成後即無法變更,且在專案期間會保持不變。
  • 此外,還有第三個值,也就是部分 API 會使用的專案編號。如要進一步瞭解這三個值,請參閱說明文件。
  1. 接著,您必須在 Cloud 控制台中啟用計費功能,才能使用 Cloud 資源/API。執行這個程式碼實驗室不會產生太多費用,甚至可能完全免費。如要關閉資源,避免在本教學課程結束後產生費用,您可以刪除建立的資源,或刪除整個專案。Google Cloud 新使用者可參加價值 $300 美元的免費試用計畫。

3. 基礎架構設定

安裝及設定 Pulumi

在 Cloud Shell 執行下列指令,安裝 Pulumi:

curl -fsSL https://get.pulumi.com | sh

將 Pulumi 新增至路徑,並查看 Pulumi 的說明訊息

export PATH=${PATH}:~/.pulumi/bin
# view the help message to verify pulumi runs
pulumi -h

執行下列指令,設定專案 ID 並授權存取。您必須按照指令提供的指示操作

export PROJECT_ID=$(gcloud config get-value project)
gcloud auth application-default login

在 Cloud Shell 建立 GCS bucket,並做為後端使用

gsutil mb gs://pulumi-${PROJECT_ID}
pulumi login gs://pulumi-${PROJECT_ID}

建立新專案

在 Cloud Shell 中建立專案根目錄

mkdir pulumi-lab && cd pulumi-lab

定義專案檔案 (Pulumi 的進入點)

cat <<EOT > Pulumi.yaml
name: pulumi-lab
description: Try Pulumi

runtime: yaml
main: yaml-repo/
EOT

定義 YAML 資源

建立目錄,以 YAML 格式保存雲端資源定義

mkdir yaml-repo

使用下列資源定義建立檔案 yaml-repo/Pulumi.yaml

  1. 值區
  2. IAM 繫結
  3. 包含「Hello World!」字串的文字物件
  4. 部分輸出內容
resources:
  # Create a GCP resource (Storage Bucket)
  my-bucket:
    type: gcp:storage:Bucket
    properties:
      location: US
      website:
        mainPageSuffix: index.html
      uniformBucketLevelAccess: true

  my-bucket-binding:
    type: gcp:storage:BucketIAMBinding
    properties:
      bucket: ${my-bucket.name}
      role: "roles/storage.objectViewer"
      members: ["allUsers"]

  index-object:
    type: gcp:storage:BucketObject
    properties:
      bucket: ${my-bucket}
      source:
        fn::stringAsset: Hello World!

outputs:
  bucketName: ${my-bucket.url}

部署資源

初始化及設定堆疊

export PULUMI_CONFIG_PASSPHRASE=pulumi-lab
pulumi stack init dev
pulumi config set gcp:project $PROJECT_ID

查看堆疊設定,您應該會看到 gcp:project 鍵,其值為專案 ID

pulumi config

此時,目錄結構應如下所示

├── Pulumi.dev.yaml
├── Pulumi.yaml
└── yaml-repo
    └── Pulumi.yaml

部署堆疊

pulumi up

這項指令會評估程式,並決定要更新哪些資源。首先,系統會顯示預覽畫面,概述執行指令後會進行的變更

輸出內容

Previewing update (dev):
Downloading plugin gcp v6.44.0: 45.69 MiB / 45.69 MiB [=============] 100.00% 1s
     Type                             Name               Plan
 +   pulumi:pulumi:Stack              pulumi-lab-dev     create
 +   ├─ gcp:storage:Bucket            my-bucket          create
 +   ├─ gcp:storage:BucketObject      index-object       create
 +   └─ gcp:storage:BucketIAMBinding  my-bucket-binding  create


Outputs:
    bucketName: output<string>

Resources:
    + 4 to create

Do you want to perform this update?  [Use arrows to move, type to filter]
  yes
> no
  details

選取「yes」,系統就會佈建資源。輸出內容應如下所示

Do you want to perform this update? yes
Updating (dev):
     Type                             Name               Status
 +   pulumi:pulumi:Stack              pulumi-lab-dev     created (3s)
 +   ├─ gcp:storage:Bucket            my-bucket          created (1s)
 +   ├─ gcp:storage:BucketObject      index-object       created (0.78s)
 +   └─ gcp:storage:BucketIAMBinding  my-bucket-binding  created (5s)


Outputs:
    bucketName: "gs://my-bucket-874aa08"

Resources:
    + 4 created

Duration: 11s

執行下列指令,會輸出

pulumi stack output

執行下列指令,確認變更

gsutil ls $(pulumi stack output bucketName)

輸出內容應如下所示:

(輸出)

gs://my-bucket-11a9046/index-object-77a5d80

4. 將 YAML 轉換為 Python

現在將上述範例轉換為 Pulumi Python 程式

pulumi convert --language python --out ./py-repo

檢查在 py-repo 中生成的程式碼

cat py-repo/__main__.py

(輸出)

import pulumi
import pulumi_gcp as gcp

my_bucket = gcp.storage.Bucket("my-bucket",
    location="US",
    website=gcp.storage.BucketWebsiteArgs(
        main_page_suffix="index.html",
    ),
    uniform_bucket_level_access=True)
my_bucket_binding = gcp.storage.BucketIAMBinding("my-bucket-binding",
    bucket=my_bucket.name,
    role="roles/storage.objectViewer",
    members=["allUsers"])
index_object = gcp.storage.BucketObject("index-object",
    bucket=my_bucket.id,
    source=pulumi.StringAsset("Hello World!"))
pulumi.export("bucketName", my_bucket.url)
.......

啟用 Python 虛擬環境

source py-repo/bin/activate

更新 Pulumi.yaml 專案檔案,指向 Python 程式。請注意,執行階段和主要進入點已變更

cat <<EOT > Pulumi.yaml
name: pulumi-lab
description: Try Pulumi

runtime: python
main: py-repo/
EOT

請嘗試重新部署堆疊,並選取「yes」

pulumi up

不應有任何變更,輸出內容大致如下:

(輸出)

Previewing update (dev):
     Type                 Name            Plan
     pulumi:pulumi:Stack  pulumi-lab-dev


Resources:
    4 unchanged

Do you want to perform this update? yes
Updating (dev):
     Type                 Name            Status
     pulumi:pulumi:Stack  pulumi-lab-dev


Outputs:
    bucketName: "gs://my-bucket-c2b49ad"

Resources:
    4 unchanged

Duration: 6s

5. 刪除資源

刪除建立的資源

pulumi destroy

確認訊息如下所示

Previewing update (dev):
     Type                 Name            Plan
     pulumi:pulumi:Stack  pulumi-lab-dev


Resources:
    4 unchanged

Do you want to perform this update?  [Use arrows to move, type to filter]
  yes
> no
  details


Do you want to perform this destroy? yes
Destroying (dev):
     Type                             Name               Status
 -   pulumi:pulumi:Stack              pulumi-lab-dev     deleted
 -   ├─ gcp:storage:BucketIAMBinding  my-bucket-binding  deleted (5s)
 -   ├─ gcp:storage:BucketObject      index-object       deleted (1s)
 -   └─ gcp:storage:Bucket            my-bucket          deleted (0.73s)


Outputs:
  - bucketName: "gs://my-bucket-874aa08"

Resources:
    - 4 deleted

Duration: 10s

6. 恭喜!

恭喜,您已完成實驗室!