จัดการ IP ใน Security Group และ Route Table บน AWS ด้วย Prefix List และ Resource Access Manager (RAM)
Managing IPs in Security Group and Route Table on AWS using Prefix List and Resource Access Manager (RAM)
ถ้าเรามี IP ที่จำเป็นต้องใช้ในหลาย ๆ security group (SG), route table และอาจมีอยู่หลาย account ด้วย (เช่นพวก VPN หรือ SaaS app) เราสามารถจัดการยังไงได้บ้าง?
[ENGLISH VERSION IS AT THE BOTTOM]
ท่าทั่วไป
ปกติเราจะใช้วิธีเพิ่ม CIDR IP เข้าไปที่ SG หรือ route table โดยตรง แต่ข้อเสียคือ
- ถ้ามีหลาย account, แต่ละ account มีหลาย SG/route table, และแต่ละ SG/route table ก็มีหลาย rule/routes ซึ่งการเปลี่ยนแปลงในอนาคตไม่ใช่เรื่องง่าย
- แม้เราใช้ script ได้ แต่หาก workload account ใช้ IaC tool ในการ provision ก็จะเป็นปัญหาใหม่ เพราะจะทำให้เกิด drift ที่ IaC
- ต้องจำว่า IP เหล่านั้นมีอะไรบ้าง และเพิ่มเข้าไปครบหรือไม่?
ท่าที่ผมอยากแนะนำ
จากรูปด้านบน เราจะสร้าง managed prefix list ไว้ที่ account หนึ่ง เช่น network account จากนั้นเพิ่ม CIDR block ที่ต้องการเข้าไปและแชร์ให้ทั้ง organization หรือ account อื่น ๆ นำไปใช้กับ SG หรือ route table ด้วย resource access manager (RAM) ซึ่งวิธีนี้จัดการได้ง่ายและ scalable กว่า
วิธี Implement
1) สร้าง Managed Prefix List
- จาก AWS Management Console เข้าไปที่ “VPC” → “Managed prefix lists’ → “Create prefix list”
- จากนั้นใส่ CIDR block ตามต้องการแล้วคลิก “Create prefix list”
- เสร็จแล้วจะได้ prefix list มาแบบนี้
2) แชร์ Prefix List ด้วย RAM
- คลิกที่ “Sharing” → “Create resource share”
- ตั้งชื่อ resource share name, เลือก “Prefix Lists” ที่เมนู drop down และเลือก prefix list ที่เราต้องการแชร์ให้ account อื่น จากนั้นคลิก “Next”
- สามารถแชร์ให้ทั้ง organization หรือจะเลือกเป็น account หรือทั้งสองอย่างก็ได้ จากนั้นทำตาม step ไปจนจบครับ
Notes:
- ถ้าแชร์ใน organization: ทุก account จะเห็น prefix list และใช้งานได้เลย
- ถ้าแชร์ไปที่ account: ฝั่ง account ปลายทางจะต้องเข้าไป accept shared resource ที่ RAM ก่อนจึงจะเห็นและนำ prefix list ไปใช้ได้
3. นำ Prefix List ไปใช้งาน
ทีนี้เราก็สามารถนำ prefix list ไปใช้งานใน SG หรือ route table ได้แล้ว (ไม่ว่าจะเป็น account ไหนก็ตามที่เราแชร์ไป)
ข้อควรระวัง
สังเกตว่า prefix list ผมกำหนด max entries ไว้ 100 หมายถึงผมสามารถเพิ่ม IP prefix ได้ถึง 100 prefixes แม้ปัจจุบันผมจะมีแค่ 3 prefixes ก็ตาม
แต่ตอนเอาไปใช้กับ SG หรือ route table มันจะกินจำนวน rules/routes เสมือนกับเรามี 100 prefixes เลย ทำให้จะเจอ error แบบนี้
- The maximum number of rules per security group has been reached.
- The maximum number of routes has been reached.
นั่นเป็นเพราะว่าค่า default ของ
- SG ตัวหนึ่งจะมี rule ได้ 60 rules
- Route table ตัวหนึ่งจะมี route ได้ 50 routes
วิธีแก้ไข
ให้ใส่ max entries ของ prelix list เท่าที่ใช้งานจริง เช่น ถ้ามี 3 CIDR block ก็ใส่แค่ 3 พอ (มัน resize ได้) หากมันยังเกินค่อยไป request เพิ่ม quota ของ SG/route table อีกที
ถ้าคิดว่าบทความนี้มีประโยชน์ อย่าลืมกด clap และ follow เพื่ออัพเดทเรื่องราวใหม่ ๆ จากผมได้นะครับ
[ENGLISH VERSION]
How can we manage if we have IPs (like VPN or SaaS apps) that need to be used in multiple security groups (SG), route tables, and potentially across multiple accounts?
Common Approach
Normally, we directly add CIDR IP blocks into SG or route table.
- The downside is if there are multiple accounts, each with multiple SG/route tables, and each SG/route table has many rules/routes. Future changes won’t be easy.
- Even though scripts can be used, if some workload accounts use an IaC tool such as Terraform or CloudFormation for provisioning, it will create a new problem because it causes drift in IaC.
- You need to remember all the IPs and whether they have been added completely or not.
Recommended Approach
From the above figure, we will create a managed prefix list in one account, such as a network account. Then add the desired CIDR blocks and share it with the entire organization or other external accounts to use with SG or route tables using the Resource Access Manager (RAM), making it easier and more scalable.
Implementation Steps
1) Create a Managed Prefix List
- Go to “AWS Management Console” → “VPC” → “Managed prefix lists” → “Create prefix list”.
- Then, input the desired CIDR block and click “Create prefix list”.
- You will get a prefix list like this.
2) Share the Prefix List with RAM
- Click “Sharing” → “Create resource share”.
- Set the resource share name, choose “Prefix Lists” from the dropdown menu, and select the prefix list you created. Then, click “Next”.
- You can share it with the whole organization or choose individual accounts or both. Follow the steps until the end.
Notes:
- If shared within the organization: All accounts can see and use the prefix list immediately.
- If shared to an account: The destination account needs to accept the shared resource in RAM before they can see and use the prefix list.
3. Use the Prefix List:
Now we can use the prefix list in SG or route tables (in any account we have shared).
Caution
Note that I set the max entries to 100, meaning I can add up to 100 IP prefixes, even though I currently only have 3 prefixes.
But when we used it with SG or route tables, it consumes the number of rules/routes as if there were 100 prefixes, leading to errors like:
- “The maximum number of rules per security group has been reached.”
- “The maximum number of routes has been reached.”
This is because the default values are:
- One SG can only have up to 60 rules.
- One route table can only have up to 50 routes.
Solution
Configure the max entries of the prefix list to the actual usage. For example, if there are 3 CIDR blocks, just put 3 (it can be resized). If it still exceeds, request an increase in the SG/route table quota later.
If you find this article useful, don’t forget to hit clap and follow for more updates from me.