### Is there an existing issue for this?

- [X] I have searched the existing issues

### Current Behavior

The first time you run samlocal deploy --guided, once starts the deploy it fails with this error inside the docker image.

```localstack_main             | 2022-05-25T14:56:42.418:WARNING:localstack.utils.server.http2_server: Error in proxy handler for request POST http://localhost:4566/: 'AmazonSNSFullAccessLS' Traceback (most recent call last):
localstack_main             |   File "/opt/code/localstack/localstack/aws/forwarder.py", line 57, in _call
localstack_main             |     return handler(context, req)
localstack_main             |   File "/opt/code/localstack/localstack/aws/skeleton.py", line 117, in __call__
localstack_main             |     return self.fn(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/aws/api/core.py", line 95, in operation_marker
localstack_main             |     return fn(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/aws/api/iam/__init__.py", line 2257, in attach_role_policy
localstack_main             |     raise NotImplementedError
localstack_main             | NotImplementedError
localstack_main             | 
localstack_main             | During handling of the above exception, another exception occurred:
localstack_main             | 
localstack_main             | Traceback (most recent call last):
localstack_main             |   File "/opt/code/localstack/localstack/utils/server/http2_server.py", line 185, in index
localstack_main             |     result = await run_sync(handler, request, data)
localstack_main             |   File "/opt/code/localstack/localstack/utils/asyncio.py", line 85, in run_sync
localstack_main             |     return await loop.run_in_executor(thread_pool, copy_context().run, func_wrapped)
localstack_main             |   File "/opt/code/localstack/localstack/utils/threads.py", line 39, in run
localstack_main             |     result = self.func(self.params, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/utils/asyncio.py", line 30, in _run
localstack_main             |     return fn(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 991, in handler
localstack_main             |     return modify_and_forward(
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 533, in wrapper
localstack_main             |     value = func(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 613, in modify_and_forward
localstack_main             |     listener_result = listener.forward_request(
localstack_main             |   File "/opt/code/localstack/localstack/services/edge.py", line 170, in forward_request
localstack_main             |     result = do_forward_request(api, method, path, data, headers, port=port)
localstack_main             |   File "/opt/code/localstack/localstack/services/edge.py", line 225, in do_forward_request
localstack_main             |     result = do_forward_request_inmem(api, method, path, data, headers, port=port)
localstack_main             |   File "/opt/code/localstack/localstack/services/edge.py", line 249, in do_forward_request_inmem
localstack_main             |     response = modify_and_forward(
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 533, in wrapper
localstack_main             |     value = func(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 613, in modify_and_forward
localstack_main             |     listener_result = listener.forward_request(
localstack_main             |   File "/opt/code/localstack/localstack/http/adapters.py", line 51, in forward_request
localstack_main             |     response = self.request(request)
localstack_main             |   File "/opt/code/localstack/localstack/aws/proxy.py", line 42, in request
localstack_main             |     return self.skeleton.invoke(context)
localstack_main             |   File "/opt/code/localstack/localstack/aws/skeleton.py", line 153, in invoke
localstack_main             |     return self.dispatch_request(context, instance)
localstack_main             |   File "/opt/code/localstack/localstack/aws/skeleton.py", line 165, in dispatch_request
localstack_main             |     result = handler(context, instance) or {}
localstack_main             |   File "/opt/code/localstack/localstack/aws/forwarder.py", line 59, in _call
localstack_main             |     return fallthrough_handler(context, req)
localstack_main             |   File "/opt/code/localstack/localstack/services/moto.py", line 108, in proxy_moto
localstack_main             |     status, headers, content = dispatch_to_moto(context)
localstack_main             |   File "/opt/code/localstack/localstack/services/moto.py", line 137, in dispatch_to_moto
localstack_main             |     return dispatch(request, request.url, request.headers)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/core/responses.py", line 205, in dispatch
localstack_main             |     return cls()._dispatch(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/core/responses.py", line 315, in _dispatch
localstack_main             |     return self.call_action()
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/core/responses.py", line 405, in call_action
localstack_main             |     response = method()
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/iam/responses.py", line 10, in attach_role_policy
localstack_main             |     iam_backend.attach_role_policy(policy_arn, role_name)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/iam/models.py", line 1596, in attach_role_policy
localstack_main             |     policy = arns[policy_arn]
localstack_main             | KeyError: 'AmazonSNSFullAccessLS'
localstack_main             | 
localstack_main             | 2022-05-25T14:56:42.418:INFO:localstack.utils.threads: Thread run method <function AdaptiveThreadPool.submit.<locals>._run at 0x7fb972d2ab90>(None) failed: 'AmazonSNSFullAccessLS' Traceback (most recent call last):
localstack_main             |   File "/opt/code/localstack/localstack/aws/forwarder.py", line 57, in _call
localstack_main             |     return handler(context, req)
localstack_main             |   File "/opt/code/localstack/localstack/aws/skeleton.py", line 117, in __call__
localstack_main             |     return self.fn(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/aws/api/core.py", line 95, in operation_marker
localstack_main             |     return fn(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/aws/api/iam/__init__.py", line 2257, in attach_role_policy
localstack_main             |     raise NotImplementedError
localstack_main             | NotImplementedError
localstack_main             | 
localstack_main             | During handling of the above exception, another exception occurred:
localstack_main             | 
localstack_main             | Traceback (most recent call last):
localstack_main             |   File "/opt/code/localstack/localstack/utils/threads.py", line 39, in run
localstack_main             |     result = self.func(self.params, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/utils/asyncio.py", line 30, in _run
localstack_main             |     return fn(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 991, in handler
localstack_main             |     return modify_and_forward(
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 533, in wrapper
localstack_main             |     value = func(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 613, in modify_and_forward
localstack_main             |     listener_result = listener.forward_request(
localstack_main             |   File "/opt/code/localstack/localstack/services/edge.py", line 170, in forward_request
localstack_main             |     result = do_forward_request(api, method, path, data, headers, port=port)
localstack_main             |   File "/opt/code/localstack/localstack/services/edge.py", line 225, in do_forward_request
localstack_main             |     result = do_forward_request_inmem(api, method, path, data, headers, port=port)
localstack_main             |   File "/opt/code/localstack/localstack/services/edge.py", line 249, in do_forward_request_inmem
localstack_main             |     response = modify_and_forward(
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 533, in wrapper
localstack_main             |     value = func(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/localstack/services/generic_proxy.py", line 613, in modify_and_forward
localstack_main             |     listener_result = listener.forward_request(
localstack_main             |   File "/opt/code/localstack/localstack/http/adapters.py", line 51, in forward_request
localstack_main             |     response = self.request(request)
localstack_main             |   File "/opt/code/localstack/localstack/aws/proxy.py", line 42, in request
localstack_main             |     return self.skeleton.invoke(context)
localstack_main             |   File "/opt/code/localstack/localstack/aws/skeleton.py", line 153, in invoke
localstack_main             |     return self.dispatch_request(context, instance)
localstack_main             |   File "/opt/code/localstack/localstack/aws/skeleton.py", line 165, in dispatch_request
localstack_main             |     result = handler(context, instance) or {}
localstack_main             |   File "/opt/code/localstack/localstack/aws/forwarder.py", line 59, in _call
localstack_main             |     return fallthrough_handler(context, req)
localstack_main             |   File "/opt/code/localstack/localstack/services/moto.py", line 108, in proxy_moto
localstack_main             |     status, headers, content = dispatch_to_moto(context)
localstack_main             |   File "/opt/code/localstack/localstack/services/moto.py", line 137, in dispatch_to_moto
localstack_main             |     return dispatch(request, request.url, request.headers)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/core/responses.py", line 205, in dispatch
localstack_main             |     return cls()._dispatch(*args, **kwargs)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/core/responses.py", line 315, in _dispatch
localstack_main             |     return self.call_action()
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/core/responses.py", line 405, in call_action
localstack_main             |     response = method()
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/iam/responses.py", line 10, in attach_role_policy
localstack_main             |     iam_backend.attach_role_policy(policy_arn, role_name)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/moto/iam/models.py", line 1596, in attach_role_policy
localstack_main             |     policy = arns[policy_arn]
localstack_main             | KeyError: 'AmazonSNSFullAccessLS'
localstack_main             | 
localstack_main             | 2022-05-25T14:56:42.418:DEBUG:localstack.utils.threads: FINISHED: <Future at 0x7fb975064af0 state=finished raised KeyError>
localstack_main             | 2022-05-25T14:56:42.420:DEBUG:localstack.utils.cloudformation.template_deployer: Error applying changes for CloudFormation stack "test": Unable to parse response (not well-formed (invalid token): line 1, column 0), invalid XML received. Further retries may succeed:
localstack_main             | b'{}' Traceback (most recent call last):
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/parsers.py", line 502, in _parse_xml_string_to_dom
localstack_main             |     parser.feed(xml_string)
localstack_main             | xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
localstack_main             | 
localstack_main             | During handling of the above exception, another exception occurred:
localstack_main             | 
localstack_main             | Traceback (most recent call last):
localstack_main             |   File "/opt/code/localstack/localstack/utils/cloudformation/template_deployer.py", line 1546, in _run
localstack_main             |     self.do_apply_changes_in_loop(changes, stack, stack_name)
localstack_main             |   File "/opt/code/localstack/localstack/utils/cloudformation/template_deployer.py", line 1616, in do_apply_changes_in_loop
localstack_main             |     self.apply_change(change, stack=stack)
localstack_main             |   File "/opt/code/localstack/localstack/utils/cloudformation/template_deployer.py", line 1682, in apply_change
localstack_main             |     result = deploy_resource(self, resource_id)
localstack_main             |   File "/opt/code/localstack/localstack/utils/cloudformation/template_deployer.py", line 844, in deploy_resource
localstack_main             |     result = execute_resource_action(resource_id, stack, ACTION_CREATE)
localstack_main             |   File "/opt/code/localstack/localstack/utils/cloudformation/template_deployer.py", line 880, in execute_resource_action
localstack_main             |     result = func["function"](resource_id, resources, resource_type, func, stack_name)
localstack_main             |   File "/opt/code/localstack/localstack/services/cloudformation/models/iam.py", line 233, in _post_create
localstack_main             |     iam.attach_role_policy(RoleName=role_name, PolicyArn=arn)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/client.py", line 508, in _api_call
localstack_main             |     return self._make_api_call(operation_name, kwargs)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/client.py", line 894, in _make_api_call
localstack_main             |     http, parsed_response = self._make_request(
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/client.py", line 917, in _make_request
localstack_main             |     return self._endpoint.make_request(operation_model, request_dict)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/endpoint.py", line 116, in make_request
localstack_main             |     return self._send_request(request_dict, operation_model)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/endpoint.py", line 196, in _send_request
localstack_main             |     success_response, exception = self._get_response(
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/endpoint.py", line 238, in _get_response
localstack_main             |     success_response, exception = self._do_get_response(
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/endpoint.py", line 305, in _do_get_response
localstack_main             |     parsed_response = parser.parse(
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/parsers.py", line 249, in parse
localstack_main             |     parsed = self._do_error_parse(response, shape)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/parsers.py", line 556, in _do_error_parse
localstack_main             |     root = self._parse_xml_string_to_dom(xml_contents)
localstack_main             |   File "/opt/code/localstack/.venv/lib/python3.10/site-packages/botocore/parsers.py", line 505, in _parse_xml_string_to_dom
localstack_main             |     raise ResponseParserError(
localstack_main             | botocore.parsers.ResponseParserError: Unable to parse response (not well-formed (invalid token): line 1, column 0), invalid XML received. Further retries may succeed:
localstack_main             | b'{}'
```

