String localInstanceId = System.getenv("instance_id");
// Associate this address with this instance only if any other instance has not assigned it to itself
for (Address address : addressList) {
if (address.getInstance() == null) {
ec2Client.associateAddress(localInstanceId, elasticIP);
} else if (!address.getInstance().getInstanceId().equals(localInstanceId) &&
!(address.getInstance().getCurrentState().equals(InstanceState.RUNNING) || address.getInstance().getCurrentState().equals(InstanceState.PENDING))) {
ec2Client.associateAddress(localInstanceId, elasticIP);
}
}