我们有一些安全组织,其中有相当多的规则。 为了适应微小的差异,不必重新创build许多安全组的相同规则,是否可以复制安全组作为起点,或使用inheritance等?
看起来你可以从Web界面复制安全组。 但是,您可以使用AWS CLI 创build安全组 :
命令:
$ aws ec2 describe-security-groups --group-id MySecurityGroupID
输出:
{ "securityGroupInfo": [ { "ipPermissionsEgress": [], "groupId": "sg-903004f8", "ipPermissions": [], "groupName": "MySecurityGroup", "ownerId": "803981987763", "groupDescription": "AWS-CLI-Example" } ], "requestId": "afb680df-d7b1-4f6a-b1a7-344fdb1e3532" }
并使用命令添加规则:
aws ec2 authorize-security-group-ingress --group-id MySecurityGroupID --ip-protocol tcp --from-port 22 --to-port 22 --cidr-ip 0.0.0.0/0
输出:
{ "return": "true", "requestId": "c24a1c93-150b-4a0a-b56b-b149c0e660d2" }
从那里你应该能够弄清楚如何简化安全组的创build。
AWS EC2控制台允许您select安全组,并立即在用户界面中执行“复制到新的”操作。
考虑看看这个博客。 这可能对你在看什么有用。
这里是我写的自定义库中的“复制安全组”python / boto方法,以使这些事情更容易/自动化。最终,这是我想出的解决scheme。
vpcId is the Virtual Private Cloud Id keys is a dictionary with your AWS keys
其余的应该是直截了当的弄清楚。
def copyEC2SecurityGroup(self, keys, region, securityGroupName, newSecurityGroupName = None, newRegion = None, vpcId = None): newEc2Connection = None print("Creating ec2Connection for source region: " + region) ec2Connection = lib.getEc2Connection(region, keys) if newRegion is None: newRegion = region else: print("New Region Detected, creating for New region: " + newRegion) newEc2Connection = lib.getEc2Connection(newRegion, keys) newRegionInfo = newEc2Connection.region print("new region is: %s" % newRegion) if newSecurityGroupName is None: newSecurityGroupName = securityGroupName print ("new security group is: %s" % newSecurityGroupName) # if copying in the same region the new security group cannot have the same name. if newRegion == region: if newSecurityGroupName == securityGroupName: print ("Old and new security groups cannot have the same name when copying to the same region.") exit(1) groups = [group for group in ec2Connection.get_all_security_groups() if group.name == securityGroupName] print"got groups count " + str(len(groups)) if groups: theOldGroup = groups[0] print theOldGroup.rules else: print("Can't find security group by the name of: %s" % securityGroupName) exit(1) print groups pprint(theOldGroup) if newEc2Connection is not None: print("Creating new security group in new region") sg = newEc2Connection.create_security_group(newSecurityGroupName, newSecurityGroupName, vpcId) sleep(5) else: print("Creating new security group in current region") sg = ec2Connection.create_security_group(newSecurityGroupName, newSecurityGroupName, vpcId) sleep(5) source_groups = [] for rule in theOldGroup.rules: for grant in rule.grants: strGrant = str(grant) print(strGrant) if strGrant.startswith("sg"): print("Cannot copy 'security group rule' (%s)... only cidr_ip's eg xxx.xxx.xxx.xxx/yy." % strGrant) continue grant_nom = grant.name or grant.group_id if grant_nom: if grant_nom not in source_groups: source_groups.append(grant_nom) sg.authorize(rule.ip_protocol, rule.from_port, rule.to_port, grant) else: sg.authorize(rule.ip_protocol, rule.from_port, rule.to_port, grant.cidr_ip) return sg
从AWS创build安全组文档,您可以使用控制台复制安全组。
由于缺乏正确的在线方式,我创build了一个超级简单的脚本来处理它。 看看你是否有兴趣
在EC2控制台中,单击“启动实例”,然后继续input虚拟信息,直到进入安全组部分。
从这里点击“select一个现有的安全组”,在下面你会看到你拥有的特定VPC的所有安全组。 您应该在“操作”下看到“复制到新的”链接,使用它将所有ACL复制到新的SG。
或者我想你可以使用脚本 – 这是更快的IMO ..
这是我为完成这个工作而编写的脚本。 它基于这个,并且适用于Python3。
示例用法是
python3 aws_sg_migrate.py --vpc=vpc-05643b6c --shell --src=us-east-1 --dest=us-west-1 sg-111111