简体   繁体   English

AWS Athena PyAthena AccessDeniedException

[英]AWS Athena PyAthena AccessDeniedException

I am new to AWS.我是 AWS 的新手。 I have a user account and two roles, one for prod one for test.我有一个用户帐户和两个角色,一个用于生产,一个用于测试。

Usually I log into my account and switch to prod role to run some simple select queries.通常我登录我的帐户并切换到 prod 角色来运行一些简单的选择查询。

Now I want to use Athena locally in Python with PyAthena.现在我想通过 PyAthena 在 Python 中本地使用 Athena。 I have tried the following resources from PyAthena documentation:我已经尝试了 PyAthena 文档中的以下资源:

from pyathena import connect
import pandas as pd

conn = connect(aws_access_key_id='YOUR_ACCESS_KEY_ID',
               aws_secret_access_key='YOUR_SECRET_ACCESS_KEY',
               s3_staging_dir='s3://YOUR_S3_BUCKET/path/to/',
               region_name='us-west-2')
df = pd.read_sql("SELECT * FROM many_rows", conn)
print(df.head())

But always having the error但是总是报错

An error occurred (AccessDeniedException) when calling the StartQueryExecution operation: User: arn:aws:iam::xxxxxx:user/xxxx@xxxxx is not authorized to perform: athena:StartQueryExecution on resource: arn:aws:athena:ap-southeast-2:xxxxx:workgroup/primary

This is the exact error I would get if I run the same query using my user account without switching the role.如果我使用我的用户帐户运行相同的查询而不切换角色,这正是我会得到的错误。

I have also tried to add a profile name parameter in connect but still not working even though the env is correctly recognised.我还尝试profile name parameter in connect添加profile name parameter in connect但即使正确识别了 env,仍然无法正常工作。

Could someone help me how to do the 'switch' role step in local python code?有人可以帮助我如何在本地 python 代码中执行“切换”角色步骤?

It seems like the issue is due to a missing role and not the profile_name parameter.问题似乎是由于缺少角色而不是profile_name参数。 If you look at the Connection class in pyathena, there's a role_arn variable that you can specify while initializing the connection.如果您查看 pyathena 中的Connection类,您可以在初始化连接时指定一个role_arn变量。 Here's the line I'm talking about. 这是我正在谈论的线路。

You might want to try it this way -你可能想这样试试——

conn = connect(aws_access_key_id='YOUR_ACCESS_KEY_ID',
               aws_secret_access_key='YOUR_SECRET_ACCESS_KEY',
               s3_staging_dir='s3://YOUR_S3_BUCKET/path/to/',
               region_name='us-west-2',
               role_arn='<your arn here'>)

I haven't tested it myself though since I do not have an Athena setup.我没有自己测试过,因为我没有 Athena 设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM