Using AWS Glue Python shell job to run Athena query

Using AWS Glue Python shell job to run Athena query

Share Everywhere

Table of contents

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

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?!

Bạn thấy bài viết này như thế nào?
1 reaction

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.

Bài viết liên quan

Khám phá FinOps

Khám phá FinOps - công nghệ đám mây

Nhiều doanh nghiệp ngày nay lựa chọn chuyển sang công nghệ đám mây với hi vọng đạt được lợi thế cạnh tranh so với đối thủ nhờ tiềm năng về hiệu quả cao và tiết kiệm chi phí hơn của công nghệ này.
Microservices Roadmap

Microservices Roadmap

- Kafka, RabbitMQ, Amazon SQS: Efficient and reliable message brokers for seamless communication between microservices.
The Data Analyst Roadmap

The Data Analyst Roadmap

**Database Knowledge**: Gain proficiency in working with databases like MySQL, PostgreSQL, or MongoDB.
Architectural patterns in software design

Architectural patterns in software design

Choose the architecture that aligns with your application's unique needs and goals. Each pattern offers a tailored approach to elevate your software system!
Exploring the Technological Marvel Behind Netflix

Exploring the Technological Marvel Behind Netflix

Ever wondered about the tech wizardry that powers your binge-watching adventures on Netflix?