def terminate(self, delete_logs):
        try:
            load_balancer_handle = self._get_load_balancer_handle()
            conn = boto.ec2.elb.connect_to_region(self.region, **self.aws_credentials)
            _logger.info("Deleting load balancer: %s" % load_balancer_handle.name)
            conn.delete_load_balancer(load_balancer_handle.name)
        except:
            _logger.error(
                "Could not delete load balancer. Please manually delete the following load "
                "balancer: %s" % self.load_balancer_dns_name
            )

        # Terminate all hosts
        try:
            host_ids = [i.id for i in load_balancer_handle.instances]
            _stop_instances(host_ids, self.region, credentials=self.aws_credentials)
        except:
            _logger.error("Could not terminate hosts. Please manually terminate from the AWS console.")

        if delete_logs:
            _logger.info("Deleting log files.")
            try:
                s3_recursive_delete(self.log_path, self.aws_credentials)
            except:
                _logger.info("Could not delete log file. Please manually delete files under: %s" % self.log_path)

        # Delete the server certificate
        if self.certificate_name:
            conn = boto.iam.connection.IAMConnection(**self.aws_credentials)
            conn.delete_server_cert(self.certificate_name)
    def launch(name, ec2_config, s3_predictive_object_path, admin_key, ssl_credentials, aws_credentials, started):

        s3_log_path = "s3://%s/%s%s_logs" % (
            ec2_config.s3_bucket,
            ec2_config.s3_log_folder_path,
            name,
        )  # s3_log_folder_path always ends in '/'
        user_data = {
            "log_path": s3_log_path,
            "aws_access_key": aws_credentials["aws_access_key_id"],
            "aws_secret_key": aws_credentials["aws_secret_access_key"],
            "s3_predictive_service": s3_predictive_object_path,
        }

        _logger.info("[Step 1/5]: Launching EC2 instances.")

        # Start the hosts up.
        ec2_hosts = _ec2_factory(
            ec2_config.instance_type,
            region=ec2_config.region,
            CIDR_rule=ec2_config.CIDR_rule,
            security_group_name=ec2_config.security_group,
            tags=ec2_config.tags,
            user_data=user_data,
            credentials=aws_credentials,
            num_hosts=ec2_config.num_hosts,
            ami_service_parameters={"service": "predictive"},
        )

        lb = None
        try:
            # Determine host ids and availability zones used.
            zones, host_ids, is_vpc = set(), [], False
            if ec2_config.num_hosts == 1:
                host_ids = [ec2_hosts.instance_id]
                zones.add(ec2_hosts.instance.placement)
                is_vpc = bool(ec2_hosts.instance.vpc_id)
            else:
                for i in ec2_hosts:
                    zones.add(i.instance.placement)
                    host_ids.append(i.instance_id)

                is_vpc = bool(ec2_hosts[0].instance.vpc_id)

            certificate_name, certificate_is_self_signed = None, None
            if ssl_credentials:
                # Using HTTPS
                (private_key_path, public_certificate_path, certificate_is_self_signed) = ssl_credentials
                certificate_name = name
                certificate_id = Ec2PredictiveServiceEnvironment._upload_ssl_info(
                    certificate_name, private_key_path, public_certificate_path, aws_credentials
                )
                listener_tuple = (443, Ec2PredictiveServiceEnvironment._PORT_NUM, "https", certificate_id)
            else:
                # Using HTTP
                listener_tuple = (80, Ec2PredictiveServiceEnvironment._PORT_NUM, "http")

            conn = boto.ec2.elb.connect_to_region(ec2_config.region, **aws_credentials)

            # Create the load balancer.
            _logger.info("[Step 2/5]: Launching Load Balancer.")

            while (datetime.now() - started).total_seconds() < MAX_CREATE_TIMEOUT_SECS:
                try:
                    lb = conn.create_load_balancer(name, zones, [listener_tuple])
                    break
                except BotoServerError as e:
                    # We just uploaded the certificate, so there's a good chance it will not be found, yet.
                    if "<Code>CertificateNotFound</Code>" not in str(e):
                        raise e
                    sleep(1)
            else:
                raise RuntimeError(
                    "Unable to successfully create load balancer. Please confirm in AWS Management Console"
                )

            # If we're running in a VPC (i.e. not "EC2-Classic") we need to attach a security group.
            if is_vpc:
                # Look up security group id
                security_group = None
                while (datetime.now() - started).total_seconds() < MAX_CREATE_TIMEOUT_SECS:
                    ec2_conn = boto.ec2.connect_to_region(ec2_config.region, **aws_credentials)
                    for sg in ec2_conn.get_all_security_groups():
                        if GRAPHLAB_NAME == sg.name:
                            security_group = sg
                            break  # found it
                    if security_group:
                        break  # no more eventual consistency
                    else:
                        sleep(1)
                else:
                    raise RuntimeError("Unable to find expected security group needed to configure load balancer.")
                lb.apply_security_groups([sg.id])

            _logger.info("[Step 3/5]: Configuring Load Balancer.")

            # Configure healthCheck
            health_target = "HTTP:%d/control/healthcheck" % PredictiveServiceEnvironment._PORT_NUM
            health_check = boto.ec2.elb.HealthCheck(
                interval=20, healthy_threshold=3, unhealthy_threshold=5, target=health_target
            )
            lb.configure_health_check(health_check)

            # Add EC2 instances to the load balancer.
            lb.register_instances(host_ids)

        except:
            _logger.error("Could not create or configure the load balancer, terminating EC2 instances.")
            _stop_instances([h.instance_id for h in ec2_hosts], ec2_config.region, credentials=aws_credentials)

            if lb:
                _logger.info("Deleting the load balancer.")
                lb.delete()

            raise

        return Ec2PredictiveServiceEnvironment(
            lb.dns_name,
            ec2_config.region,
            s3_log_path,
            admin_key,
            certificate_name,
            certificate_is_self_signed,
            aws_credentials,
        )