ใช้ Pulumi บน Google Cloud ด้วย YAML

1. ภาพรวม

แล็บนี้จะสอนวิธีใช้ Pulumi ซึ่งเป็นเครื่องมือ Infrastructure as Code เพื่อจัดสรรและจัดการทรัพยากร Google Cloud

สิ่งที่คุณจะได้เรียนรู้

ในแล็บนี้ คุณจะได้เรียนรู้วิธีทำสิ่งต่อไปนี้

  • ติดตั้งและกำหนดค่า Pulumi
  • เขียนโปรแกรม YAML เพื่อจำลองโครงสร้างพื้นฐานใน Google Cloud
  • จัดสรรและจัดการทรัพยากรระบบคลาวด์โดยใช้ Pulumi
  • ใช้ pulumi convert เพื่อแปลงโปรแกรม YAML เป็นโปรแกรม Python

2. การตั้งค่าและข้อกำหนด

การตั้งค่าสภาพแวดล้อมแบบเรียนรู้ด้วยตนเอง

  1. ลงชื่อเข้าใช้ Google Cloud Console แล้วสร้างโปรเจ็กต์ใหม่หรือใช้โปรเจ็กต์ที่มีอยู่ซ้ำ หากยังไม่มีบัญชี Gmail หรือ Google Workspace คุณต้องสร้างบัญชี

b35bf95b8bf3d5d8.png

a99b7ace416376c4.png

bd84a6d3004737c5.png

  • ชื่อโปรเจ็กต์คือชื่อที่แสดงสำหรับผู้เข้าร่วมโปรเจ็กต์นี้ ซึ่งเป็นสตริงอักขระที่ Google APIs ไม่ได้ใช้ โดยคุณจะอัปเดตได้ทุกเมื่อ
  • รหัสโปรเจ็กต์จะไม่ซ้ำกันในโปรเจ็กต์ Google Cloud ทั้งหมดและเปลี่ยนแปลงไม่ได้ (เปลี่ยนไม่ได้หลังจากตั้งค่าแล้ว) Cloud Console จะสร้างสตริงที่ไม่ซ้ำกันโดยอัตโนมัติ ซึ่งโดยปกติแล้วคุณไม่จำเป็นต้องสนใจว่าสตริงนั้นคืออะไร ใน Codelab ส่วนใหญ่ คุณจะต้องอ้างอิงรหัสโปรเจ็กต์ (โดยปกติจะระบุเป็น PROJECT_ID) หากไม่ชอบรหัสที่สร้างขึ้น คุณก็สร้างรหัสแบบสุ่มอีกรหัสหนึ่งได้ หรือคุณจะลองใช้ชื่อของคุณเองเพื่อดูว่าพร้อมใช้งานหรือไม่ก็ได้ คุณจะเปลี่ยนแปลงรหัสนี้หลังจากขั้นตอนนี้ไม่ได้ และรหัสจะยังคงอยู่ตลอดระยะเวลาของโปรเจ็กต์
  • โปรดทราบว่ายังมีค่าที่ 3 ซึ่งคือหมายเลขโปรเจ็กต์ที่ API บางตัวใช้ ดูข้อมูลเพิ่มเติมเกี่ยวกับค่าทั้ง 3 นี้ได้ในเอกสารประกอบ
  1. จากนั้นคุณจะต้องเปิดใช้การเรียกเก็บเงินใน Cloud Console เพื่อใช้ทรัพยากร/API ของ Cloud การทำตาม Codelab นี้ไม่ควรมีค่าใช้จ่ายมากนัก หรืออาจไม่มีเลย หากต้องการปิดทรัพยากรเพื่อไม่ให้มีการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่สร้างขึ้นหรือลบทั้งโปรเจ็กต์ได้ ผู้ใช้ Google Cloud รายใหม่มีสิทธิ์เข้าร่วมโปรแกรมช่วงทดลองใช้ฟรีมูลค่า$300 USD

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

เรียกใช้คำสั่งต่อไปนี้เพื่อตั้งค่ารหัสโปรเจ็กต์และให้สิทธิ์เข้าถึง คุณต้องทำตามวิธีการที่คำสั่งระบุ

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

สร้างที่เก็บข้อมูล GCS ใน Cloud Shell และใช้เป็นแบ็กเอนด์

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. Bucket
  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 ที่มีรหัสโปรเจ็กต์เป็นค่า

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

เลือกใช่ แล้วระบบจะจัดสรรทรัพยากร เอาต์พุตควรมีลักษณะดังนี้

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

ลองติดตั้งใช้งานสแต็กอีกครั้งและเลือกใช่

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. ยินดีด้วย

ยินดีด้วย คุณทำแล็บเสร็จแล้ว