Example usage for com.amazonaws.services.ec2 AmazonEC2 disassociateAddress

List of usage examples for com.amazonaws.services.ec2 AmazonEC2 disassociateAddress

Introduction

In this page you can find the example usage for com.amazonaws.services.ec2 AmazonEC2 disassociateAddress.

Prototype

DisassociateAddressResult disassociateAddress(DisassociateAddressRequest disassociateAddressRequest);

Source Link

Document

Disassociates an Elastic IP address from the instance or network interface it's associated with.

Usage

From source file:com.netflix.eureka.aws.EIPManager.java

License:Apache License

/**
 * Unbind the EIP that this instance is associated with.
 */// www. j a  v a 2s.c  o m
public void unbindEIP() throws Exception {
    InstanceInfo myInfo = applicationInfoManager.getInfo();
    String myPublicIP = null;
    if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
        myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
        if (myPublicIP == null) {
            logger.info("Instance is not associated with an EIP. Will not try to unbind");
            return;
        }

        try {
            AmazonEC2 ec2Service = getEC2Service();
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest()
                    .withPublicIps(myPublicIP);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
                String domain = eipAddress.getDomain();
                if ("vpc".equals(domain)) {
                    dissociateRequest.setAssociationId(eipAddress.getAssociationId());
                } else {
                    dissociateRequest.setPublicIp(eipAddress.getPublicIp());
                }

                ec2Service.disassociateAddress(dissociateRequest);
                logger.info("Dissociated the EIP {} from this instance", myPublicIP);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Cannot dissociate address from this instance", e);
        }
    }

}

From source file:com.netflix.eureka.util.EIPManager.java

License:Apache License

/**
 * Unbind the EIP that this instance is associated with.
 *//*  ww  w  .j  a v  a  2 s.  c o m*/
public void unbindEIP() {
    InstanceInfo myInfo = ApplicationInfoManager.getInstance().getInfo();
    String myPublicIP = null;
    if (myInfo != null && myInfo.getDataCenterInfo().getName() == Name.Amazon) {
        myPublicIP = ((AmazonInfo) myInfo.getDataCenterInfo()).get(MetaDataKey.publicIpv4);
        try {
            AmazonEC2 ec2Service = getEC2Service();
            DescribeAddressesRequest describeAddressRequest = new DescribeAddressesRequest()
                    .withPublicIps(myPublicIP);
            DescribeAddressesResult result = ec2Service.describeAddresses(describeAddressRequest);
            if ((result.getAddresses() != null) && (!result.getAddresses().isEmpty())) {
                Address eipAddress = result.getAddresses().get(0);
                DisassociateAddressRequest dissociateRequest = new DisassociateAddressRequest();
                String domain = eipAddress.getDomain();
                if ("vpc".equals(domain)) {
                    dissociateRequest.setAssociationId(eipAddress.getAssociationId());
                } else {
                    dissociateRequest.setPublicIp(eipAddress.getPublicIp());
                }

                ec2Service.disassociateAddress(dissociateRequest);
                logger.info("Dissociated the EIP {} from this instance", myPublicIP);
            }
        } catch (Throwable e) {
            throw new RuntimeException("Cannot dissociate address" + myPublicIP + "from this instance", e);
        }
    }

}

From source file:com.urbancode.terraform.tasks.aws.helpers.AWSHelper.java

License:Apache License

/**
 * removes the association of the elasticIp from the instance. This is done automatically by
 * amazon when an instance is terminated, there is a few seconds delay.
 *
 * @param associationId//w w  w . java2 s  .  co  m
 * @param ec2Client
 */
public void disassociateElasticIp(String associationId, AmazonEC2 ec2Client) {
    DisassociateAddressRequest request = new DisassociateAddressRequest().withAssociationId(associationId);
    ec2Client.disassociateAddress(request);
}

From source file:fr.xebia.workshop.continuousdelivery.CreateNexusInstance.java

License:Apache License

@Override
public void execute(AmazonEC2 ec2, WorkshopInfrastructure workshopInfrastructure) throws Exception {
    logger.info("STARTING CREATE NEXUS SERVER");

    // TERMINATE EXISTING NEXUS SERVERS IF EXIST
    AmazonAwsUtils.terminateInstancesByRole(TeamInfrastructure.ROLE_NEXUS, ec2);

    // CREATE NEXUS INSTANCE
    String cloudConfigFilePath = "fr/xebia/workshop/continuousdelivery/cloud-config-amzn-linux-nexus.txt";
    String userData = CloudInitUserDataBuilder.start().addCloudConfigFromFilePath(cloudConfigFilePath)
            .buildBase64UserData();/*  w  ww  .j  av a 2s  .  c  o m*/

    RunInstancesRequest runInstancesRequest = new RunInstancesRequest() //
            .withInstanceType(InstanceType.T1Micro.toString()) //
            .withImageId(AmazonAwsUtils.AMI_AMZN_LINUX_EU_WEST) //
            .withMinCount(1) //
            .withMaxCount(1) //
            .withSecurityGroupIds("accept-all") //
            .withKeyName(KEY_PAIR_NAME) //
            .withUserData(userData);

    // START NEXUS INSTANCE
    List<Instance> nexusInstances = AmazonAwsUtils.reliableEc2RunInstances(runInstancesRequest, ec2);
    Instance nexusInstance = Iterables.getOnlyElement(nexusInstances);

    // TAG NEXUS INSTANCES
    CreateTagsRequest createTagsRequest = new CreateTagsRequest();
    createTagsRequest.withResources(nexusInstance.getInstanceId()) //
            .withTags(//
                    new Tag("Name", "nexus"), //
                    new Tag("Workshop", "continuous-delivery-workshop"), //
                    new Tag("Role", TeamInfrastructure.ROLE_NEXUS));
    ec2.createTags(createTagsRequest);

    // first waits for Nexus availability, otherwise the following elastic IP assignment will break its installation
    waitForNexusAvailability(nexusInstance);

    final String publicIp = workshopInfrastructure.getNexusPublicIp();

    // ASSOCIATE NEXUS INSTANCE WITH PUBLIC IP
    Address address = Iterables.getOnlyElement(
            ec2.describeAddresses(new DescribeAddressesRequest().withPublicIps(publicIp)).getAddresses());
    String currentlyAssociatedId = address.getInstanceId();
    if (currentlyAssociatedId == null) {
        logger.debug("Public IP {} is not currently associated with an instance", publicIp);
    } else {
        logger.info("Public IP {} is currently associated instance '{}'. Disassociate it first.", publicIp,
                currentlyAssociatedId);
        ec2.disassociateAddress(new DisassociateAddressRequest(publicIp));
    }

    ec2.associateAddress(new AssociateAddressRequest(nexusInstance.getInstanceId(), publicIp));

    try {
        AmazonAwsUtils.awaitForHttpAvailability(workshopInfrastructure.getNexusUrlWithIp());
        AmazonAwsUtils.awaitForHttpAvailability(workshopInfrastructure.getNexusUrlWithDomainName());
    } catch (Exception e) {
        logger.warn("Silently skipped " + e, e);
    }

    logger.info("1 NEXUS SERVER {} SUCCESSFULLY CREATED AND ASSOCIATED WITH {}: {}",
            new Object[] { nexusInstance.getInstanceId(), publicIp, nexusInstance });
}