### Expected Behavior

Create the stack properly and link the IAM policy properly. If you declare the policy inline inside the sam template it works properly.

### How are you starting LocalStack?

With a docker-compose file

### Steps To Reproduce

#### How are you starting localstack (e.g., `bin/localstack` command, arguments, or `docker-compose.yml`)

docker-compose.yml

```
version: "3.8"

services:
  localstack:
    container_name: "${LOCALSTACK_DOCKER_NAME-localstack_main}"
    image: localstack/localstack:0.14.3
    network_mode: bridge
    ports:
      - "127.0.0.1:53:53"                # only required for Pro (DNS)
      - "127.0.0.1:53:53/udp"            # only required for Pro (DNS)
      - "127.0.0.1:443:443"              # only required for Pro (LocalStack HTTPS Edge Proxy)
      - "127.0.0.1:4510-4559:4510-4559"  # external service port range
      - "127.0.0.1:4566:4566"            # LocalStack Edge Proxy
    environment:
      - SERVICES=dynamodb,cloudformation,lambda,s3,sts,apigateway,iam
      - DEBUG=1
      - HOST_TMP_FOLDER=${TMPDIR:-/tmp/}localstack
      - DOCKER_HOST=unix:///var/run/docker.sock
    volumes:
      - "${TMPDIR:-/tmp}/localstack:/tmp/localstack"
      - "/var/run/docker.sock:/var/run/docker.sock"
    networks:
      net1:
        ipv4_address: 10.10.100.3

networks:
  net1:
    driver: bridge
    enable_ipv6: false
    ipam:
      config:
      - subnet: 10.10.100.0/24
        gateway: 10.10.100.32
```

