/**
  * Constructs a new CreateLoadBalancerRequest object. Callers should use the setter or fluent
  * setter (with...) methods to initialize any additional object members.
  *
  * @param loadBalancerName The name associated with the load balancer. The name must be unique
  *     within your set of load balancers.
  * @param listeners A list of the following tuples: LoadBalancerPort, InstancePort, and Protocol.
  * @param availabilityZones A list of Availability Zones.
  *     <p>At least one Availability Zone must be specified. Specified Availability Zones must be
  *     in the same EC2 Region as the load balancer. Traffic will be equally distributed across all
  *     zones.
  *     <p>You can later add more Availability Zones after the creation of the load balancer by
  *     calling <a>EnableAvailabilityZonesForLoadBalancer</a> action.
  */
 public CreateLoadBalancerRequest(
     String loadBalancerName,
     java.util.List<Listener> listeners,
     java.util.List<String> availabilityZones) {
   setLoadBalancerName(loadBalancerName);
   setListeners(listeners);
   setAvailabilityZones(availabilityZones);
 }
  public static String setUpElbWithLoadGenerator(String loadGenerator) throws Exception {

    String loadBalancerName = "projecttwoelb";

    // Create the ELB client.
    elbClient = new AmazonElasticLoadBalancingClient(awsCredentials());
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    elbClient.setRegion(usEast1);

    CreateLoadBalancerRequest elbRequest = new CreateLoadBalancerRequest();
    elbRequest.setLoadBalancerName(loadBalancerName);

    // Security Group

    DescribeSecurityGroupsRequest sgRequest = new DescribeSecurityGroupsRequest();
    ArrayList<String> list = new ArrayList<String>();
    list.add("SecurityGroupForELBAndAutoscale");
    sgRequest.setGroupNames(list);

    DescribeSecurityGroupsResult result = ec2.describeSecurityGroups(sgRequest);
    List<SecurityGroup> groups = result.getSecurityGroups();

    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add(groups.get(0).getGroupId());
    elbRequest.setSecurityGroups(securityGroups);

    // Adding tags
    Collection<com.amazonaws.services.elasticloadbalancing.model.Tag> tags =
        new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Tag>();

    com.amazonaws.services.elasticloadbalancing.model.Tag tag =
        new com.amazonaws.services.elasticloadbalancing.model.Tag();
    tag.setKey("Project");
    tag.setValue("2.1");
    tags.add(tag);

    AddTagsRequest tagReq = new AddTagsRequest();
    tagReq.withTags(tags);
    elbRequest.setTags(tags);

    // Specify ports
    List<Listener> listener = new ArrayList<Listener>(1);
    listener.add(new Listener("HTTP", 80, 80));
    elbRequest.setListeners(listener);

    // Speciy availability zones
    List<String> zones = new ArrayList<String>(1);
    zones.add("us-east-1c");
    elbRequest.setAvailabilityZones(zones);

    CreateLoadBalancerResult lbResult = elbClient.createLoadBalancer(elbRequest);

    // Disable Cross Zone
    ModifyLoadBalancerAttributesRequest attribRequest = new ModifyLoadBalancerAttributesRequest();
    LoadBalancerAttributes attribs = new LoadBalancerAttributes();
    CrossZoneLoadBalancing crossZone = new CrossZoneLoadBalancing();
    crossZone.setEnabled(false);
    attribs.setCrossZoneLoadBalancing(crossZone);
    attribRequest.setLoadBalancerAttributes(attribs);
    attribRequest.setLoadBalancerName(loadBalancerName);
    elbClient.modifyLoadBalancerAttributes(attribRequest);

    // Specify Health Check
    String target = "HTTP:80/heartbeat?lg=" + URLEncoder.encode(loadGenerator, "UTF-8");
    ConfigureHealthCheckRequest healthCheckRequest = new ConfigureHealthCheckRequest();
    HealthCheck healthCheck = new HealthCheck(target, 5, 2, 2, 10);
    healthCheckRequest.setHealthCheck(healthCheck);
    healthCheckRequest.setLoadBalancerName(loadBalancerName);
    elbClient.configureHealthCheck(healthCheckRequest);

    System.out.println("Load balancer created with name: " + lbResult.getDNSName());

    return lbResult.getDNSName();
  }
 /**
  * Constructs a new CreateLoadBalancerRequest object. Callers should use the setter or fluent
  * setter (with...) methods to initialize any additional object members.
  *
  * @param loadBalancerName The name associated with the load balancer. The name must be unique
  *     within your set of load balancers.
  */
 public CreateLoadBalancerRequest(String loadBalancerName) {
   setLoadBalancerName(loadBalancerName);
 }