Terminate AWS instances with Java SDK

    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(
        AWSAccessKeyId, SecretAccessKey);

    AmazonEC2Client ec2Client = new AmazonEC2Client(awsCredentials);
    ec2Client.setEndpoint("ec2.us-east-1.amazonaws.com"); // Zone

    List<String> instancesToTerminate = new ArrayList<String>();
    
    DescribeInstancesResult result = ec2Client.describeInstances();
    List<Reservation> reservations = result.getReservations();
    for (Reservation reservation : reservations) {
      List<Instance> instances = reservation.getInstances();
      for (Instance instance : instances) {
        System.out.println("Terminating: " + instance.getInstanceId());
        instancesToTerminate.add(instance.getInstanceId());
      }
    }

    TerminateInstancesRequest term = new TerminateInstancesRequest();
    term.setInstanceIds(instancesToTerminate);
    ec2Client.terminateInstances(term);

No comments:

Post a Comment

무한의 세계

무한 집합의 크기 Cardinality , 즉 원소의 개수를 수학에서는 '농도'라고 말한다. 유한 집합의 크기는 그대로 원소의 개수 이지만, 무한 집합의 경우는 원소의 개수를 낱낱이 셈하는 것은 불가능하기 때문에 '농도'라...