AdamD97
AdamD97

Reputation: 87

AWS lambda query on Athena returns nothing

I am trying to make a Python script containing a query to my Athena database (I created an Amazon S3 bucket as an output). I saw some basics tutorials to do so, and got this code :

import json
import boto3
import time

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

    QueryResponse = client.start_query_execution(
        QueryString = "SELECT id FROM table;",
        QueryExecutionContext = {
            'Database' : 'raw'
        },
        ResultConfiguration = {
            'OutputLocation' : 's3://mybucket/'
       }
    )


    #Oberserve results : 
    queryId = QueryResponse['QueryExecutionId']
    time.sleep(10)

    results = client.get_query_results(QueryExecutionId = queryId)
    for row in results['ResultSet']['Rows']:
        print(row)

This code is supposed to print data retrieved by the query. But when I run it, I have this error message :

    Response
{
  "errorMessage": "An error occurred (InvalidRequestException) when calling the 
GetQueryResults operation: Query did not finish successfully. Final query state: FAILED",
  "errorType": "InvalidRequestException",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 23, in lambda_handler\n    results = 
client.get_query_results(QueryExecutionId = queryId)\n",
    "  File \"/var/runtime/botocore/client.py\", line 357, in _api_call\n    return 
self._make_api_call(operation_name, kwargs)\n",
    "  File \"/var/runtime/botocore/client.py\", line 676, in _make_api_call\n    raise 
error_class(parsed_response, operation_name)\n"
  ]
}

I tried to run the query on Athena to see if it was returning data and the query works perfectly. I also tried to run the code without the part writter after : '#Observe results :' and it seems that my code doesn't return anything because the response is just 'null'. I don't understand why or where the problem comes from. Any ideas or suggestions about what I should do ?

Thanks for helping.

Upvotes: 1

Views: 1452

Answers (1)

AdamD97
AdamD97

Reputation: 87

Ok I just checked the history in Athena and it was actually an Amazon S3 permission problem. I did add the role I use permissions on S3 and it works fine.

Upvotes: 1

Related Questions