# Buggy code
def deploy_template(template):
    if isinstance(template, string_types):
        template = parse_template(template)

    for key, resource in iteritems(template["Resources"]):
        deploy_resource(resource)


# Traceback (ResponseParserError)
ERROR:localstack.mock.generic_proxy:Error forwarding request: Traceback (most recent call last):
File "/opt/code/localstack/localstack/mock/generic_proxy.py", line 88, in forward
data=data, headers=self.headers, return_forward_info=True)
File "/opt/code/localstack/localstack/mock/proxy/sns_listener.py", line 37, in update_sns
queue_url = aws_stack.get_sqs_queue_url(queue_name)
File "/opt/code/localstack/localstack/utils/aws/aws_stack.py", line 285, in get_sqs_queue_url
response = client.get_queue_url(QueueName=queue_name)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/client.py", line 253, in _api_call
return self._make_api_call(operation_name, kwargs)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/client.py", line 531, in _make_api_call
operation_model, request_dict)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 141, in make_request
return self._send_request(request_dict, operation_model)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 168, in _send_request
request, operation_model, attempts)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/endpoint.py", line 233, in _get_response
response_dict, operation_model.output_shape)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 210, in parse
parsed = self._do_error_parse(response, shape)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 438, in _do_error_parse
root = self._parse_xml_string_to_dom(xml_contents)
File "/opt/code/localstack/.venv/lib/python2.7/site-packages/botocore/parsers.py", line 390, in _parse_xml_string_to_dom
"invalid XML received:\n%s" % (e, xml_string))
ResponseParserError: Unable to parse response (syntax error: line 1, column 54), invalid XML received:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>500 Internal Server Error</title>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

# Fixed code
def deploy_template(template, stack_name):
    if isinstance(template, string_types):
        template = parse_template(template)

    if MARKER_DONT_REDEPLOY_STACK in template:
        # If we are currently deploying, then bail. This can occur if
        # deploy_template(..) method calls boto's update_stack(..) (to update the
        # state of resources) which itself triggers another call to deploy_template(..).
        # We don't want to end up in an infinite/recursive deployment loop.
        return

    resource_map = template.get("Resources")
    if not resource_map:
        LOGGER.warning("CloudFormation template contains no Resources section")
        return

    next = resource_map

    # resource_list = resource_map.values()
    iters = 3
    for i in range(0, iters):
        # print('deployment iteration', i)
        # get resource details
        for resource_id, resource in iteritems(next):
            resource["__details__"] = describe_stack_resources(stack_name, resource_id)[
                0
            ]

        next = resources_to_deploy_next(resource_map, stack_name)
        if not next:
            return

        for resource_id, resource in iteritems(next):
            deploy_resource(resource_id, resource_map, stack_name=stack_name)

    LOGGER.warning(
        "Unable to resolve all dependencies and deploy all resources "
        + "after %s iterations. Remaining (%s): %s" % (iters, len(next), next)
    )