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());
// }
}
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);
}
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;
}
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;
}
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);
});
}
Aggregations