如何使用boto3的revoke_ingress方法从入站规则中删除特定的安全组

How to use revoke_ingress method of boto3 to remove a particular security group from inbound rules

我想使用 revoke_ingress 方法 boto3

从另一个安全组 sg-ZZ 的入站规则中删除特定的安全 sg-yy 组

来源:sg-ZZ 目标:sg-yy

我试过了:

    response = security_group_source.revoke_ingress(
        FromPort=FromPort,
        GroupName=groupName_source,
        IpPermissions=[
            {'ToPort': ToPort,
             'UserIdGroupPairs': [
                 {
                     'Description': description_target,
                     'GroupId': group_id_target,
                     'GroupName': groupName_target,
                     'VpcId': VpcId_target,

                 },
             ]
             },
        ],
        SourceSecurityGroupName=groupName_source,
        SourceSecurityGroupOwnerId=owner_id_source,
        ToPort=ToPort,
        DryRun=True
    )
    print( response )

但我收到错误消息:调用 RevokeSecurityGroupIngress 操作时发生错误 (InvalidGroup.NotFound):安全组 'sg_group_name' 在默认 VPC 'vpc-1111'[=14= 中不存在]

我不能使用:

security_group.revoke_ingress(
    IpPermissions = IpPermissions,
)

因为我需要删除特定的安全组

有人可以帮我吗?

这可以通过保持正确的参数来解决。 Boto3 文档中没有明确指定用于非默认 VPC 的参数,但通过错误解决对我有用。 非默认 VPC 的工作代码:

       response = security_group_source.revoke_ingress(
            GroupId=group_id_source,
            IpPermissions=[
                {'FromPort': FromPort,
                 'IpProtocol': IpProtocol,
                 'ToPort': ToPort,
                 'UserIdGroupPairs': [
                     {
                         'GroupId': group_id_target,
                         'VpcId': VpcId_target,
                     },
                 ]
                 },
            ],
            DryRun=False
        )