ลองใช้งาน CloudFormation Guard กับ AWS Guard Rules Registry เพื่อเพิ่มความปลอดภัยให้ Infrastructure as Code

ลองใช้งาน CloudFormation Guard กับ AWS Guard Rules Registry เพื่อเพิ่มความปลอดภัยให้ Infrastructure as Code

CloudFormation Guard เป็นเครื่องมือจาก AWS สำหรับตรวจสอบความถูกต้องและความปลอดภัยของ CloudFormation Template ด้วยกฎที่กำหนดเองหรือจาก AWS Guard Rules Registry
Clock Icon2025.05.20

จากในบทความของผมก่อนหน้านี้ ผมได้พูดเกี่ยวกับ CloudFormation Guard ไป

https://dev.classmethod.jp/articles/session-governance-and-security-with-infrastructure-as-code-aws-summit-th/

ในวันนี้เราจะมาลองใช้งานจริงๆว่ามันทำงานยังไงกันครับ

1. CloudFormation Guard คืออะไร

CloudFormation Guard (cfn-guard) เป็นเครื่องมือโอเพ่นซอร์สจาก AWS ที่ช่วยให้ผู้ใช้สามารถกำหนดนโยบาย (Policy-as-Code) เพื่อควบคุมและตรวจสอบ CloudFormation Template ได้อย่างมีประสิทธิภาพ โดยสามารถเขียนกฎ (Rules) เพื่อระบุว่า Resource ต่าง ๆ ควรหรือไม่ควรมีค่าบางอย่าง เช่น ต้องเข้ารหัสข้อมูล, ใช้ instance type ที่กำหนด หรือไม่เปิด public access เป็นต้น

จุดเด่นของ cfn-guard คือความสามารถในการทำงานแบบ static analysis ซึ่งช่วยให้ตรวจสอบ template ได้ตั้งแต่ก่อน deploy ทำให้ลดความผิดพลาดและเพิ่มความปลอดภัยให้กับโครงสร้างพื้นฐาน

2. วิธีใช้งาน

สำหรับเนื้อหาฉบับเต็มสามารถเข้าไปอ่านได้ที่
👉 https://github.com/aws-cloudformation/cloudformation-guard

แต่การใช้งาน CloudFormation Guard มีขั้นตอนหลัก ๆ ดังนี้:

  1. ติดตั้ง cfn-guard CLI:
cargo install cfn-guard
  1. เขียนไฟล์กฎ (เช่น rules.guard) เพื่อกำหนดเงื่อนไขที่ต้องการตรวจสอบ

3.ใช้คำสั่ง cfn-guard validate เพื่อตรวจสอบ template:

cfn-guard validate --data template.yaml --rules rules.guard

ผลลัพธ์จะแสดงว่า template ผ่านหรือไม่ผ่านกฎที่กำหนด

2.1 วิธีการติดตั้ง

วิธีการติดตั้งจะแตกต่างกันไปตามระบบปฏิบัติการ (OS) ที่ใช้งาน เช่น macOS, Windows หรือ Linux
โปรดตรวจสอบวิธีติดตั้งที่เหมาะสมจากลิงก์บทความด้านบน

https://github.com/aws-cloudformation/cloudformation-guard?tab=readme-ov-file#installation

เมื่อติดตั้งเสร็จแล้ว ให้ลองรันคำสั่งเพื่อตรวจสอบว่าใช้งานได้หรือไม่:

$ cfn-guard help
cfn-guard 3.1.2

Guard is a general-purpose tool that provides a simple declarative syntax to define
policy-as-code as rules to validate against any structured hierarchical data (like JSON/YAML).
Rules are composed of clauses expressed using Conjunctive Normal Form
(fancy way of saying it is a logical AND of OR clauses). Guard has deep
integration with CloudFormation templates for evaluation but is a general tool
that equally works for any JSON- and YAML- data.

cfn-guard จะมีคำสั่งหลัก ๆ อยู่ 2 คำสั่ง ได้แก่:

  • cfn-guard validate

    ใช้สำหรับตรวจสอบว่า CloudFormation Template ของคุณเป็นไปตามกฎ (rules) ที่กำหนดไว้หรือไม่

  • cfn-guard test

    ใช้สำหรับทดสอบกฎที่เขียนขึ้น ว่าทำงานถูกต้องหรือไม่ โดยสามารถเขียน test case สำหรับ rules ได้

ตัวอย่างการใช้งานเบื้องต้นจะอยู่ในหัวข้อถัดไป

3. ดาวน์โหลดไฟล์กฎ