#### Client commands (e.g., AWS SDK code snippet, or sequence of "awslocal" commands)

template.yml

```AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  events-generator-sam
  
  SAM Template test

Parameters:
  Environment:
    Type: String
    Default: sandbox
  Project:
    Type: String
    Default: test
  Component:
    Type: String
    Default: test
  ApiGatewayIdV1:
    Type: String
    Default: abc1234
  ApiGatewayIdV2:
    Type: String
    Default: wxyz789
  DynamoDBUrl:
    Type: String
    Default: LOCALSTACK_HOSTNAME
  UseLocalStack:
    Type: String
    Default: "true"

Conditions:
  UseLocalStack:  !Equals [ !Ref UseLocalStack, "true" ]


# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 360
    Environment:
      Variables:
        REGION: !Ref "AWS::Region"
        ENVIRONMENT: !Ref Environment
        PROJECT: !Ref Project
        COMPONENT: !Ref Component
        DYNAMO_DB_JOB_TABLE: !Ref JobTable
        DYNAMO_DB_URL: !Ref DynamoDBUrl


Resources:
  TestFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Join [ "-", [ !Ref Environment, !Ref Component, "api" ] ]
      CodeUri: api/
      Handler: api
      Runtime: go1.x
      Architectures:
        - x86_64
      Tracing: Active
      Events:
        CatchAll:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /{proxy+}
            Method: ANY
      Environment:
        Variables:
          .....
      Policies:
        - DynamoDBCrudPolicy:
            TableName: !Ref JobTable
        - AmazonAPIGatewayInvokeFullAccess

  WorkerFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: !Join [ "-", [ !Ref Environment, !Ref Component, "worker" ] ]
      CodeUri: worker/
      Handler: worker
      Runtime: go1.x
      Architectures:
        - x86_64
      MemorySize: 512
      Timeout: 180
      Tracing: Active
      Events:
        JobTable:
          Type: DynamoDB
          Properties:
            Stream: !GetAtt JobTable.StreamArn
            StartingPosition: TRIM_HORIZON
            BatchSize: 1
      Environment:
        Variables:
          ....
      Policies:
        - DynamoDBCrudPolicy: # localStack complains because the policy name is < 20 char
            TableName: !Ref JobTable
        - Fn::If:
          - UseLocalStack 
          - AmazonSNSFullAccessLS
          - AmazonSNSFullAccess # localStack complains because the policy name is < 20 char
        - AmazonAPIGatewayInvokeFullAccess

  JobTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: !Join [ "-", [ !Ref Environment, !Ref Component, "jobs" ] ]
      AttributeDefinitions:
        - AttributeName: Id
          AttributeType: S
      KeySchema:
        - AttributeName: Id
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      StreamSpecification:
        StreamViewType: NEW_IMAGE


Outputs:
  ....

```

```
awslocal iam create-policy --policy-name AmazonSNSFullAccessLS --policy-document '{"Version":"2012-10-17","Statement":[{"Action":["sns:*"],"Effect":"Allow","Resource":"*"}]}'
awslocal iam create-policy --policy-name AmazonAPIGatewayInvokeFullAccess --policy-document '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["execute-api:Invoke","execute-api:ManageConnections"],"Resource":"arn:aws:execute-api:*:*:*"}]}'

samlocal build && samlocal deploy --guided
```


### Environment

```markdown
- OS: Mac os Monterrey
- LocalStack: 0.14.3
```


### Anything else?

_No response_
template = parse_template(template)
    return json.dumps(template)
def is_local_service_url(url):
    if not url:
        return False
    candidates = (
        constants.LOCALHOST,
        constants.LOCALHOST_HOSTNAME,
        config.LOCALSTACK_HOSTNAME,
        config.HOSTNAME_EXTERNAL,
    )
    if any(re.match(r"^[^:]+://[^:/]*%s([:/]|$)" % host, url) for host in candidates):
        return True
    host = url.split("://")[-1].split("/")[0]
    return "localhost" in host
def convert_s3_to_local_url(url):
    url_parsed = urlparse(url)
    path = url_parsed.path
    headers = CaseInsensitiveDict({"Host": url_parsed.netloc})
    bucket_name = s3_utils.extract_bucket_name(headers, path)
    key_name = s3_utils.extract_key_name(headers, path)
    
    bucket_name = s3_listener.normalize_bucket_name(bucket_name)
    local_url = f"{config.service_url('s3')}/{bucket_name}/{key_name}"
    return local_url