Search code examples
javaamazon-web-servicesload-balancingamazon-elb

how can i create a load balancer in AWS using the AWS java sdk


as part of a project i need to create a new load balancer on amazon AWS. i need to do it using the java sdk

how can i set the load balancer up using code?

here is what i tried so far without success.

//create load balancer
        CreateLoadBalancerRequest lbRequest = new CreateLoadBalancerRequest();
        lbRequest.setLoadBalancerName("loader");
        List<Listener> listeners = new ArrayList<Listener>(1);
        listeners.add(new Listener("HTTP", 80, 80));
        lbRequest.withAvailabilityZones(availabilityZone1,availabilityZone2);
        lbRequest.setListeners(listeners);

        CreateLoadBalancerResult lbResult=new CreateLoadBalancerResult();
        System.out.println("created load balancer loader");

Solution

  • well finelly i got it:

    first create an elb client

    AmazonElasticLoadBalancingClient elb = new AmazonElasticLoadBalancingClient(credentials);
    

    then

    use it to initiate the request

      //create load balancer
            CreateLoadBalancerRequest lbRequest = new CreateLoadBalancerRequest();
            lbRequest.setLoadBalancerName("loader");
            List<Listener> listeners = new ArrayList<Listener>(1);
            listeners.add(new Listener("HTTP", 80, 80));
            lbRequest.withAvailabilityZones(availabilityZone1,availabilityZone2);
            lbRequest.setListeners(listeners);
    
            CreateLoadBalancerResult lbResult=elb.createLoadBalancer(lbRequest);
            System.out.println("created load balancer loader");
    

    then register your instances to the Load balancer

    //get the running instances
            DescribeInstancesResult describeInstancesRequest = ec2.describeInstances();
            List<Reservation> reservations = describeInstancesRequest.getReservations();
            List<Instance> instances = new ArrayList<Instance>();
    
            for (Reservation reservation : reservations) {
                instances.addAll(reservation.getInstances());
            }
    
    
            //get instance id's
            String id;
            List instanceId=new ArrayList();
            List instanceIdString=new ArrayList();
            Iterator<Instance> iterator=instances.iterator();
            while (iterator.hasNext())
            {
                id=iterator.next().getInstanceId();
                instanceId.add(new com.amazonaws.services.elasticloadbalancing.model.Instance(id));
                instanceIdString.add(id);
            }
    
    
            //register the instances to the balancer
            RegisterInstancesWithLoadBalancerRequest register =new RegisterInstancesWithLoadBalancerRequest();
            register.setLoadBalancerName("loader");
            register.setInstances((Collection)instanceId);
            RegisterInstancesWithLoadBalancerResult registerWithLoadBalancerResult= elb.registerInstancesWithLoadBalancer(register);
    

    and you are done...