【问题标题】:AWS Lambda function fails while query AthenaAWS Lambda 函数在查询 Athena 时失败
【发布时间】:2020-08-31 15:31:00
【问题描述】:

我正在尝试编写一个简单的 Lambda 函数来查询 Athena 中的表。但几秒钟后,我在 Cloudwatch 日志中看到“状态:失败”。 没有关于失败原因的描述性错误消息。

我的测试代码如下:

import json
import time
import boto3

# athena constant
DATABASE = 'default'
TABLE = 'test'

# S3 constant
S3_OUTPUT = 's3://test-output/'

# number of retries
RETRY_COUNT = 1000

def lambda_handler(event, context):
    # created query
    query = "SELECT * FROM default.test limit 2"
    #  % (DATABASE, TABLE)
    # athena client
    client = boto3.client('athena')
    
    # Execution
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': DATABASE
        },
        ResultConfiguration={
            'OutputLocation': S3_OUTPUT,
        }
    )
    
    # get query execution id
    query_execution_id = response['QueryExecutionId']
    print(query_execution_id)
    
    # get execution status
    for i in range(1, 1 + RETRY_COUNT):

        # get query execution
        query_status = client.get_query_execution(QueryExecutionId=query_execution_id)
        query_execution_status = query_status['QueryExecution']['Status']['State']

        if query_execution_status == 'SUCCEEDED':
            print("STATUS:" + query_execution_status)
            break

        if query_execution_status == 'FAILED':
            #raise Exception("STATUS:" + query_execution_status)
            print("STATUS:" + query_execution_status)

        else:
            print("STATUS:" + query_execution_status)
            time.sleep(i)
    else:
        # Did not encounter a break event. Need to kill the query
        client.stop_query_execution(QueryExecutionId=query_execution_id)
        raise Exception('TIME OVER')
        
    # get query results
    result = client.get_query_results(QueryExecutionId=query_execution_id)
    print(result)
    return

日志显示如下:

2020-08-31T10:52:12.443-04:00
    
START RequestId: e5434651-d36e-48f0-8f27-0290 Version: $LATEST
    
2020-08-31T10:52:13.481-04:00
    
88162f38-bfcb-40ae-b4a3-0b5a21846e28
    
2020-08-31T10:52:13.500-04:00
    
STATUS:QUEUED
    
2020-08-31T10:52:14.519-04:00
    
STATUS:RUNNING
    
2020-08-31T10:52:16.540-04:00
    
STATUS:RUNNING
    
2020-08-31T10:52:19.556-04:00
    
STATUS:RUNNING
    
2020-08-31T10:52:23.574-04:00
    
STATUS:RUNNING
    
2020-08-31T10:52:28.594-04:00
    
STATUS:FAILED
    
2020-08-31T10:52:28.640-04:00
    
....more status: FAILED
....
    
END RequestId: e5434651-d36e-48f0-8f27-0290
    
REPORT RequestId: e5434651-d36e-48f0-8f27-0290 Duration: 30030.22 ms Billed Duration: 30000 ms Memory Size: 128 MB Max Memory Used: 72 MB Init Duration: 307.49 ms

2020-08-31T14:52:42.473Z e5434651-d36e-48f0-8f27-0290 Task timed out after 30.03 seconds 

我认为我拥有授予该角色的 S3 存储桶访问权限(如果没有,我会看到错误消息)。桶中也没有创建文件。我不确定这里出了什么问题。我错过了什么? 谢谢

【问题讨论】:

  • 如果您在 Amazon Athena 控制台中运行查询,运行需要多长时间?
  • 要测试您首先需要确保此查询直接在 Athena 中正常工作?你能在 Athena 中运行它并确认吗?]\
  • 是您的代码打印了 FAILED,尝试打印更多从 Athena 返回的错误。

标签: amazon-web-services aws-lambda amazon-athena


【解决方案1】:

日志中的最后一行显示

2020-08-31T14:52:42.473Z e5434651-d36e-48f0-8f27-0290 Task timed out after 30.03 seconds 

在我看来,Lambda 函数的超时设置为 30 秒。尝试将其增加到超过 Athena 查询所需的时间(最长为 15 分钟)。

【讨论】:

  • 我将超时时间增加到 10 分钟。但我得到状态:失败。不确定查询的构建方式是否有问题。无法获取有关函数失败原因的信息。
  • 您可以做两件事以进行进一步调查 1. 在 AWS 控制台中尝试查询,看看是否出现错误或 2. 还将“UnprocessedQueryExecutionIds”部分中的“StateChangeReason”和“ErrorMessage”字段打印到查看更多错误消息(请参阅响应语法here
猜你喜欢
  • 2018-11-18
  • 2021-07-26
  • 2018-08-03
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 2019-12-03
  • 2021-12-27
  • 2020-09-12
相关资源
最近更新 更多