简体   繁体   English

如何在 AWS EC2 实例上将 IP 地址的大列表列入白名单?

[英]How to whitelist a large list of IP addresses on AWS EC2 instances?

I have a large list of IPs (>100) that need to be whitelisted for both inbound and outbound communication on EC2 instances.我有大量 IP(>100)需要列入 EC2 实例上的入站和出站通信的白名单。 Is there a way to whitelist them collectively?有没有办法将它们一起列入白名单? (The IPs also don't belong to a range and are discontinuous). (IP 也不属于某个范围并且是不连续的)。 We are currenlty using security groups for whitelisting IPs, but I couldn't find an easy way to whitelist a large collection of IPs.我们目前正在使用安全组将 IP 列入白名单,但我找不到一种简单的方法来将大量 IP 列入白名单。

PS- I tried exploring IP sets in AWS WAF, but it requires setting up an application load balancer, additionally since we are already using security groups, blocking IPs at the application layer (via WAF) will also block IPs that are already whitelisted at the EC2 level. PS-我尝试在 AWS WAF 中探索 IP 集,但它需要设置一个应用程序负载均衡器,此外,由于我们已经在使用安全组,在应用程序层(通过 WAF)阻止 IP 也会阻止已经列入白名单的 IP EC2级别。

Thanks in Advance!提前致谢!

For an EC2 port access the best option you got is security groups.对于 EC2 端口访问,您获得的最佳选择是安全组。 But you better use IaC to manage this such as Terraform or CloudFormation - it will help you to better manage the IP list and save them all in one file.但是您最好使用 IaC 来管理它,例如 Terraform 或 CloudFormation——它将帮助您更好地管理 IP 列表并将它们全部保存在一个文件中。

Here is a reference for Terraform: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group这是 Terraform 的参考: https ://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group

You can use managed prefix lists which allows you to make it easier to configure and maintain your security groups.您可以使用托管前缀列表,从而更轻松地配置和维护您的安全组。

Keep in mind that every entry in the prefix list count as a rule in your SG, so probably you will need to request an increase of the default quota (60 inbound and 60 outbound rules per security group).请记住,前缀列表中的每个条目都算作 SG 中的一条规则,因此您可能需要请求增加默认配额(每个安全组 60 个入站和 60 个出站规则)。

Another easy option is you maintain a csv file to store IP addresses and use a python script to update your security group.另一个简单的选择是您维护一个 csv 文件来存储 IP 地址并使用 python 脚本来更新您的安全组。 Since working with Excel/csv files are more popular you can easily find python scripts to read csv files and the use boto3 to update your security group.由于使用 Excel/csv 文件更受欢迎,您可以轻松找到 python 脚本来读取 csv 文件并使用 boto3 更新您的安全组。

Maintaining the whitelisted IPs in a Security group, which is the best option I believe , you don't need for both inbound and outbound.在安全组中维护列入白名单的 IP,这是我认为的最佳选择,您不需要入站和出站。 Since Security group is stateful, you need to mention whitelisted IPs in inbound rule only.由于安全组是有状态的,您只需在入站规则中提及列入白名单的 IP。 ( You may have different use case for outbound, which I am not sure ) 您可能有不同的出站用例,我不确定

However, I think the best way to handle it via an automation.但是,我认为通过自动化处理它的最佳方法。 You can create a Dynamodb table with different rule entries, enable dynamodb stream, any change triggers a Lambda which inturn creates/amends security group associated with the EC2.您可以创建具有不同规则条目的 Dynamodb 表,启用 dynamodb 流,任何更改都会触发 Lambda,Lambda 进而创建/修改与 EC2 关联的安全组。

I know it's pretty late but you can allow all IPs on your security group and inside your ec2 machine if it's Linux based then set iptables to explicitly allow those ip addresses and block anything else.我知道已经很晚了,但如果基于 Linux,您可以允许安全组和 ec2 机器内部的所有 IP,然后设置 iptables 以明确允许这些 ip 地址并阻止其他任何内容。

iptables -A INPUT -s ip1,ip2,ip3 -j ACCEPT
iptables -A INPUT -s 0.0.0.0/0 -d ip2 -j DROP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM