gpt4 book ai didi

amazon-web-services - 使用 Lambda 函数运行 AWS Athena 的查询

转载 作者:行者123 更新时间:2023-12-04 01:49:01 31 4
gpt4 key购买 nike

我在 AWS Athena 上创建了一个表,我可以在该表上运行任何查询而不会出现任何错误:

select * from mytestdb.test

该表有三列, customer_Id, product_Id, price .

我尝试创建一个使用 boto3 为我运行相同查询的 lambda 函数:
import time
import boto3

DATABASE = 'mytestdb'
TABLE = 'test'

output='s3://mybucketons3/'

COLUMN = 'Customer_Id'

def lambda_handler(event, context):

keyword = 'xyz12345'

query = "SELECT * FROM %s.%s where %s = '%s';" % (DATABASE, TABLE, COLUMN, keyword)

client = boto3.client('athena')

# Execution
response = client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': DATABASE
},
ResultConfiguration={
'OutputLocation': output,
}
)


return

但是我收到以下错误:
Response:
{
"errorMessage": "An error occurred (AccessDeniedException) when calling the StartQueryExecution operation: User: arn:aws:sts::076088932150:assumed-role/Test/QueryTest is not authorized to perform: athena:StartQueryExecution on resource: arn:aws:athena:us-west-2:076088932150:workgroup/primary",
"errorType": "ClientError",

这似乎是一种访问问题,但我不知道为什么,因为我同时拥有 lambda 和 athena db 具有相同的帐户。

最佳答案

正如我在评论中提到的,您的 Lambda 角色应该包含允许与 Athena 服务交互的策略。我还为您的 S3 存储桶添加了完全权限。例子:

{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1547414166585",
"Action": [
"athena:StartQueryExecution"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "Stmt1547414166586",
"Action": [
"s3:*"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::your-bucket-name/*"
}
]
}

关于amazon-web-services - 使用 Lambda 函数运行 AWS Athena 的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54172923/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com