如何使用Java过滤Amazon EC2中的启动时间日期?

时间:2018-07-04 06:03:44

标签: java amazon-s3 amazon-ec2

下面是我的代码。我试图获取在当前日期之前30天启动的实例ID。我无法获取数据,因为它返回了一个空列表。

public List<String> getFilteredInstances(){
        List<String> instanceIds=getAllInstanceIds();
        Filter instanceIdFilter = new Filter("instance-id",instanceIds);
        //Creating date
        LocalDate today = LocalDate.now();
        List<String> launchTimeFilterValue = new ArrayList<>();
        //Subtracting 30 days from the current date
        launchTimeFilterValue.add(today.minusDays(30).toString());
        Filter launchTimeFilter = new Filter("launch-time",launchTimeFilterValue);
        AmazonEC2 amazonEC2 = AmazonEC2ClientBuilder.defaultClient();
        DescribeInstancesRequest request = new DescribeInstancesRequest();
        DescribeInstancesResult response = amazonEC2.describeInstances(request.withFilters(instanceIdFilter, launchTimeFilter));

        List<String> filteredInstanceIds=new ArrayList<>();
           response.getReservations().stream()
               .flatMap(reservation -> reservation.getInstances().stream())
               .forEach(instance -> filteredInstanceIds.add(instance.getInstanceId()));

        return filteredInstanceIds;}

0 个答案:

没有答案