Search in sources :

Example 16 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project jointware by isdream.

the class EC2Main method main.

/**
 * @param args
 */
public static void main(String[] args) {
    AWSCredentials ac = new BasicAWSCredentials(ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    AWSCredentialsProvider credentialsProvider = new AWSStaticCredentialsProvider(ac);
    AmazonEC2 ec2 = AmazonEC2ClientBuilder.standard().withRegion(Regions.AP_NORTHEAST_1).withCredentials(credentialsProvider).build();
    System.out.println(ec2.getClass().getName());
// for (com.amazonaws.services.ec2.model.Region region : ec2.describeRegions().getRegions()) {
// System.out.println(region.getRegionName());
// }
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider)

Example 17 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project incubator-gobblin by apache.

the class AWSSdkClient method addPermissionsToSecurityGroup.

/**
 * Open firewall for a security group
 *
 * @param groupName Open firewall for this security group
 * @param ipRanges Open firewall for this IP range
 * @param ipProtocol Open firewall for this protocol type (eg. tcp, udp)
 * @param fromPort Open firewall for port range starting at this port
 * @param toPort Open firewall for port range ending at this port
 */
public void addPermissionsToSecurityGroup(String groupName, String ipRanges, String ipProtocol, Integer fromPort, Integer toPort) {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final IpPermission ipPermission = new IpPermission().withIpRanges(ipRanges).withIpProtocol(ipProtocol).withFromPort(fromPort).withToPort(toPort);
    final AuthorizeSecurityGroupIngressRequest authorizeSecurityGroupIngressRequest = new AuthorizeSecurityGroupIngressRequest().withGroupName(groupName).withIpPermissions(ipPermission);
    amazonEC2.authorizeSecurityGroupIngress(authorizeSecurityGroupIngressRequest);
    LOGGER.info("Added permissions: " + ipPermission + " to security group: " + groupName);
}
Also used : AuthorizeSecurityGroupIngressRequest(com.amazonaws.services.ec2.model.AuthorizeSecurityGroupIngressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) IpPermission(com.amazonaws.services.ec2.model.IpPermission)

Example 18 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project incubator-gobblin by apache.

the class AWSSdkClient method getInstancesForGroup.

/**
 * Get list of EC2 {@link Instance}s for a auto scaling group
 *
 * @param groupName Auto scaling group name
 * @param status Instance status (eg. running)
 * @return List of EC2 instances found for the input auto scaling group
 */
public List<Instance> getInstancesForGroup(String groupName, String status) {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final DescribeInstancesResult instancesResult = amazonEC2.describeInstances(new DescribeInstancesRequest().withFilters(new Filter().withName("tag:aws:autoscaling:groupName").withValues(groupName)));
    final List<Instance> instances = new ArrayList<>();
    for (Reservation reservation : instancesResult.getReservations()) {
        for (Instance instance : reservation.getInstances()) {
            if (null == status || null == instance.getState() || status.equals(instance.getState().getName())) {
                instances.add(instance);
                LOGGER.info("Found instance: " + instance + " which qualified filter: " + status);
            } else {
                LOGGER.info("Found instance: " + instance + " but did not qualify for filter: " + status);
            }
        }
    }
    return instances;
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) ArrayList(java.util.ArrayList) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 19 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project incubator-gobblin by apache.

the class AWSSdkClient method getAvailabilityZones.

/**
 * Get availability zones in an Amazon AWS region
 *
 * @return List of availability zones
 */
public List<AvailabilityZone> getAvailabilityZones() {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final DescribeAvailabilityZonesResult describeAvailabilityZonesResult = amazonEC2.describeAvailabilityZones();
    final List<AvailabilityZone> availabilityZones = describeAvailabilityZonesResult.getAvailabilityZones();
    LOGGER.info("Found: " + availabilityZones.size() + " availability zone");
    return availabilityZones;
}
Also used : AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

Example 20 with AmazonEC2

use of com.amazonaws.services.ec2.AmazonEC2 in project photon-model by vmware.

the class AWSRemoteCleanup method deleteNetworkInterfaces.

private void deleteNetworkInterfaces(String vpcId, AmazonEC2 usEastEc2Client) {
    DescribeNetworkInterfacesRequest networkInterfacesRequest = new DescribeNetworkInterfacesRequest().withFilters(new Filter(VPC_KEY, Collections.singletonList(vpcId)));
    DescribeNetworkInterfacesResult networkInterfacesResult = usEastEc2Client.describeNetworkInterfaces(networkInterfacesRequest);
    networkInterfacesResult.getNetworkInterfaces().forEach(networkInterface -> {
        DescribeAddressesRequest addressesRequest = new DescribeAddressesRequest().withFilters(new Filter(NETWORK_INTERFACE_KEY, Collections.singletonList(networkInterface.getNetworkInterfaceId())));
        DescribeAddressesResult addressResult = usEastEc2Client.describeAddresses(addressesRequest);
        addressResult.getAddresses().forEach(address -> {
            // There is no hardcore dependency on EIP, but we may run out of addresses and
            // would be good to disassociate followed by releasing them.
            DisassociateAddressRequest disassociateAddressRequest = new DisassociateAddressRequest().withAssociationId(address.getAssociationId());
            usEastEc2Client.disassociateAddress(disassociateAddressRequest);
            ReleaseAddressRequest releaseAddressRequest = new ReleaseAddressRequest().withAllocationId(address.getAllocationId());
            usEastEc2Client.releaseAddress(releaseAddressRequest);
        });
        // Deleting Network Interfaces
        DeleteNetworkInterfaceRequest deleteNetworkInterfaceRequest = new DeleteNetworkInterfaceRequest().withNetworkInterfaceId(networkInterface.getNetworkInterfaceId());
        this.host.log("Terminating stale NIC: %s", networkInterface.getNetworkInterfaceId());
        usEastEc2Client.deleteNetworkInterface(deleteNetworkInterfaceRequest);
    });
}
Also used : Filter(com.amazonaws.services.ec2.model.Filter) DisassociateAddressRequest(com.amazonaws.services.ec2.model.DisassociateAddressRequest) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) DescribeNetworkInterfacesResult(com.amazonaws.services.ec2.model.DescribeNetworkInterfacesResult) DescribeNetworkInterfacesRequest(com.amazonaws.services.ec2.model.DescribeNetworkInterfacesRequest) ReleaseAddressRequest(com.amazonaws.services.ec2.model.ReleaseAddressRequest) DeleteNetworkInterfaceRequest(com.amazonaws.services.ec2.model.DeleteNetworkInterfaceRequest)

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