ข้ามไปยังเนื้อหา

Running Many Units at Once

terragrunt run --all plan กับ terragrunt run --all apply ทำงานกับทุก unit ที่อยู่ใต้ directory tree ปัจจุบันพร้อมกันทีเดียว ตามลำดับ dependency ที่คำนวณจาก dependency block ของแต่ละ unit

Terminal window
terragrunt run --all plan
terragrunt run --all apply

run --all ค้นหาทุก unit ที่อยู่ใต้ working directory ปัจจุบัน สร้าง dependency graph จาก dependency block ของแต่ละตัว (เรื่องที่คุยไปในบทก่อนหน้า) แล้วรันคำสั่งที่ระบุกับแต่ละ unit ตามลำดับที่ถูกต้อง vpc ถูก apply ก่อน ec2 โดยอัตโนมัติ ไม่มีใครต้องจำหรือ hard-code ลำดับนั้นเอง

command surface สำหรับหลาย unit ของ Terragrunt เปลี่ยนไปแล้ว และ blog post กับคำตอบเก่า ๆ จำนวนมากบนอินเทอร์เน็ตก็ยังพูดถึง syntax เก่าอยู่ มีประวัติสองยุคที่ควรรู้ไว้ เพื่อไม่ให้โดนคำแนะนำเก่าหลอก

terragrunt plan-all กับ terragrunt apply-all (รวมถึง destroy-all, output-all, validate-all) ถูก ลบออกไปทั้งหมด จาก Terragrunt เวอร์ชันปัจจุบันแล้ว ไม่มีอยู่อีกต่อไป รันแล้วจะ fail เฉย ๆ อย่าใช้คำสั่งเหล่านี้ และให้ถือว่า tutorial ไหนที่ยังพึ่งคำสั่งพวกนี้อยู่ เขียนมาสำหรับ Terragrunt เวอร์ชันเก่ามาก

Terminal window
# Removed entirely — these commands no longer exist in current Terragrunt
terragrunt plan-all
terragrunt apply-all

terragrunt run-all <command> ยังรันได้อยู่ทุกวันนี้ แต่ deprecated แล้ว กำลังถูกแทนที่ด้วย run --all ที่เป็น syntax ที่คอร์สนี้สอน

Terminal window
# Deprecated — being replaced by run --all; avoid in new configuration
terragrunt run-all plan

บางครั้งคุณไม่อยากรันทุก unit ใน tree อยากรันแค่บางส่วน เช่น เฉพาะที่อยู่ใต้ region เดียว โดยไม่แตะส่วนที่เหลือของ tree เลย flag --filter ทำหน้าที่นี้ โดย match unit ด้วย path glob flag ที่วางไว้ก่อนคำสั่งต้องมี separator -- เพื่อให้ Terragrunt รู้ว่า flag ของตัวเองจบตรงไหน แล้วคำสั่งของแต่ละ unit เริ่มตรงไหน

Terminal window
# Only plan units under ./networking (and anything depending on them)
terragrunt run --all --filter './networking/**' -- plan

สำหรับ CI การรอ confirm ด้วยมือทุก unit ไม่ใช่ทางเลือกที่ทำได้ --non-interactive ข้าม prompt พวกนั้นไปเลย ทำให้ apply รันแบบไม่มีคนคอยกดยืนยันได้

Terminal window
terragrunt run --all --non-interactive -- apply

terraform block ข้างใน terragrunt.hcl ของ unit หนึ่งกำหนด before_hook กับ after_hook ได้ คือคำสั่ง shell อะไรก็ได้ที่ Terragrunt รันก่อนหรือหลัง operation หนึ่งบน unit นั้นเจาะจง

ec2/terragrunt.hcl
terraform {
before_hook "validate_before_plan" {
commands = ["plan"]
execute = ["./scripts/validate-tags.sh"]
}
after_hook "notify_slack" {
commands = ["apply"]
execute = ["./scripts/notify-slack.sh"]
run_on_error = false
}
}

commands list ว่า Terragrunt command ไหนบ้างที่ trigger hook นี้ execute คือคำสั่งจริงที่ Terragrunt รัน (เป็น argument list ไม่ใช่ shell string) และ run_on_error ควบคุมว่า after_hook จะยังรันไหมถ้า operation ที่ผูกอยู่ด้วย fail ระหว่าง run --all apply ที่ทำกับหลาย unit hook ของแต่ละ unit จะทำงานรอบ ๆ apply ของ unit นั้น ๆ เอง Slack notification ข้างบนจะยิงหนึ่งครั้งต่อ unit ที่ apply สำเร็จ ไม่ใช่ยิงครั้งเดียวสำหรับทั้ง run

flowchart LR
  cmd["terragrunt run --all apply"] --> vpc["vpc unit applies first"]
  vpc --> ec2["ec2 unit applies (depends on vpc)"]
  vpc --> rds["rds unit applies (depends on vpc)"]
  vpc -->|after_hook| slack["notify-slack.sh"]
run --all apply กระจายไปทั่ว unit ตามลำดับ dependency พร้อม hook ที่ยิงหลัง apply ของ unit หนึ่ง
คำสั่งที่ใช้งานอยู่ตอนนี้สำหรับรัน plan ข้ามทุก unit ใน directory tree คืออะไร
terragrunt plan-all กับ apply-all เกิดอะไรขึ้น
สถานะของคำสั่ง terragrunt run-all ตอนนี้เป็นยังไง
before_hook หรือ after_hook ใช้ทำอะไร