# Buggy code
def fetch_cluster_credentials(
    cls, db_user, db_name, cluster_id, iam_profile, duration_s, autocreate, db_groups
):
    """Fetches temporary login credentials from AWS. The specified user
    must already exist in the database, or else an error will occur"""

    if iam_profile is None:
        boto_client = boto3.client("redshift")
    else:
        logger.debug(
            "Connecting to Redshift using 'IAM'" + f"with profile {iam_profile}"
        )
        boto_session = boto3.Session(profile_name=iam_profile)
        boto_client = boto_session.client("redshift")

    try:
        return boto_client.get_cluster_credentials(
            DbUser=db_user,
            DbName=db_name,
            ClusterIdentifier=cluster_id,
            DurationSeconds=duration_s,
            AutoCreate=autocreate,
            DbGroups=db_groups,
        )

    except boto_client.exceptions.ClientError as e:
        raise dbt.exceptions.FailedToConnectException(
            "Unable to get temporary Redshift cluster credentials: {}".format(e)
        )


# Traceback (KeyError)
2020-09-14 11:15:23.743840 (MainThread): Traceback (most recent call last):
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/adapters/postgres/connections.py", line 46, in exception_handler
yield
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/adapters/sql/connections.py", line 76, in add_query
cursor = connection.handle.cursor()
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/contracts/connection.py", line 69, in handle
self._handle.resolve(self)
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/contracts/connection.py", line 90, in resolve
return self.opener(connection)
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/adapters/postgres/connections.py", line 77, in open
credentials = cls.get_credentials(connection.credentials)
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/adapters/redshift/connections.py", line 152, in get_credentials
return cls.get_tmp_iam_cluster_credentials(credentials)
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/adapters/redshift/connections.py", line 128, in get_tmp_iam_cluster_credentials
credentials.db_groups,
File "/Users/xxx/venv/lib/python3.7/site-packages/dbt/adapters/redshift/connections.py", line 93, in fetch_cluster_credentials
boto_client = boto3.client('redshift')
File "/Users/xxx/venv/lib/python3.7/site-packages/boto3/__init__.py", line 91, in client
return _get_default_session().client(*args, **kwargs)
File "/Users/xxx/venv/lib/python3.7/site-packages/boto3/session.py", line 263, in client
aws_session_token=aws_session_token, config=config)
File "/Users/xxx/venv/lib/python3.7/site-packages/botocore/session.py", line 828, in create_client
endpoint_resolver = self._get_internal_component('endpoint_resolver')
File "/Users/xxx/venv/lib/python3.7/site-packages/botocore/session.py", line 695, in _get_internal_component
return self._internal_components.get_component(name)
File "/Users/xxx/venv/lib/python3.7/site-packages/botocore/session.py", line 907, in get_component
del self._deferred[name]
KeyError: 'endpoint_resolver'

# Fixed code
def fetch_cluster_credentials(
    cls, db_user, db_name, cluster_id, iam_profile, duration_s, autocreate, db_groups
):
    """Fetches temporary login credentials from AWS. The specified user
    must already exist in the database, or else an error will occur"""

    if iam_profile is None:
        session = boto3.Session()
        boto_client = session.client("redshift")
    else:
        logger.debug(
            "Connecting to Redshift using 'IAM'" + f"with profile {iam_profile}"
        )
        boto_session = boto3.Session(profile_name=iam_profile)
        boto_client = boto_session.client("redshift")

    try:
        return boto_client.get_cluster_credentials(
            DbUser=db_user,
            DbName=db_name,
            ClusterIdentifier=cluster_id,
            DurationSeconds=duration_s,
            AutoCreate=autocreate,
            DbGroups=db_groups,
        )

    except boto_client.exceptions.ClientError as e:
        raise dbt.exceptions.FailedToConnectException(
            "Unable to get temporary Redshift cluster credentials: {}".format(e)
        )