1

I'm working with AWS Lambda and I would like to make a simple query in athena and store my data in an s3.

My code :

import boto3

def lambda_handler(event, context):
    query_1 = "SELECT * FROM test_athena_laurent.stage limit 5;"
    database = "test_athena_laurent"
    s3_output = "s3://athena-laurent-result/lambda/"

    client = boto3.client('athena')

    response = client.start_query_execution(
    QueryString=query_1,
    ClientRequestToken='string',
    QueryExecutionContext={
        'Database': database
    },
    ResultConfiguration={
        'OutputLocation': 's3://athena-laurent-result/lambda/'
    }
    )
    return response

It works on spyder 2.7 but in AWS I have this error :

Parameter validation failed:
Invalid length for parameter ClientRequestToken, value: 6, valid range: 32-inf: ParamValidationError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 18, in lambda_handler
    'OutputLocation': 's3://athena-laurent-result/lambda/'

I think that It doesn't understand my path and I don't know why.

Thanks

3
  • "Parameter validation failed - Invalid length for parameter ClientRequestToken" is pretty unambiguous, as far as error messages are concerned. Maybe 'string' is not the right value for this parameter. Commented Nov 29, 2017 at 13:19
  • Thanks, it's my error. Do you know where I can find ClientRequestToken ? Commented Nov 29, 2017 at 13:30
  • I delete ClientRequestToken like in spyder. In spyder it work but in AWS It doesn't. May be I do not have the rights. ClientError: An error occurred (AccessDeniedException) when calling the StartQueryExecution Commented Nov 29, 2017 at 13:39

2 Answers 2

2

ClientRequestToken (string) -- A unique case-sensitive string used to ensure the request to create the query is idempotent (executes only once). If another StartQueryExecution request is received, the same response is returned and another query is not created. If a parameter has changed, for example, the QueryString , an error is returned. [Boto3 Docs]

This field is autopopulated if not provided.

If you are providing a string value for ClientRequestToken, ensure it is within length limits from 32 to 128.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the link to the docs, very helpful. I was looking at boto3 docs and it's not there. The error message i get is Invalid length for parameter ClientRequestToken, value: 28, valid range: 32-inf which implies 32 until infinitiy
0

Per @Tomalak's point ClientRequestToken is a string. However, per the documentation I just linked, you don't need it anyway when using the SDK.

This token is listed as not required because AWS SDKs (for example the AWS SDK for Java) auto-generate the token for users. If you are not using the AWS SDK or the AWS CLI, you must provide this token or the action will fail.

So, I would refactor as such:

import boto3


def lambda_handler(event, context):
    query_1 = "SELECT * FROM some_database.some_table limit 5;"
    database = "some_database"
    s3_output = "s3://some_bucket/some_tag/"

    client = boto3.client('athena')

    response = client.start_query_execution(QueryString = query_1,
                                        QueryExecutionContext={
                                            'Database': database
                                        },
                                        ResultConfiguration={
                                            'OutputLocation': s3_output
                                        }
                                        )
    return response

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.