javaamazon-web-servicesec2-api-tools

How to check EC2instance is running or stop in AWS using java?


how do i know my instance is running or stop on EC2 using AWS-java-sdk

   DescribeInstancesRequest dis =new DescribeInstancesRequest();   
   dis.setInstanceIds(instancesList);
   System.out.println(ec2.describeInstances(dis));

i try with this but its not give me output as i requires

any one help for instance is running or stop on EC2 ?


Solution

  • DescribeInstancesRequest dis = new DescribeInstancesRequest();   
    dis.setInstanceIds(instancesList);
    DescribeInstancesResult disresult = ec2.describeInstances(dis);
    List<Reservation> list = disresult.getReservations();
    
    System.out.println("-------------- status of instances -------------");
    for (Reservation res : list) {
        List <Instance> instancelist = res.getInstances();
    
        for (Instance instance : instancelist) {
            System.out.println("Instance Status : " + instance.getState().getName());
            List <Tag> t1 = instance.getTags();
            for (Tag teg : t1) {
                System.out.println("Instance Name : "+ teg.getValue());
            }
        }   
        System.out.println("------------------------------------------------");
    }