[英]How to use boto3 to add Security Group rule between Security Groups of different accounts?
我对boto3的世界相当新,并且我正在尝试使用它(版本1.7.57)
a)在账户A,VPC A中创建一个安全组(让我们称之为sg-a)
b)在账户B,VPC B中创建一个安全组(让我们称之为sg-b)
c)创建一组允许彼此交谈的规则
a)和b)很容易使用
ec2_client.create_security_group(...)
更新:
在create_security_group调用中添加更多信息以获得完整性。 如果使用ec2客户端create_security_group()调用(而不是VPC资源调整),并且您不希望在默认VPC中创建安全组,请确保包含VpcId参数。
ec2_client.create_security_group
(
Description='This is a description',
GroupName='SecurityGroupTest',
VpcId=some_vpc_id
)
结束更新
我从这些调用的返回中获取安全组ID(例如“sg-a”和“sg-b”),然后尝试使用它们来制定规则:
ec2_client.authorize_security_group_ingress(
FromPort=80,
ToPort=80,
IpProtocol='tcp',
GroupId='sg-a',
SourceGroup='sg-b',
GroupOwner='Account B's ID'
)
这会导致以下错误:
botocore.exceptions.ParamValidationError: Parameter validation failed:
Unknown parameter in input: "GroupOwner", must be one of: CidrIp, FromPort, GroupId, GroupName, IpPermissions, IpProtocol, SourceSecurityGroupName, SourceSecurityGroupOwnerId, ToPort, DryRun
Unknown parameter in input: "SourceGroup", must be one of: CidrIp, FromPort, GroupId, GroupName, IpPermissions, IpProtocol, SourceSecurityGroupName, SourceSecurityGroupOwnerId, ToPort, DryRun
这似乎与AWS API文档有所不同https://docs.aws.amazon.com/cli/latest/reference/ec2/authorize-security-group-ingress.html
其中列出了source-group
和group-owner
作为AWS API authorize-security-group-ingress
调用的有效参数。 事实上,
aws ec2 authorize-security-group-ingress --group-id sg-a --protocol tcp --port 80 --source-group sg-b --group-owner <Accont B's ID>
工作得很漂亮。
所以....我猜也是
a)boto3尚未更新以支持这些参数
要么
b)我错过了什么。
我希望我错过了一些东西。
这是工作代码。
笔记:
蟒蛇:
ip_perm = [{
'IpProtocol': 'tcp',
'FromPort': 22,
'ToPort': 22,
'UserIdGroupPairs': [{
'GroupId': src_sg_id, # ID (starts with sg-...)
'UserId': src_account # The account number of the other side
}]
}]
response = client.authorize_security_group_ingress(
IpPermissions=ip_perm,
GroupId=sg_id) # This is the security group to add the rule to
注意:如果要在控制台中执行此操作,请将“入站源”指定为accountno / sg-id。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.