Search in sources :

Example 16 with Ec2Client

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

the class AllocateAddress 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_EAST_1;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    System.out.println(getAllocateAddress(ec2, instanceId));
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Example 17 with Ec2Client

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

the class CreateInstance method createEC2Instance.

// snippet-start:[ec2.java2.create_instance.main]
public static String createEC2Instance(Ec2Client ec2, String name, String amiId) {
    RunInstancesRequest runRequest = RunInstancesRequest.builder().imageId(amiId).instanceType(InstanceType.T1_MICRO).maxCount(1).minCount(1).build();
    RunInstancesResponse response = ec2.runInstances(runRequest);
    String instanceId = response.instances().get(0).instanceId();
    Tag tag = Tag.builder().key("Name").value(name).build();
    CreateTagsRequest tagRequest = CreateTagsRequest.builder().resources(instanceId).tags(tag).build();
    try {
        ec2.createTags(tagRequest);
        System.out.printf("Successfully started EC2 Instance %s based on AMI %s", instanceId, amiId);
        return instanceId;
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : RunInstancesResponse(software.amazon.awssdk.services.ec2.model.RunInstancesResponse) CreateTagsRequest(software.amazon.awssdk.services.ec2.model.CreateTagsRequest) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) RunInstancesRequest(software.amazon.awssdk.services.ec2.model.RunInstancesRequest) Tag(software.amazon.awssdk.services.ec2.model.Tag)

Example 18 with Ec2Client

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

the class CreateSecurityGroup method main.

public static void main(String[] args) {
    final String USAGE = "\n" + "Usage:\n" + "   <groupName> <groupDesc> <vpcId> \n\n" + "Where:\n" + "   groupName - a group name (for example, TestKeyPair). \n\n" + "   groupDesc - a group description  (for example, TestKeyPair). \n\n" + "   vpcId - a VPC ID that you can obtain from the AWS Management Console (for example, vpc-xxxxxf2f). \n\n";
    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String groupName = args[0];
    String groupDesc = args[1];
    String vpcId = args[2];
    // snippet-start:[ec2.java2.create_security_group.client]
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    // snippet-end:[ec2.java2.create_security_group.client]
    String id = createEC2SecurityGroup(ec2, groupName, groupDesc, vpcId);
    System.out.printf("Successfully created Security Group with this ID %s", id);
    ec2.close();
}
Also used : Region(software.amazon.awssdk.regions.Region) Ec2Client(software.amazon.awssdk.services.ec2.Ec2Client)

Example 19 with Ec2Client

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

the class DescribeInstances method describeEC2Instances.

// snippet-start:[ec2.java2.describe_instances.main]
public static void describeEC2Instances(Ec2Client ec2) {
    boolean done = false;
    String nextToken = null;
    try {
        do {
            DescribeInstancesRequest request = DescribeInstancesRequest.builder().maxResults(6).nextToken(nextToken).build();
            DescribeInstancesResponse response = ec2.describeInstances(request);
            for (Reservation reservation : response.reservations()) {
                for (Instance instance : reservation.instances()) {
                    System.out.println("Instance Id is " + instance.instanceId());
                    System.out.println("Image id is " + instance.imageId());
                    System.out.println("Instance type is " + instance.instanceType());
                    System.out.println("Instance state name is " + instance.state().name());
                    System.out.println("monitoring information is " + instance.monitoring().state());
                }
            }
            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) 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 20 with Ec2Client

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

the class DescribeSecurityGroups method main.

public static void main(String[] args) {
    final String USAGE = "To run this example, supply a group id\n" + "Ex: DescribeSecurityGroups <groupId>\n";
    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String groupId = args[0];
    Region region = Region.US_WEST_2;
    Ec2Client ec2 = Ec2Client.builder().region(region).build();
    describeEC2SecurityGroups(ec2, groupId);
    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