Athena query fails with boto3 (S3 location invalid)

Viewed 14631

I'm trying to execute a query in Athena, but it fails.

Code:

client.start_query_execution(QueryString="CREATE DATABASE IF NOT EXISTS db;",
                           QueryExecutionContext={'Database': 'db'},
                           ResultConfiguration={
                                     'OutputLocation': "s3://my-bucket/",
                                     'EncryptionConfiguration': {
                                             'EncryptionOption': 'SSE-S3'
                                             }
                                     })

But it raises the following exception:

botocore.errorfactory.InvalidRequestException: An error occurred (InvalidRequestException) 
when calling the StartQueryExecution operation: The S3 location provided to save your 
query results is invalid. Please check your S3 location is correct and is in the same 
region and try again. If you continue to see the issue, contact customer support 
for further assistance.

However, if I go to the Athena Console, go to Settings and enter the same S3 location (for example):

athena settings

the query runs fine.

What's wrong with my code? I've used the API of several the other services (eg, S3) successfully, but in this one I believe I'm passing some incorrect parameters. Thanks.

Python: 3.6.1. Boto3: 1.4.4

5 Answers

In my case, IAM role didn't have all the permissions for the S3 bucket. I gave IAM role following permissions for Athena results bucket.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:GetObject",
                "s3:ListBucket",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::athena_results_bucket",
                "arn:aws:s3:::athena_results_bucket"
            ],
            "Effect": "Allow"
        }
    ]
}

I received the OP error, attempted Justin's answer, and got the following error

SYNTAX_ERROR: line 1:15: Schema TableName does not exist

Meaning that it was not able to find the tables that I had previously created through the AWS Athena UI.

The simple solution was to use dclaze's answer instead. These two answers cannot be used simultaneously, or you will get back the initial (OP) error.

Related