Java Code Examples for com.amazonaws.services.ec2.AmazonEC2ClientBuilder#defaultClient()

The following examples show how to use com.amazonaws.services.ec2.AmazonEC2ClientBuilder#defaultClient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RebootInstance.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply an instance id\n" +
        "Ex: RebootInstance <instance_id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String instance_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    RebootInstancesRequest request = new RebootInstancesRequest()
        .withInstanceIds(instance_id);

    RebootInstancesResult response = ec2.rebootInstances(request);

    System.out.printf(
        "Successfully rebooted instance %s", instance_id);
}
 
Example 2
Source File: DeleteSecurityGroup.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a security group id\n" +
        "Ex: DeleteSecurityGroup <security-group-id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String group_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DeleteSecurityGroupRequest request = new DeleteSecurityGroupRequest()
        .withGroupId(group_id);

    DeleteSecurityGroupResult response = ec2.deleteSecurityGroup(request);

    System.out.printf(
        "Successfully deleted security group with id %s", group_id);
}
 
Example 3
Source File: DescribeAddresses.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeAddressesResult response = ec2.describeAddresses();

    for(Address address : response.getAddresses()) {
        System.out.printf(
                "Found address with public IP %s, " +
                "domain %s, " +
                "allocation id %s " +
                "and NIC id %s",
                address.getPublicIp(),
                address.getDomain(),
                address.getAllocationId(),
                address.getNetworkInterfaceId());
    }
}
 
Example 4
Source File: CreateKeyPair.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a key pair name\n" +
        "Ex: CreateKeyPair <key-pair-name>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String key_name = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    CreateKeyPairRequest request = new CreateKeyPairRequest()
        .withKeyName(key_name);

    CreateKeyPairResult response = ec2.createKeyPair(request);

    System.out.printf(
        "Successfully created key pair named %s",
        key_name);
}
 
Example 5
Source File: ReleaseAddress.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply an allocation ID.\n" +
        "Ex: ReleaseAddress <allocation_id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String alloc_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    ReleaseAddressRequest request = new ReleaseAddressRequest()
        .withAllocationId(alloc_id);

    ReleaseAddressResult response = ec2.releaseAddress(request);

    System.out.printf(
        "Successfully released elastic IP address %s", alloc_id);
}
 
Example 6
Source File: DeleteKeyPair.java    From aws-doc-sdk-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a key pair name\n" +
        "Ex: DeleteKeyPair <key-pair-name>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String key_name = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DeleteKeyPairRequest request = new DeleteKeyPairRequest()
        .withKeyName(key_name);

    DeleteKeyPairResult response = ec2.deleteKeyPair(request);

    System.out.printf(
        "Successfully deleted key pair named %s", key_name);
}
 
Example 7
Source File: DescribeRegionsAndZones.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
    {
        final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

// snippet-start:[ec2.java1.describe_region_and_zones.regions]
        DescribeRegionsResult regions_response = ec2.describeRegions();

        for(Region region : regions_response.getRegions()) {
            System.out.printf(
                "Found region %s " +
                "with endpoint %s",
                region.getRegionName(),
                region.getEndpoint());
        }
// snippet-end:[ec2.java1.describe_region_and_zones.regions]

// snippet-start:[ec2.java1.describe_region_and_zones.zones]
        DescribeAvailabilityZonesResult zones_response =
            ec2.describeAvailabilityZones();

        for(AvailabilityZone zone : zones_response.getAvailabilityZones()) {
            System.out.printf(
                "Found availability zone %s " +
                "with status %s " +
                "in region %s",
                zone.getZoneName(),
                zone.getState(),
                zone.getRegionName());
        }
// snippet-end:[ec2.java1.describe_region_and_zones.zones]
    }
 
Example 8
Source File: DescribeAccount.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {

        // snippet-start:[ec2.java1.describe_account.main]
        AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

        try{
            DescribeAccountAttributesResult accountResults = ec2.describeAccountAttributes();
            List<AccountAttribute> accountList = accountResults.getAccountAttributes();

            for (ListIterator iter = accountList.listIterator(); iter.hasNext(); ) {

                AccountAttribute attribute = (AccountAttribute) iter.next();
                System.out.print("\n The name of the attribute is "+attribute.getAttributeName());
                List<AccountAttributeValue> values = attribute.getAttributeValues();

                 //iterate through the attribute values
                for (ListIterator iterVals = values.listIterator(); iterVals.hasNext(); ) {
                    AccountAttributeValue myValue = (AccountAttributeValue) iterVals.next();
                    System.out.print("\n The value of the attribute is "+myValue.getAttributeValue());
                }
            }
            System.out.print("Done");
        }
        catch (Exception e)
        {
            e.getStackTrace();
        }
        // snippet-end:[ec2.java1.describe_account.main]
    }
 
Example 9
Source File: DescribeKeyPairs.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeKeyPairsResult response = ec2.describeKeyPairs();

    for(KeyPairInfo key_pair : response.getKeyPairs()) {
        System.out.printf(
            "Found key pair with name %s " +
            "and fingerprint %s",
            key_pair.getKeyName(),
            key_pair.getKeyFingerprint());
    }
}
 
Example 10
Source File: DescribeSecurityGroups.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a group id\n" +
        "Ex: DescribeSecurityGroups <group-id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String group_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    DescribeSecurityGroupsRequest request =
        new DescribeSecurityGroupsRequest()
            .withGroupIds(group_id);

    DescribeSecurityGroupsResult response =
        ec2.describeSecurityGroups(request);

    for(SecurityGroup group : response.getSecurityGroups()) {
        System.out.printf(
            "Found security group with id %s, " +
            "vpc id %s " +
            "and description %s",
            group.getGroupId(),
            group.getVpcId(),
            group.getDescription());
    }
}
 