หลักการทำงานคือ เราจะต้องเอาไฟล์ CloudFormation Template (.yaml) เช็คกับกฎที่เราสร้างขึ้น (.guard)
ถ้าผิดพลาดจะขึ้นว่า Fail ถ้าผ่านก็ไม่มีอะไรเกิดขึ้นครับ

โดยอ่านมาถึงตรงนี้ หลายคนอาจจะสงสัยว่าไฟล์กฎจะไปหาจากไหน

เราสามารถสร้างขึ้นมาเอง หรือไปโหลดจากที่ๆเขาเตรียมกฎมาแล้วได้ครับ
โดยที่ AWS เผยแผ่เป็น OpenSource สามารถดูได้ที่ aws-guard-rules-registry ด้านล่างนี้

https://github.com/aws-cloudformation/aws-guard-rules-registry

โดยในนี้มันจะมีไฟล์กฎจะมีแบบเป็น

  • Managed Rule Sets
  • กฎตาม Service

Managed Rule Sets มีอะไรบ้างสามารถดูได้จากลิ้งค์ด้านล่างนี้

https://github.com/aws-cloudformation/aws-guard-rules-registry?tab=readme-ov-file#managed-rule-sets

ถ้าอยากดาวน์โหลดไฟล์ทั้งหมดในรอบเดียวโหลดได้ที่ Release

https://github.com/aws-cloudformation/aws-guard-rules-registry/releases

ส่วนกฎตาม Service คือ เมื่อเราเข้าไปที่ดูที่ rules/aws เราจะเห็นว่ามีโฟล์เดอร์ถูกแยกตาม Service อยู่

Screenshot 2025-05-20 144720

แล้วเมื่อเราเข้าไปดู เราจะเห็นว่ามีไฟล์กฎแยกตามการตั้งค่าต่างๆไว้อยู่

Screenshot 2025-05-20 144945

4. ลองใช้งานจริง

คราวนี้ผมจะมาลองใช้งานจริงกัน

โดยผมมีไฟล์ CloudFormation Template อยู่อันนึงครับ
เราจะมาเช็คกับ security_group_ingress_all_protocols_rule.guard(ไฟล์จาก กฎตาม Service) ซึ่งเป็นไฟล์ที่เราจะมาเช็คว่า Security Group มีการเปิด inbound Rule port เป็น all หรือเปล่า ถ้ามี ให้ขึ้นว่าครับ
จะเป็นยังไงไปลองกันครับ

อันนี้รายละเอียดไฟล์ของผม

Screenshot 2025-05-20 151759

เราใช้ Command ต่อไปนี้ในการตรวจสอบ

cfn-guard validate --data Step1_template.yaml --rules security_group_ingress_all_protocols_rule.guard

จะเห็นว่าไม่มีอะไรขึ้นมา หมายความว่ามันผ่านครับ ในไฟล์ Step1_template.yaml ไม่มีการเปิด inbound Rule port เป็น all

Monosnap Windows PowerShell 2025-05-20 15.21.57

คราวนี้ผมจะลองแก้เนื้อหาใน template ให้มันผิดกฎที่เราสร้างขึ้น

Monosnap ● Step1_template.yaml - Visual Studio Cod

แล้วลองรันอีกรอบ
จะเห็นว่าขึ้น FAIL แล้ว

Monosnap Windows PowerShell 2025-05-20 15.37.20

เท่านี้เราก็น่าจะเห็นได้แล้วว่า CloudFormation Guard มีประโยชน์อย่างไร

5. สรุป

CloudFormation Guard เป็นเครื่องมือที่ช่วยให้การพัฒนา Infrastructure as Code มีความปลอดภัยและเป็นระบบมากยิ่งขึ้น โดยการใช้กฎในการควบคุมการตั้งค่าของ resource ต่าง ๆ ร่วมกับ AWS Guard Rules Registry ทำให้สามารถเข้าถึงกฎมาตรฐานได้ง่ายและนำไปใช้งานได้ทันที ลดเวลาในการเขียนกฎเอง และเพิ่มความมั่นใจในการ deploy สภาพแวดล้อมบน AWS

6. บทความที่เกี่ยวข้อง

https://dev.classmethod.jp/articles/what-is-aws-cloudformation-2022-th/

https://github.com/aws-cloudformation/cloudformation-guard

https://github.com/aws-cloudformation/aws-guard-rules-registry

https://dev.classmethod.jp/articles/session-governance-and-security-with-infrastructure-as-code-aws-summit-th/

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.