Search in sources :

Example 96 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project SimianArmy by Netflix.

the class AWSClient method createTagsForResources.

@Override
public void createTagsForResources(Map<String, String> keyValueMap, String... resourceIds) {
    Validate.notNull(keyValueMap);
    Validate.notEmpty(keyValueMap);
    Validate.notNull(resourceIds);
    Validate.notEmpty(resourceIds);
    AmazonEC2 ec2Client = ec2Client();
    List<Tag> tags = new ArrayList<Tag>();
    for (Map.Entry<String, String> entry : keyValueMap.entrySet()) {
        tags.add(new Tag(entry.getKey(), entry.getValue()));
    }
    CreateTagsRequest req = new CreateTagsRequest(Arrays.asList(resourceIds), tags);
    ec2Client.createTags(req);
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) Tag(com.amazonaws.services.ec2.model.Tag)

Example 97 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project SimianArmy by Netflix.

the class AWSClient method describeSecurityGroups.

/**
 * Describe a set of security groups.
 *
 * @param groupNames the names of the groups to find
 * @return a list of matching groups
 */
public List<SecurityGroup> describeSecurityGroups(String... groupNames) {
    AmazonEC2 ec2Client = ec2Client();
    DescribeSecurityGroupsRequest request = new DescribeSecurityGroupsRequest();
    if (groupNames == null || groupNames.length == 0) {
        LOGGER.info(String.format("Getting all EC2 security groups in region %s.", region));
    } else {
        LOGGER.info(String.format("Getting EC2 security groups for %d names in region %s.", groupNames.length, region));
        request.withGroupNames(groupNames);
    }
    DescribeSecurityGroupsResult result;
    try {
        result = ec2Client.describeSecurityGroups(request);
    } catch (AmazonServiceException e) {
        if (e.getErrorCode().equals("InvalidGroup.NotFound")) {
            LOGGER.info("Got InvalidGroup.NotFound error for security groups; returning empty list");
            return Collections.emptyList();
        }
        throw e;
    }
    List<SecurityGroup> securityGroups = result.getSecurityGroups();
    LOGGER.info(String.format("Got %d EC2 security groups in region %s.", securityGroups.size(), region));
    return securityGroups;
}
Also used : AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 98 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project SimianArmy by Netflix.

the class AWSClient method deleteSnapshot.

/**
 * {@inheritDoc}
 */
@Override
public void deleteSnapshot(String snapshotId) {
    Validate.notEmpty(snapshotId);
    LOGGER.info(String.format("Deleting snapshot %s in region %s.", snapshotId, region));
    AmazonEC2 ec2Client = ec2Client();
    DeleteSnapshotRequest request = new DeleteSnapshotRequest().withSnapshotId(snapshotId);
    ec2Client.deleteSnapshot(request);
}
Also used : AmazonEC2(com.amazonaws.services.ec2.AmazonEC2)

Example 99 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class AllocateAddress method main.

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);
}
Also used : AssociateAddressResult(com.amazonaws.services.ec2.model.AssociateAddressResult) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AllocateAddressRequest(com.amazonaws.services.ec2.model.AllocateAddressRequest) AllocateAddressResult(com.amazonaws.services.ec2.model.AllocateAddressResult)

Example 100 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project aws-doc-sdk-examples by awsdocs.

the class DeleteKeyPair method main.

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);
}
Also used : DeleteKeyPairResult(com.amazonaws.services.ec2.model.DeleteKeyPairResult) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DeleteKeyPairRequest(com.amazonaws.services.ec2.model.DeleteKeyPairRequest)

Aggregations

AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)65 Filter (com.amazonaws.services.ec2.model.Filter)25 Instance (com.amazonaws.services.ec2.model.Instance)20 DescribeImagesRequest (com.amazonaws.services.ec2.model.DescribeImagesRequest)17 DescribeImagesResult (com.amazonaws.services.ec2.model.DescribeImagesResult)15 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)12 Reservation (com.amazonaws.services.ec2.model.Reservation)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)10 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)9 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)8 DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)8 Tag (com.amazonaws.services.ec2.model.Tag)8 AWSCredentials (com.amazonaws.auth.AWSCredentials)6 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)6 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)5 DescribeSecurityGroupsResult (com.amazonaws.services.ec2.model.DescribeSecurityGroupsResult)5 DisassociateAddressRequest (com.amazonaws.services.ec2.model.DisassociateAddressRequest)5 ReleaseAddressRequest (com.amazonaws.services.ec2.model.ReleaseAddressRequest)5 AmazonServiceException (com.amazonaws.AmazonServiceException)4