title: (InvalidRequestException) when calling the GetQueryResults..... Querying Athena From Lambda Python.... Cannot Read Results
body: <p>I have been trying to query Athena from my lambda function (Python3.8) but I keep getting the same error although tried adding an if else statement to check the status of the execution and i always the same error on the aws console and the cli locally</p>
  <p>Here is the lambda function:</p>
  <pre><code>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)
  </code></pre>
  <p>and this is the IAM Role I have attached to my lambda function:</p>
  <pre><code>{
      &quot;Version&quot;: &quot;2012-10-17&quot;,
      &quot;Statement&quot;: [
          {
              &quot;Sid&quot;: &quot;VisualEditor0&quot;,
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Action&quot;: [
                  &quot;S3:GetBucketLocation&quot;,
                  &quot;S3:GetObject&quot;
              ],
              &quot;Resource&quot;: [
                  &quot;arn:aws:s3:::athena-results-queries-developer/*&quot;,
              ]
          },
          {
              &quot;Sid&quot;: &quot;VisualEditor1&quot;,
              &quot;Effect&quot;: &quot;Allow&quot;,
              &quot;Action&quot;: [
                  &quot;athena:StartQueryExecution&quot;,
                  &quot;athena:StopQueryExecution&quot;,
                  &quot;athena:GetQueryExecution&quot;,
                  &quot;athena:GetQueryResults&quot;,
                  &quot;glue:GetTable&quot;
              ],
              &quot;Resource&quot;: &quot;*&quot;
          }
      ]
  }
  </code></pre>
  <p>This is the error I keep getting in the logs</p>
  <blockquote>
  <p>An error occurred (InvalidRequestException) when calling the GetQueryResults operation: Query did not finish successfully. Final query state: FAILED</p>
  <p>&quot;errorType&quot;: &quot;InvalidRequestException&quot;</p>
  <p>&quot;stackTrace&quot;: [<br />
  [<br />
  &quot;/var/task/lambda_function.py&quot;,<br />
  26,<br />
  &quot;function&quot;,<br />
  &quot;results=client.get_query_results(QueryExecutionId = queryId)&quot;<br />
  ],
  [
  &quot;/var/runtime/botocore/client.py&quot;,
  316,
  &quot;_api_call&quot;,
  &quot;return self._make_api_call(operation_name, kwargs)&quot;
  ],
  [
  &quot;/var/runtime/botocore/client.py&quot;,
  626,
  &quot;_make_api_call&quot;,
  &quot;raise error_class(parsed_response, operation_name)&quot;
  ]
  ]
  }</p>
  </blockquote>
  <p>If anyone can help me would really appreciate it - I have been trying to solve this for days</p>