Search in sources :

Example 1 with AssociateAddressRequest

use of com.amazonaws.services.ec2.model.AssociateAddressRequest in project cloudbreak by hortonworks.

the class AwsResourceConnector method associateElasticIpToInstance.

private void associateElasticIpToInstance(AmazonEC2 amazonEC2Client, String eipAllocationId, String instanceId) {
    AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withAllocationId(eipAllocationId).withInstanceId(instanceId);
    amazonEC2Client.associateAddress(associateAddressRequest);
}
Also used : AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest)

Example 2 with AssociateAddressRequest

use of com.amazonaws.services.ec2.model.AssociateAddressRequest in project GNS by MobilityFirst.

the class AWSEC2 method associateAddress.

/**
   *
   * @param ec2
   * @param ip
   * @param instance
   */
public static void associateAddress(AmazonEC2 ec2, String ip, Instance instance) {
    Address address;
    if ((address = findElasticIP(ec2, ip)) != null) {
        if (address.getDomain().equals("vpc")) {
            System.out.println("VPC Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest().withInstanceId(instance.getInstanceId()).withAllocationId(address.getAllocationId()));
        } else {
            System.out.println("EC2 Classic Elastic IP:  " + ip);
            ec2.associateAddress(new AssociateAddressRequest(instance.getInstanceId(), ip));
        }
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest)

Example 3 with AssociateAddressRequest

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

the class AllocateAddress method getAllocateAddress.

// snippet-start:[ec2.java2.allocate_address.main]
public static String getAllocateAddress(Ec2Client ec2, String instanceId) {
    try {
        AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder().domain(DomainType.VPC).build();
        AllocateAddressResponse allocateResponse = ec2.allocateAddress(allocateRequest);
        String allocationId = allocateResponse.allocationId();
        AssociateAddressRequest associateRequest = AssociateAddressRequest.builder().instanceId(instanceId).allocationId(allocationId).build();
        AssociateAddressResponse associateResponse = ec2.associateAddress(associateRequest);
        return associateResponse.associationId();
    } catch (Ec2Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : AssociateAddressRequest(software.amazon.awssdk.services.ec2.model.AssociateAddressRequest) AssociateAddressResponse(software.amazon.awssdk.services.ec2.model.AssociateAddressResponse) Ec2Exception(software.amazon.awssdk.services.ec2.model.Ec2Exception) AllocateAddressResponse(software.amazon.awssdk.services.ec2.model.AllocateAddressResponse) AllocateAddressRequest(software.amazon.awssdk.services.ec2.model.AllocateAddressRequest)

Example 4 with AssociateAddressRequest

use of com.amazonaws.services.ec2.model.AssociateAddressRequest 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 5 with AssociateAddressRequest

use of com.amazonaws.services.ec2.model.AssociateAddressRequest in project eureka by Netflix.

the class EIPManager method bindEIP.

/**
 * Checks if an EIP is bound and optionally binds the EIP.
 *
 * The list of EIPs are arranged with the EIPs allocated in the zone first
 * followed by other EIPs.
 *
 * If an EIP is already bound to this instance this method simply returns. Otherwise, this method tries to find
 * an unused EIP based on information from AWS. If it cannot find any unused EIP this method, it will be retried
 * for a specified interval.
 *
 * One of the following scenarios can happen here :
 *
 *  1) If the instance is already bound to an EIP as deemed by AWS, no action is taken.
 *  2) If an EIP is already bound to another instance as deemed by AWS, that EIP is skipped.
 *  3) If an EIP is not already bound to an instance and if this instance is not bound to an EIP, then
 *     the EIP is bound to this instance.
 */
public void bindEIP() {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myInstanceId = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.instanceId);
    String myZone = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.availabilityZone);
    Collection<String> candidateEIPs = getCandidateEIPs(myInstanceId, myZone);
    AmazonEC2 ec2Service = getEC2Service();
    boolean isMyinstanceAssociatedWithEIP = false;
    Address selectedEIP = null;
    for (String eipEntry : candidateEIPs) {
        try {
            String associatedInstanceId;
            // Check with AWS, if this EIP is already been used by another instance
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest().withPublicIps(eipEntry);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                associatedInstanceId = eipAddress.getInstanceId();
                // already marked.
                if (((associatedInstanceId == null) || (associatedInstanceId.isEmpty()))) {
                    if (selectedEIP == null) {
                        selectedEIP = eipAddress;
                    }
                } else if (isMyinstanceAssociatedWithEIP = (associatedInstanceId.equals(myInstanceId))) {
                    // This EIP is associated with an instance, check if this is the same as the current instance.
                    // If it is the same, stop searching for an EIP as this instance is already associated with an
                    // EIP
                    selectedEIP = eipAddress;
                    break;
                } else {
                    // The EIP is used by some other instance, hence skip it
                    logger.warn("The selected EIP {} is associated with another instance {} according to AWS," + " hence skipping this", eipEntry, associatedInstanceId);
                }
            }
        } catch (Throwable t) {
            logger.error("Failed to bind elastic IP: {} to {}", eipEntry, myInstanceId, t);
        }
    }
    if (null != selectedEIP) {
        String publicIp = selectedEIP.getPublicIp();
        // Only bind if the EIP is not already associated
        if (!isMyinstanceAssociatedWithEIP) {
            AssociateAddressRequest associateAddressRequest = new AssociateAddressRequest().withInstanceId(myInstanceId);
            String domain = selectedEIP.getDomain();
            if ("vpc".equals(domain)) {
                associateAddressRequest.setAllocationId(selectedEIP.getAllocationId());
            } else {
                associateAddressRequest.setPublicIp(publicIp);
            }
            ec2Service.associateAddress(associateAddressRequest);
            logger.info("\n\n\nAssociated {} running in zone: {} to elastic IP: {}", myInstanceId, myZone, publicIp);
        }
        logger.info("My instance {} seems to be already associated with the EIP {}", myInstanceId, publicIp);
    } else {
        logger.info("No EIP is free to be associated with this instance. Candidate EIPs are: {}", candidateEIPs);
    }
}
Also used : Address(com.amazonaws.services.ec2.model.Address) DescribeAddressesRequest(com.amazonaws.services.ec2.model.DescribeAddressesRequest) DescribeAddressesResult(com.amazonaws.services.ec2.model.DescribeAddressesResult) AssociateAddressRequest(com.amazonaws.services.ec2.model.AssociateAddressRequest) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) AmazonInfo(com.netflix.appinfo.AmazonInfo) InstanceInfo(com.netflix.appinfo.InstanceInfo)

