Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong AWS profile being retrieved in case of non default profile configured #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

grufino
Copy link

@grufino grufino commented Aug 20, 2022

There is already an issue reported by another user which I was able to reproduce in a slightly different scenario, but the error and conditions are apparently the same: #118

I drilled down and was able to identify the exact error scenario, steps to reproduce:

  1. Configure a dbt profile that uses a non default aws profile, e.g.:
raptor:
  outputs:
    dev:
      database: awsdatacatalog
      region_name: us-west-2
      s3_staging_dir: s3://athenagraphs/athena/
      schema: graphs_db
      aws_profile_name: athena
      type: athena
  target: dev
  1. Have the same config on your ~/.aws/credentials and ~/.aws/config files, e.g.:
    credentials:
[athena]
aws_access_key_id = ***********************
aws_secret_access_key = ***********************************

config:

[athena]
region = us-west-2
  1. create a data materialization in dbt (anything will work, just materialize to a table) and try to run it twice.

In the first time it will be created fine, but in the second, when it has to run the macro drop_relation (which maps to the python function clean_up_table), there will be an error in the dbt logs:

17:44:02.203689 [debug] [Thread-1 ]: Athena adapter: Error running SQL: macro drop_relation
17:44:02.212093 [error] [MainThread]: local variable 'table' referenced before assignment

Which when debugged from within python, inside the function clean_up_table, you notice the following:

ipdb> table = glue_client.get_table(
                DatabaseName=database_name,
                Name=table_name
            )
*** botocore.exceptions.ClientError: An error occurred (UnrecognizedClientException) when calling the GetTable operation: The security token included in the request is invalid.

So I noticed that the profile being used by boto3 was the default, it was trying to use my default profile AWS credentials to run this command, which justifies the credentials error. I have also tested that without specifying a profile (so it tries to get the default one) this error doesn't occur.

The solution is very specific to the problem I had, but I believe it might be an issue for the other functions that use the same logic to acquire a client, I just don't have scenarios to test all of them so changed only this one. I tried to look for an earlier place in the code to put this so it would already be the default when needed and this function wouldn't have to worry about it, but since we only have the profile after opening the connection, this is the best I could do, please let me know if you can see a better place to put this.

@cstruct
Copy link

cstruct commented Aug 23, 2022

This is a duplicate of #56 and #121. Setting boto3.setup_default_session there also won't fix the issue in all cases.

w0rks74710n pushed a commit to w0rks74710n/dbt-athena that referenced this pull request Aug 29, 2022
…upport configured AWS profile when calling boto3
nicor88 pushed a commit to Avinash-1394/dbt-athena that referenced this pull request Jan 27, 2023
Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants