# Buggy code
def initialize_resources(self):
    def set_status(status):
        self._add_stack_event(status)
        self.status = status

    self.resource_map.create()
    self.output_map.create()

    def run_loop(*args):
        # NOTE: We're adding this additional loop, as it seems that in some cases moto
        #   does not consider resource dependencies (e.g., if a "DependsOn" resource property
        #   is defined). This loop allows us to incrementally resolve such dependencies.
        resource_map = self.resource_map
        for i in range(MAX_DEPENDENCY_DEPTH):
            unresolved = getattr(resource_map, "_unresolved_resources", {})
            if not unresolved:
                set_status("CREATE_COMPLETE")
                return resource_map
            resource_map._unresolved_resources = {}
            for resource_id, resource_details in unresolved.items():
                # Re-trigger the resource creation
                parse_and_create_resource(*resource_details, force_create=True)
            if unresolved.keys() == resource_map._unresolved_resources.keys():
                # looks like no more resources can be resolved -> bail
                LOG.warning(
                    "Unresolvable dependencies, there may be undeployed stack resources: %s"
                    % unresolved
                )
                break
        set_status("CREATE_FAILED")
        raise Exception(
            "Unable to resolve all CloudFormation resources after traversing "
            + "dependency tree (maximum depth %s reached)" % MAX_DEPENDENCY_DEPTH
        )

    # NOTE: We're running the loop in the background, as it might take some time to complete
    FuncThread(run_loop).start()


# Traceback (TypeError)
2020-02-05T17:05:30:ERROR:flask.app: Exception on / [POST]
Traceback (most recent call last):
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask_cors/extension.py", line 161, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/code/localstack/.venv/lib/python3.7/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/code/localstack/localstack/services/firehose/firehose_api.py", line 276, in post_request
es_update=es_update, version_id=version_id)
TypeError: update_destination() got an unexpected keyword argument 'es_update'

# Fixed code
def initialize_resources(self):
    def set_status(status):
        self._add_stack_event(status)
        self.status = status

    self.resource_map.create()
    self.output_map.create()

    def run_loop(*args):
        # NOTE: We're adding this additional loop, as it seems that in some cases moto
        #   does not consider resource dependencies (e.g., if a "DependsOn" resource property
        #   is defined). This loop allows us to incrementally resolve such dependencies.
        resource_map = self.resource_map
        unresolved = {}
        for i in range(MAX_DEPENDENCY_DEPTH):
            unresolved = getattr(resource_map, "_unresolved_resources", {})
            if not unresolved:
                set_status("CREATE_COMPLETE")
                return resource_map
            resource_map._unresolved_resources = {}
            for resource_id, resource_details in unresolved.items():
                # Re-trigger the resource creation
                parse_and_create_resource(*resource_details, force_create=True)
            if unresolved.keys() == resource_map._unresolved_resources.keys():
                # looks like no more resources can be resolved -> bail
                LOG.warning(
                    "Unresolvable dependencies, there may be undeployed stack resources: %s"
                    % unresolved
                )
                break
        set_status("CREATE_FAILED")
        raise Exception(
            "Unable to resolve all CloudFormation resources after traversing "
            + "dependency tree (maximum depth %s reached): %s"
            % (MAX_DEPENDENCY_DEPTH, unresolved.keys())
        )

    # NOTE: We're running the loop in the background, as it might take some time to complete
    FuncThread(run_loop).start()