title: Run AWS Athena’s queries with Lambda function
body: <p>I created a table on AWS Athena on which I can run any query without any error:</p>
  
  <pre><code>select * from mytestdb.test
  </code></pre>
  
  <p>The table has three columns, <code>customer_Id, product_Id, price</code>.</p>
  
  <p>I tried to create a lambda function that run the same query for me using boto3:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>However I got the following error:</p>
  
  <pre><code>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",
  </code></pre>
  
  <p>It seems sort of access issue however I am not sure why because I have both lambda and athena db with the same account.  </p>