Search in sources :

Example 21 with EC2Client

use of software.amazon.awssdk.services.ec2.EC2Client in project aws-doc-sdk-examples by awsdocs.

the class FindRunningInstances method findRunningEC2Instances.

// snippet-start:[ec2.java2.running_instances.main]
public static void findRunningEC2Instances(Ec2Client ec2) {
    try {
        String nextToken = null;
        do {
            Filter filter = Filter.builder().name("instance-state-name").values("running").build();
            DescribeInstancesRequest request = DescribeInstancesRequest.builder().filters(filter).build();
            DescribeInstancesResponse response = ec2.describeInstances(request);
            for (Reservation reservation : response.reservations()) {
                for (Instance instance : reservation.instances()) {
                    System.out.printf("Found Reservation with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.instanceId(), instance.imageId(), instance.instanceType(), instance.state().name(), instance.monitoring().state());
                    System.out.println("");
                }
            }
            nextToken = response.nextToken();
        } while (nextToken != null);
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : Reservation(software.amazon.awssdk.services.ec2.model.Reservation) Filter(software.amazon.awssdk.services.ec2.model.Filter) Instance(software.amazon.awssdk.services.ec2.model.Instance) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) DescribeInstancesRequest(software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest) DescribeInstancesResponse(software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)

Example 22 with EC2Client

use of software.amazon.awssdk.services.ec2.EC2Client in project aws-doc-sdk-examples by awsdocs.

the class RebootInstance method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "   <instanceId> \n\n" + "Where:\n" + "   instanceId - an instance id value that you can obtain from the AWS Console. \n\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String instanceId = args[0];
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    rebootEC2Instance(ec2, instanceId);
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Example 23 with EC2Client

use of software.amazon.awssdk.services.ec2.EC2Client in project aws-doc-sdk-examples by awsdocs.

the class StartStopInstance method stopInstance.

// snippet-end:[ec2.java2.start_stop_instance.start]
// snippet-start:[ec2.java2.start_stop_instance.stop]
public static void stopInstance(Ec2Client ec2, String instanceId) {
    StopInstancesRequest request = StopInstancesRequest.builder().instanceIds(instanceId).build();
    ec2.stopInstances(request);
    System.out.printf("Successfully stopped instance %s", instanceId);
}
Also used : StopInstancesRequest(software.amazon.awssdk.services.ec2.model.StopInstancesRequest)

Example 24 with EC2Client

use of software.amazon.awssdk.services.ec2.EC2Client in project legacy-jclouds-examples by jclouds.

the class MainApp method main.

public static void main(String[] args) throws TimeoutException {
    if (args.length < PARAMETERS)
        throw new IllegalArgumentException(INVALID_SYNTAX);
    // Args
    String accesskeyid = args[0];
    String secretkey = args[1];
    String command = args[2];
    String name = args[3];
    // Init
    RestContext<EC2Client, EC2AsyncClient> context = ContextBuilder.newBuilder("aws-ec2").credentials(accesskeyid, secretkey).build();
    // Get a synchronous client
    EC2Client client = context.getApi();
    try {
        if (command.equals("create")) {
            KeyPair pair = createKeyPair(client, name);
            RunningInstance instance = createSecurityGroupKeyPairAndInstance(client, name);
            System.out.printf("instance %s ready%n", instance.getId());
            System.out.printf("ip address: %s%n", instance.getIpAddress());
            System.out.printf("dns name: %s%n", instance.getDnsName());
            System.out.printf("login identity:%n%s%n", pair.getKeyMaterial());
        } else if (command.equals("destroy")) {
            destroySecurityGroupKeyPairAndInstance(client, name);
        } else {
            throw new IllegalArgumentException(INVALID_SYNTAX);
        }
    } finally {
        // Close connecton
        context.close();
        System.exit(0);
    }
}
Also used : KeyPair(org.jclouds.ec2.domain.KeyPair) RunningInstance(org.jclouds.ec2.domain.RunningInstance) EC2AsyncClient(org.jclouds.ec2.EC2AsyncClient) EC2Client(org.jclouds.ec2.EC2Client)

Example 25 with EC2Client

use of software.amazon.awssdk.services.ec2.EC2Client in project aws-doc-sdk-examples by awsdocs.

the class DescribeInstances method main.

public static void main(String[] args) {
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    describeEC2Instances(ec2);
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Aggregations

Ec2Client (software.amazon.awssdk.services.ec2.Ec2Client)21 Region (software.amazon.awssdk.regions.Region)20 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)16 Test (org.junit.Test)5 EC2Client (software.amazon.awssdk.services.ec2.EC2Client)5 Injector (com.google.inject.Injector)2 DescribeInstancesRequest (software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest)2 DescribeInstancesResponse (software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse)2 Instance (software.amazon.awssdk.services.ec2.model.Instance)2 Reservation (software.amazon.awssdk.services.ec2.model.Reservation)2 AbstractModule (com.google.inject.AbstractModule)1 Module (com.google.inject.Module)1 ListIterator (java.util.ListIterator)1 EC2Client (org.jclouds.aws.ec2.EC2Client)1 EC2AsyncClient (org.jclouds.ec2.EC2AsyncClient)1 EC2Client (org.jclouds.ec2.EC2Client)1 KeyPair (org.jclouds.ec2.domain.KeyPair)1 RunningInstance (org.jclouds.ec2.domain.RunningInstance)1 AccountAttribute (software.amazon.awssdk.services.ec2.model.AccountAttribute)1 AccountAttributeValue (software.amazon.awssdk.services.ec2.model.AccountAttributeValue)1