Aws lambda (InvalidRequestException)调用GetQueryResults时。。。。。从Lambda Python查询Athena。。。。无法读取结果

Aws lambda (InvalidRequestException)调用GetQueryResults时。。。。。从Lambda Python查询Athena。。。。无法读取结果,aws-lambda,boto3,amazon-iam,amazon-athena,python-3.8,Aws Lambda,Boto3,Amazon Iam,Amazon Athena,Python 3.8,我一直在尝试从lambda函数(Python3.8)中查询Athena,但我不断收到相同的错误,尽管我尝试添加if-else语句来检查执行状态,但我始终在aws控制台和本地cli上遇到相同的错误 以下是lambda函数: import json import boto3 import time def function(event, context): client=boto3.client('athena') #setup and perform query que

我一直在尝试从lambda函数(Python3.8)中查询Athena,但我不断收到相同的错误,尽管我尝试添加if-else语句来检查执行状态,但我始终在aws控制台和本地cli上遇到相同的错误

以下是lambda函数:

import json
import boto3
import time

def function(event, context):
    client=boto3.client('athena')

    #setup and perform query
    queryStart=client.start_query_execution(
        QueryString = 'SELECT * FROM my_s3_bucket_developer limit 8;',
        QueryExecutionContext = {
            'Database':'mydb'
        },
        ResultConfiguration = {
            'OutputLocation': 's3://athena-results-queries-developer/'
        }
    ) 
    
    #get query ID
    queryId= queryStart['QueryExecutionId']

    #we gonna sleep the function now because we don't know how 
    #long it will take to execute the query
    time.sleep(25)

    results=client.get_query_results(QueryExecutionId = queryId)
    for row in results['ResultSet']['Rows']:
        print(row)
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "S3:GetBucketLocation",
                "S3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::athena-results-queries-developer/*",
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "athena:StartQueryExecution",
                "athena:StopQueryExecution",
                "athena:GetQueryExecution",
                "athena:GetQueryResults",
                "glue:GetTable"
            ],
            "Resource": "*"
        }
    ]
}
这是我在lambda函数中附加的IAM角色:

import json
import boto3
import time

def function(event, context):
    client=boto3.client('athena')

    #setup and perform query
    queryStart=client.start_query_execution(
        QueryString = 'SELECT * FROM my_s3_bucket_developer limit 8;',
        QueryExecutionContext = {
            'Database':'mydb'
        },
        ResultConfiguration = {
            'OutputLocation': 's3://athena-results-queries-developer/'
        }
    ) 
    
    #get query ID
    queryId= queryStart['QueryExecutionId']

    #we gonna sleep the function now because we don't know how 
    #long it will take to execute the query
    time.sleep(25)

    results=client.get_query_results(QueryExecutionId = queryId)
    for row in results['ResultSet']['Rows']:
        print(row)
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "S3:GetBucketLocation",
                "S3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::athena-results-queries-developer/*",
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "athena:StartQueryExecution",
                "athena:StopQueryExecution",
                "athena:GetQueryExecution",
                "athena:GetQueryResults",
                "glue:GetTable"
            ],
            "Resource": "*"
        }
    ]
}
这就是我一直在日志中看到的错误

调用GetQueryResults操作时发生错误(InvalidRequestException):查询未成功完成。最终查询状态:失败

“errorType”:“InvalidRequestException”

“stackTrace”:[
[
“/var/task/lambda_function.py”,
26,
“功能”,
“results=client.get\u query\u results(QueryExecutionId=queryId)”
], [ “/var/runtime/botocore/client.py”, 316, “_api_调用”, 返回self.\u make\u api\u调用(操作名称,kwargs) ], [ “/var/runtime/botocore/client.py”, 626, “_make_api_call”, 引发错误\u类(已解析的\u响应、操作\u名称) ] ] }


如果有人能帮我的话,我会非常感激的-我已经尝试解决这个问题好几天了

问题是你没有等到查询正确完成。您需要调用
get\u query\u execution
并检查查询是否成功,然后再调用
get\u query\u results


这里有一个完整的例子,你可以从中获得灵感:

谢谢你,你为我节省了很多时间。我在代码中添加的所有内容都是在get\u query\u results()之前的这一行。get\u query\u execution(QueryExecutionId=queryId)