private DeferredResult<AWSLoadBalancerContext> createLoadBalancer( AWSLoadBalancerContext context) { CreateLoadBalancerRequest request = buildCreationRequest(context); String message = "Create a new AWS Load Balancer with name [" + context.loadBalancerStateExpanded.name + "]."; AWSDeferredResultAsyncHandler<CreateLoadBalancerRequest, CreateLoadBalancerResult> handler = new AWSDeferredResultAsyncHandler<>(this, message); context.client.createLoadBalancerAsync(request, handler); return handler.toDeferredResult().thenApply(result -> { context.loadBalancerStateExpanded.address = result.getDNSName(); return context; }); }
private CreateLoadBalancerRequest buildCreationRequest(AWSLoadBalancerContext context) { // Combine all security groups associated with the LB to a single list Collection<SecurityGroupState> securityGroupsToUse = new ArrayList<>(); if (context.provisionedSecurityGroupState != null) { securityGroupsToUse.add(context.provisionedSecurityGroupState); } if (context.securityGroupStates != null && !context.securityGroupStates.isEmpty()) { securityGroupsToUse.addAll(context.securityGroupStates); } CreateLoadBalancerRequest request = new CreateLoadBalancerRequest() .withLoadBalancerName(context.loadBalancerStateExpanded.name) .withListeners(buildListeners(context)) .withSubnets(context.loadBalancerStateExpanded.subnets.stream() .map(subnet -> subnet.id) .collect(Collectors.toList()) ) .withSecurityGroups( securityGroupsToUse.stream().map(sg -> sg.id).collect(Collectors.toList())); // Set scheme to Internet-facing if specified. By default, an internal load balancer is // created if (!Boolean.TRUE.equals(context.loadBalancerStateExpanded.internetFacing)) { request.setScheme("internal"); } return request; }
public static String provisionAWSLoadBalancerWithEC2Client(VerificationHost host, AmazonElasticLoadBalancingAsyncClient client, String name, String subnetId, String securityGroupId, List<String> instanceIds) { if (name == null) { name = LOAD_BALANCER_NAME_PREFIX + System.currentTimeMillis(); } Listener defaultListener = new Listener().withInstancePort(80) .withInstanceProtocol("HTTP") .withLoadBalancerPort(80) .withProtocol("HTTP"); CreateLoadBalancerRequest request = new CreateLoadBalancerRequest() .withLoadBalancerName(name) .withSubnets(Collections.singletonList(subnetId)) .withSecurityGroups(Collections.singleton(securityGroupId)) .withListeners(Collections.singleton(defaultListener)); CreateLoadBalancerResult result = null; try { result = client.createLoadBalancer(request); } catch (Exception e) { host.log(Level.SEVERE, "Error encountered in provisioning load balancer on AWS: %s", Utils.toString(e)); } assertNotNull(result); assertNotNull(result.getDNSName()); if (instanceIds != null) { registerAWSInstancesToLoadBalancer(host, client, name, instanceIds); } return name; }
public static String createNewLoadBalancer(String name, Listener... listeners) { if (name == null || name.trim().length() == 0) throw new RuntimeException( "The name of the load balancer cannot be empty!"); connect(); CreateLoadBalancerRequest req = new CreateLoadBalancerRequest(); req.setLoadBalancerName(name); if (listeners != null && listeners.length > 0) { ArrayList<com.amazonaws.services.elasticloadbalancing.model.Listener> actualListeners = new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Listener>(); for (Listener listener : listeners) actualListeners .add(new com.amazonaws.services.elasticloadbalancing.model.Listener( listener.protocol, listener.port, listener.port)); req.setListeners(actualListeners); } ArrayList<String> securityGroups = new ArrayList<String>(); securityGroups.add(AmazonEC2.getSecurityGroupId()); req.setSecurityGroups(securityGroups); req.setAvailabilityZones(AmazonEC2.getAllAvailabilityZones()); CreateLoadBalancerResult res = client.createLoadBalancer(req); return res.getDNSName(); }
public LoadBalancerDescription createELB(final CreateLoadBalancerRequest request) throws Exception { new Runner<>() .retryInterval(Duration.ofSeconds(20)) .maxAttempts(5) .retryOn(e -> e instanceof AmazonServiceException) .run(() -> { logger.info("create elb, request={}", request); elb.createLoadBalancer(request); return null; }); return describeELB(request.getLoadBalancerName()); }
@Override public void execute(Context context) throws Exception { CreateLoadBalancerRequest request = new CreateLoadBalancerRequest() .withLoadBalancerName(resource.name) .withScheme(resource.scheme.orElse(null)) .withTags(new Tag().withKey("cloud-manager:env").withValue(context.env.name)); if (resource.subnet != null) { request.withSecurityGroups(resource.securityGroup.remoteSecurityGroup.getGroupId()) .withSubnets(resource.subnet.remoteSubnets.stream().map(Subnet::getSubnetId).collect(Collectors.toList())); } else { List<String> zones = AWS.ec2.availabilityZones(); request.withAvailabilityZones(zones.get(0)); } if (resource.listenHTTP) { request.getListeners().add(new Listener("HTTP", 80, 80)); } if (resource.listenHTTPS) { String certARN = resource.amazonCertARN != null ? resource.amazonCertARN : resource.cert.remoteCert.getServerCertificateMetadata().getArn(); request.getListeners().add(new Listener() .withProtocol("HTTPS") .withLoadBalancerPort(443) .withInstanceProtocol("HTTP") .withInstancePort(80) .withSSLCertificateId(certARN)); } resource.remoteELB = AWS.elb.createELB(request); configureELB(context.env.region); configureHealthCheck(); context.output(String.format("elb/%s/DNS", resource.id), resource.remoteELB.getDNSName()); }