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

1. ภาพรวม

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

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

ในห้องทดลองนี้ คุณจะได้ศึกษาวิธีทำสิ่งต่อไปนี้

  • ติดตั้งและกำหนดค่า Pulumi
  • เขียนโปรแกรม YAML เพื่อสร้างโมเดลโครงสร้างพื้นฐานบน Google Cloud
  • จัดสรรและจัดการทรัพยากรระบบคลาวด์โดยใช้ Pulumi
  • ใช้ pulumi conversion เพื่อแปลงโปรแกรม 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 ของระบบคลาวด์ การใช้งาน Codelab นี้น่าจะไม่มีค่าใช้จ่ายใดๆ หากมี หากต้องการปิดทรัพยากรเพื่อไม่ให้มีการเรียกเก็บเงินนอกเหนือจากบทแนะนำนี้ คุณสามารถลบทรัพยากรที่คุณสร้างหรือลบทั้งโปรเจ็กต์ได้ ผู้ใช้ใหม่ของ Google Cloud จะมีสิทธิ์เข้าร่วมโปรแกรมทดลองใช้ฟรี$300 USD

3. การตั้งค่าโครงสร้างพื้นฐาน

ติดตั้งและกำหนดค่า Pulumi

เรียกใช้คำสั่งต่อไปนี้เพื่อติดตั้ง Pulumi ใน Cloud Shell

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

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

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. ออบเจ็กต์ข้อความที่มีสตริง "สวัสดีทุกคน"
  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 ให้ชี้ไปที่โปรแกรม Pulumi. โปรดทราบว่ารันไทม์และรายการหลักมีการเปลี่ยนแปลง

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

ยินดีด้วย คุณใช้งานห้องทดลองเสร็จแล้ว