Example 11
Source File: DescribeInstances.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();
    boolean done = false;

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    while(!done) {
        DescribeInstancesResult response = ec2.describeInstances(request);

        for(Reservation reservation : response.getReservations()) {
            for(Instance instance : reservation.getInstances()) {
                System.out.printf(
                    "Found instance with id %s, " +
                    "AMI %s, " +
                    "type %s, " +
                    "state %s " +
                    "and monitoring state %s",
                    instance.getInstanceId(),
                    instance.getImageId(),
                    instance.getInstanceType(),
                    instance.getState().getName(),
                    instance.getMonitoring().getState());
            }
        }

        request.setNextToken(response.getNextToken());

        if(response.getNextToken() == null) {
            done = true;
        }
    }
}
 
Example 12
Source File: AllocateAddress.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply an instance id\n" +
        "Ex: AllocateAddress <instance_id>\n";

    if (args.length != 1) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String instance_id = args[0];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    AllocateAddressRequest allocate_request = new AllocateAddressRequest()
        .withDomain(DomainType.Vpc);

    AllocateAddressResult allocate_response =
        ec2.allocateAddress(allocate_request);

    String allocation_id = allocate_response.getAllocationId();

    AssociateAddressRequest associate_request =
        new AssociateAddressRequest()
            .withInstanceId(instance_id)
            .withAllocationId(allocation_id);

    AssociateAddressResult associate_response =
        ec2.associateAddress(associate_request);

    System.out.printf(
        "Successfully associated Elastic IP address %s " +
        "with instance %s",
        associate_response.getAssociationId(),
        instance_id);
}
 
Example 13
Source File: CreateInstance.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply an instance name and AMI image id\n" +
        "Ex: CreateInstance <instance-name> <ami-image-id>\n";

    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String name = args[0];
    String ami_id = args[1];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    RunInstancesRequest run_request = new RunInstancesRequest()
        .withImageId(ami_id)
        .withInstanceType(InstanceType.T1Micro)
        .withMaxCount(1)
        .withMinCount(1);

    RunInstancesResult run_response = ec2.runInstances(run_request);

    String reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();

    Tag tag = new Tag()
        .withKey("Name")
        .withValue(name);

    CreateTagsRequest tag_request = new CreateTagsRequest()
        .withTags(tag);

    CreateTagsResult tag_response = ec2.createTags(tag_request);

    System.out.printf(
        "Successfully started EC2 instance %s based on AMI %s",
        reservation_id, ami_id);
}
 
Example 14
Source File: Ec2Facade.java    From aws-service-catalog-terraform-reference-architecture with Apache License 2.0 4 votes vote down vote up
public Ec2Facade() {
    this.ec2 = AmazonEC2ClientBuilder.defaultClient();
}
 
Example 15
Source File: CreateSecurityGroup.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args)
{
    final String USAGE =
        "To run this example, supply a group name, group description and vpc id\n" +
        "Ex: CreateSecurityGroup <group-name> <group-description> <vpc-id>\n";

    if (args.length != 3) {
        System.out.println(USAGE);
        System.exit(1);
    }

    String group_name = args[0];
    String group_desc = args[1];
    String vpc_id = args[2];

    final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

    CreateSecurityGroupRequest create_request = new
        CreateSecurityGroupRequest()
            .withGroupName(group_name)
            .withDescription(group_desc)
            .withVpcId(vpc_id);

    CreateSecurityGroupResult create_response =
        ec2.createSecurityGroup(create_request);

    System.out.printf(
        "Successfully created security group named %s",
        group_name);

    IpRange ip_range = new IpRange()
        .withCidrIp("0.0.0.0/0");

    IpPermission ip_perm = new IpPermission()
        .withIpProtocol("tcp")
        .withToPort(80)
        .withFromPort(80)
        .withIpv4Ranges(ip_range);

    IpPermission ip_perm2 = new IpPermission()
        .withIpProtocol("tcp")
        .withToPort(22)
        .withFromPort(22)
        .withIpv4Ranges(ip_range);

    AuthorizeSecurityGroupIngressRequest auth_request = new
        AuthorizeSecurityGroupIngressRequest()
            .withGroupName(group_name)
            .withIpPermissions(ip_perm, ip_perm2);

    AuthorizeSecurityGroupIngressResult auth_response =
        ec2.authorizeSecurityGroupIngress(auth_request);

    System.out.printf(
        "Successfully added ingress policy to security group %s",
        group_name);
}
 
Example 16
Source File: FindRunningInstances.java    From aws-doc-sdk-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {

        // snippet-start:[ec2.java1.running_instances.main]
        AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient();

        try {
            //Create the Filter to use to find running instances
            Filter filter = new Filter("instance-state-name");
            filter.withValues("running");

            //Create a DescribeInstancesRequest
            DescribeInstancesRequest request = new DescribeInstancesRequest();
            request.withFilters(filter);

            // Find the running instances
            DescribeInstancesResult response = ec2.describeInstances(request);

            for (Reservation reservation : response.getReservations()){

                for (Instance instance : reservation.getInstances()) {

                    //Print out the results
                    System.out.printf(
                            "Found reservation with id %s, " +
                                    "AMI %s, " +
                                    "type %s, " +
                                    "state %s " +
                                    "and monitoring state %s",
                            instance.getInstanceId(),
                            instance.getImageId(),
                            instance.getInstanceType(),
                            instance.getState().getName(),
                            instance.getMonitoring().getState());
                }
            }
            System.out.print("Done");

        } catch (SdkClientException e) {
            e.getStackTrace();
        }
        // snippet-end:[ec2.java1.running_instances.main]
    }