在安全组中添加 IP 列表作为源 IP

Add a list of IP as Source IP in Security Group

我有一个 IP 地址列表。我想允许来自他们的 tcp/22 流量并阻止任何其他 IP 地址。该列表很长,包含大约 50-60 个 IP 地址。如何将其添加到安全组,而无需手动将它们一一添加。

谢谢!

解决方案:

我找到了使用 aws cli 的方法

允许通过 SSH 访问一系列 cidrs

bash# for ip in {csv_list_of_cidrs}
> do
> aws ec2 authorize-security-group-ingress --group-id <sg_ig_here> --protocol tcp --port 22 --cidr $ip
> done

例如:

for ip in {1.1.1.1/8,2.2.2.2/16,3.2.3.12/29}
do
aws ec2 authorize-security-group-ingress --group-id sg-xxxxxxxxx --protocol tcp --port 22 --cidr $ip
done

使用boto3的解决方案参考下面的答案

这是一个您可以使用的 boto3 脚本:

    ec2_client = boto3.client('ec2')
    ec2_client.authorize_security_group_ingress(
        GroupId=group_id,
        IpPermissions=[
            {
                'IpProtocol': 'tcp',
                'FromPort': 22,
                'ToPort': 22,
                'IpRanges': [ip_list_here]
            }
        ])