Lambda Python request athena error OutputLocation

允我心安 提交于 2019-12-24 07:55:00

问题


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


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/47551305/lambda-python-request-athena-error-outputlocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!