code: public static boolean doesSecurityGroupExist(AmazonEC2 ec2,
              String securityGroupName) throws AmazonClientException,
              AmazonServiceException {
  
          DescribeSecurityGroupsRequest securityGroupsRequest =
                  new DescribeSecurityGroupsRequest()
                      .withGroupNames(securityGroupName);
  
          try {
              ec2.describeSecurityGroups(securityGroupsRequest);
              return true;
  
          } catch (AmazonServiceException ase) {
              if (INVALID_GROUP_NOT_FOUND.equals(ase.getErrorCode())) {
                  return false;
              }
              throw ase;
          }
      }
docs: Provides a quick answer to whether a security group exists.
  
  @param ec2
  the EC2 client to use for making service requests
  @param securityGroupName
  the name of the security group being queried
  @throws AmazonClientException
  If any internal errors are encountered inside the client
  while attempting to make the request or handle the response.
  For example if a network connection is not available.
  @throws AmazonServiceException
  If an error response is returned by AmazonEC2 indicating
  either a problem with the data in the request, or a server
  side issue.