Amazon web services 如何使用boto3在不同帐户的安全组之间添加安全组规则?

Amazon web services 如何使用boto3在不同帐户的安全组之间添加安全组规则?,amazon-web-services,boto3,Amazon Web Services,Boto3,我对boto3的世界相当陌生,我正试图使用它(版本1.7.57)来 a) 在帐户a、VPC a(我们称之为sg-a)中创建一个安全组 b) 在帐户b、专有网络b(我们称之为sg-b)中创建一个安全组 c) 创建一套规则,让他们彼此交谈 a) b)使用起来很容易 ec2_client.create_security_group(...) 更新: 添加有关create_security_组的更多信息需要完整性。如果使用ec2客户端create_security_group()调用(与VPC资源风格

我对boto3的世界相当陌生,我正试图使用它(版本1.7.57)来
a) 在帐户a、VPC a(我们称之为sg-a)中创建一个安全组
b) 在帐户b、专有网络b(我们称之为sg-b)中创建一个安全组
c) 创建一套规则,让他们彼此交谈

a) b)使用起来很容易

ec2_client.create_security_group(...)
更新:
添加有关create_security_组的更多信息需要完整性。如果使用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”),然后尝试使用这些ID制定规则:

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文档有所不同
它将
源组
组所有者
列为AWS API
授权安全组入口
调用的有效参数。其实,

aws ec2 authorize-security-group-ingress --group-id sg-a  --protocol tcp --port 80 --source-group sg-b --group-owner <Accont B's ID>
aws ec2授权安全组入口--组id sg-a--协议tcp--端口80--源组sg-b--组所有者以下是工作代码

注:

  • 您必须启用VPC对等
  • 安全组仅在同一区域内工作。不能在其他区域中指定安全组
  • Python:

    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。

    并且它与AWS boto3上的文档没有任何偏差。我在周五下班回家的火车上发现了文档的IpPermissions部分,也在这里看到了您的响应。不得不等到今天早上(星期一)测试,但它确实是工作!非常感谢您的帮助!小心不要将boto3 API文档和详细信息与AWS CLI命令和文档混淆。我知道它们不是一回事,但据我所知,boto3将努力实现与底层AWS API的对等。这看起来像是一个案件,只是还没有赶上,但我想确保我没有错过什么。:)请注意,基础AWS API与AWS CLI不同。boto3和CLI之间通常不存在等价性,尽管从功能角度来看存在等价性。下面是的底层API。我正在尝试使用Python编写它。我无法通过错误“安全组“sg example”不存在(它存在)。我已经在两个帐户之间设置了VPC对等。我仍然无法让它两次工作。我也无法让您的AWS CLI示例工作。您能否提供有关AWS CLI命令、您的环境、您的命令创建的安全组规则的屏幕截图的更多详细信息。错误消息似乎至少给出了部分答案:参数名为
    SourceSecurityGroupOwnerId
    ,而不是
    GroupOwner