Using AWS Glue Python shell job to run Athena query
Thực hiện set up connection: Tham khảo: https://shinchan.asia/2022/05/21/run-an-aws-glue-job-inside-a-vpc/
(phải set up S3 và athena endpoint – nếu dùng Athena endpoint thì sẽ k cần NAT, không cần internet connection!).
- Note: Có thể sử dụng Glue endpoint thay vì Athena endpoint
2. Tạo job:
Chọn Python Shell script editor
Chọn Create
Click sang Job detail:
Điền Name
Chọn IAM Role
Click mở rộng Advanced properties
Chọn Connection đã tạo ở phần Connections
Click sang tab Script
Dán script vào editor
Save
Chọn Run để run job
Script để run:
import time
import boto3
query = 'SELECT * FROM cloudtrail_logs_aws_cloudtrail_logs_741023408660_05daf6c9 LIMIT 5'
DATABASE = 'default'
output='s3://yen-bucket-demo/'
query = "SELECT * FROM default.tb"
client = boto3.client('athena')
# Execution
response = client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': DATABASE
},
ResultConfiguration={
'OutputLocation': output,
}
)
print(response)
Cái SG để Glue connect tới S3 hơi đặc biệt chút, nếu không mở all TCP port thì sẽ toang kết nối. -> phải có ít nhất 1 rule mở all TCP trong SG!
\=> Chốt vấn đề ở đây là:
1/ Khi gặp timeout, đầu tiên hãy kiểm tra SG! Nếu đảm bảo SG không sai rồi thì tiếp tục investigate tới endpoint!
2/ Buộc phải tạo S3 endpoint và Athena endpoint cho VPC. Với S3 endpoint, nếu sử dụng dạng Gateway thì phải kiểm tra route table xem đã route đến chưa. Còn Athena endpoint hay S3 endpoint dạng Interface thì không cần route. (Lưu ý: Interface mất phí, còn Gateway thì không)
3/ Nếu đã tạo rồi, vẫn thấy không được thì thử xóa gateway đi tạo lại?!