Running Many Units at Once
ไอเดียในหนึ่งประโยค
หัวข้อที่มีชื่อว่า “ไอเดียในหนึ่งประโยค”terragrunt run --all plan กับ terragrunt run --all apply ทำงานกับทุก unit ที่อยู่ใต้ directory tree ปัจจุบันพร้อมกันทีเดียว ตามลำดับ dependency ที่คำนวณจาก dependency block ของแต่ละ unit
run —all คือคำสั่งที่ใช้งานอยู่ตอนนี้
หัวข้อที่มีชื่อว่า “run —all คือคำสั่งที่ใช้งานอยู่ตอนนี้”terragrunt run --all planterragrunt run --all applyrun --all ค้นหาทุก unit ที่อยู่ใต้ working directory ปัจจุบัน สร้าง dependency graph จาก dependency block ของแต่ละตัว (เรื่องที่คุยไปในบทก่อนหน้า) แล้วรันคำสั่งที่ระบุกับแต่ละ unit ตามลำดับที่ถูกต้อง vpc ถูก apply ก่อน ec2 โดยอัตโนมัติ ไม่มีใครต้องจำหรือ hard-code ลำดับนั้นเอง
เรื่อง syntax เก่าที่ยังเจอได้ในบทความเก่า ๆ
หัวข้อที่มีชื่อว่า “เรื่อง syntax เก่าที่ยังเจอได้ในบทความเก่า ๆ”command surface สำหรับหลาย unit ของ Terragrunt เปลี่ยนไปแล้ว และ blog post กับคำตอบเก่า ๆ จำนวนมากบนอินเทอร์เน็ตก็ยังพูดถึง syntax เก่าอยู่ มีประวัติสองยุคที่ควรรู้ไว้ เพื่อไม่ให้โดนคำแนะนำเก่าหลอก
terragrunt plan-all กับ terragrunt apply-all (รวมถึง destroy-all, output-all, validate-all) ถูก ลบออกไปทั้งหมด จาก Terragrunt เวอร์ชันปัจจุบันแล้ว ไม่มีอยู่อีกต่อไป รันแล้วจะ fail เฉย ๆ อย่าใช้คำสั่งเหล่านี้ และให้ถือว่า tutorial ไหนที่ยังพึ่งคำสั่งพวกนี้อยู่ เขียนมาสำหรับ Terragrunt เวอร์ชันเก่ามาก
# Removed entirely — these commands no longer exist in current Terragruntterragrunt plan-allterragrunt apply-allterragrunt run-all <command> ยังรันได้อยู่ทุกวันนี้ แต่ deprecated แล้ว กำลังถูกแทนที่ด้วย run --all ที่เป็น syntax ที่คอร์สนี้สอน
# Deprecated — being replaced by run --all; avoid in new configurationterragrunt run-all planscope run —all ให้แคบลง และรันแบบ non-interactive
หัวข้อที่มีชื่อว่า “scope run —all ให้แคบลง และรันแบบ non-interactive”บางครั้งคุณไม่อยากรันทุก unit ใน tree อยากรันแค่บางส่วน เช่น เฉพาะที่อยู่ใต้ region เดียว โดยไม่แตะส่วนที่เหลือของ tree เลย flag --filter ทำหน้าที่นี้ โดย match unit ด้วย path glob flag ที่วางไว้ก่อนคำสั่งต้องมี separator -- เพื่อให้ Terragrunt รู้ว่า flag ของตัวเองจบตรงไหน แล้วคำสั่งของแต่ละ unit เริ่มตรงไหน
# Only plan units under ./networking (and anything depending on them)terragrunt run --all --filter './networking/**' -- planสำหรับ CI การรอ confirm ด้วยมือทุก unit ไม่ใช่ทางเลือกที่ทำได้ --non-interactive ข้าม prompt พวกนั้นไปเลย ทำให้ apply รันแบบไม่มีคนคอยกดยืนยันได้
terragrunt run --all --non-interactive -- applybefore_hook กับ after_hook
หัวข้อที่มีชื่อว่า “before_hook กับ after_hook”terraform block ข้างใน terragrunt.hcl ของ unit หนึ่งกำหนด before_hook กับ after_hook ได้ คือคำสั่ง shell อะไรก็ได้ที่ Terragrunt รันก่อนหรือหลัง operation หนึ่งบน unit นั้นเจาะจง
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"]