AWS CLIでAWS Athenaのクエリがデータベースが見つからないためになんども失敗する場合は結果出力先のS3のバケットのregionを確認する
俺の屍を越えていけ、的なメモ。
require 'aws-sdk-athena' client = Aws::Athena::Client.new begin start_response = client.start_query_execution({ query_string: "SELECT * FROM samples limit 10", query_execution_context: { database: "mydatabase", }, result_configuration: { output_location: "s3://sample-woshidan-test-tokyo/athena_query_result" }, }) sleep(5) result_response = client.get_query_results({ query_execution_id: start_response.query_execution_id }) rescue Aws::Athena::Errors::InvalidRequestException => ex puts ex.inspect ensure puts "Ensure" end
上記のコードで aws-sdk-athena
でAthena のクエリを飛ばそうとしたら
Query did not finish successfully. Final query state: FAILED (Aws::Athena::Errors::InvalidRequestException)
というエラーが出て失敗する。なんで失敗したのかさっぱりわからないので、CLIで実行し直すと、
$ aws athena start-query-execution \ > --query-string "SELECT * FROM mydatabase.samples limit 10;" \ > --result-configuration OutputLocation=s3://sample-woshidan-test/athena_query_result { "QueryExecutionId": "f8e45456-238a-44ba-955a-40f048e5c3b2" } $ aws athena get-query-execution --query-execution-id f8e45456-238a-44ba-955a-40f048e5c3b2 { "QueryExecution": { "Status": { "SubmissionDateTime": 1506675885.243, "State": "FAILED", "CompletionDateTime": 1506675885.387, "StateChangeReason": "com.facebook.presto.hive.DataCatalogException: Namespace mydatabase not found. Please check your query." }, "Query": "SELECT * FROM mydatabase.logs limit 10", "Statistics": { "DataScannedInBytes": 0, "EngineExecutionTimeInMillis": 53 }, "ResultConfiguration": { "OutputLocation": "s3://sample-woshidan-test/athena_query_result/f8e45456-238a-44ba-955a-40f048e5c3b2.csv" }, "QueryExecutionId": "f8e45456-238a-44ba-955a-40f048e5c3b2" } }
というメッセージが出て、どうも database
が見つからないらしい。
もしかして、 AWS のアカウントの region
を間違ったかな、とAWS CLIの設定で使う region を Athenaの管理画面で表示されている region に設定しなおしてみると
- region = ap-northeast-1 + region = us-east-2
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.
戻すと
+ region = ap-northeast-1 - region = us-east-2
Query did not finish successfully. Final query state: FAILED (Aws::Athena::Errors::InvalidRequestException)
どっちに設定しても何かしらダメっぽい...
最終的に
自分がAthenaのクライアントとして選んだ aws-athena-client
はその実装でAWS CLIを利用しています。
そして、AWS CLIで設定値として使うregion は ~/.aws/config
で指定しているためか
- クエリを投げるAthenaのデータベースが
~/.aws/config
で指定したregionに存在すること - クエリ結果の出力先のS3のバケットが
~/.aws/config
で指定したregionに存在すること
の2点がAWS CLIでAthenaにクエリを投げるためには必要みたいです。そして最終的に結果出力先のS3のバケットのregionをAthenaのデータベースのregionに揃えてことなきを得ました。
AthenaのクエリでスキャンするS3のバケットのregionは、AWS CLIで使うIAMアカウントがアクセス権を持ってさえいればAthenaのデータベースのregionと違ってもクエリが実行できるのもあっていまいち原因がわからず焦りました。
もしかしたらAthena - S3 間だけでなく、複数のAWSのサービスが協調して動く場合、CLIが一個しかregionの値を持たない、みたいなことが原因で似たようなトラブルはあるかもなと思いつつ、現場からは以上です。