'Replace the instances in the group.
Replace the instances in the group using updated launch configuration.'

def _replace(self, min_in_service, batch_size, pause_sec):

    def changing_instances(tmpl):
        instances = grouputils.get_members(self)
        current = set(((i.name, i.t) for i in instances))
        updated = set(tmpl.resource_definitions(None).items())
        affected = set((k for (k, v) in (current ^ updated)))
        return set((i.FnGetRefId() for i in instances if (i.name in affected)))
    def pause_between_batch():
        while True:
            try:
                (yield)
            except scheduler.Timeout:
                return
    capacity = (len(self.nested()) if self.nested() else 0)
    batches = list(self._get_batches(capacity, batch_size, min_in_service))
    update_timeout = self._update_timeout(len(batches), pause_sec)
    try:
        for (index, (total_capacity, efft_bat_sz)) in enumerate(batches):
            template = self._create_template(total_capacity, efft_bat_sz)
            self._lb_reload(exclude=changing_instances(template))
            updater = self.update_with_template(template)
            checker = scheduler.TaskRunner(self._check_for_completion, updater)
            checker(timeout=update_timeout)
            if ((index < (len(batches) - 1)) and (pause_sec > 0)):
                self._lb_reload()
                waiter = scheduler.TaskRunner(pause_between_batch)
                waiter(timeout=pause_sec)
    finally:
        self._lb_reload()
