如何获取尚未分配给 EC2 实例的安全组

How to get security groups not yet assigned to an EC2 instance

我有以下方法:

public String createGroup( String groupName, String description, String vpcId ) {
    logger.debug( "Create Group called");
    CreateSecurityGroupRequest sgrReq = new CreateSecurityGroupRequest();
    sgrReq.setGroupName(groupName);
    sgrReq.setDescription(description);
    sgrReq.setVpcId(vpcId);

    CreateSecurityGroupResult csgRes = ec2Client.createSecurityGroup(sgrReq );
    String groupId = csgRes.getGroupId();
    logger.debug( "Security Group '{}', with ID {}, created in VPC '{}'", groupName, groupId, vpcId);

    return groupId;
}

它创建了一个安全组,当我进入安全组的 EC2 控制台时可以看到它。我还没有通过 ec2Client.modifyInstanceAttribute 将组分配给 EC2 实例。我将使用什么 service/method 的 AWS SDK 来列出所有安全组,甚至是那些尚未分配给任何实例的安全组?明确地说,ec2Client.getEc2Instance(ec2Id).getSecurityGroups() 不起作用,因为该组尚未分配给实例。

您可以使用 DescribeSecurityGroups API without applying any filter.