Aggregations

AssociateAddressRequest (com.amazonaws.services.ec2.model.AssociateAddressRequest)4 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)2 Address (com.amazonaws.services.ec2.model.Address)2 AllocateAddressRequest (com.amazonaws.services.ec2.model.AllocateAddressRequest)1 AllocateAddressResult (com.amazonaws.services.ec2.model.AllocateAddressResult)1 AssociateAddressResult (com.amazonaws.services.ec2.model.AssociateAddressResult)1 DescribeAddressesRequest (com.amazonaws.services.ec2.model.DescribeAddressesRequest)1 DescribeAddressesResult (com.amazonaws.services.ec2.model.DescribeAddressesResult)1 AmazonInfo (com.netflix.appinfo.AmazonInfo)1 InstanceInfo (com.netflix.appinfo.InstanceInfo)1 AllocateAddressRequest (software.amazon.awssdk.services.ec2.model.AllocateAddressRequest)1 AllocateAddressResponse (software.amazon.awssdk.services.ec2.model.AllocateAddressResponse)1 AssociateAddressRequest (software.amazon.awssdk.services.ec2.model.AssociateAddressRequest)1 AssociateAddressResponse (software.amazon.awssdk.services.ec2.model.AssociateAddressResponse)1 Ec2Exception (software.amazon.awssdk.services.ec2.model.Ec2Exception)1