본문 바로가기
[AWS-SM]/Athena

[중요] Query AWS ALB Access Logs in Amazon S3 with Athena

by SAMSUNG Metaverse-Cloud 2023. 4. 2.
728x90

 

Athena를 사용하여 Amazon S3에서 AWS ALB 액세스 로그 쿼리

 

Querying+Application+Load+Balancer+Logs.sql
0.00MB

 

 

 

 

 

 

 

 

 

 

 

 

## 위 작업전에 하기처럼 S3버킷에 폴더 생성 필요

 

 

 

 

 

## Run query 수행후, alb_logs 테이블 생성

 

 

 

 

 

 

 

 

 

 

=============================

CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs (
            type string,
            time string,
            elb string,
            client_ip string,
            client_port int,
            target_ip string,
            target_port int,
            request_processing_time double,
            target_processing_time double,
            response_processing_time double,
            elb_status_code string,
            target_status_code string,
            received_bytes bigint,
            sent_bytes bigint,
            request_verb string,
            request_url string,
            request_proto string,
            user_agent string,
            ssl_cipher string,
            ssl_protocol string,
            target_group_arn string,
            trace_id string,
            domain_name string,
            chosen_cert_arn string,
            matched_rule_priority string,
            request_creation_time string,
            actions_executed string,
            redirect_url string,
            lambda_error_reason string,
            target_port_list string,
            target_status_code_list string,
            classification string,
            classification_reason string
            )
            ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.RegexSerDe'
            WITH SERDEPROPERTIES (
            'serialization.format' = '1',
            'input.regex' = 
        '([^ ]*) ([^ ]*) ([^ ]*) ([^ ]*):([0-9]*) ([^ ]*)[:-]([0-9]*) ([-.0-9]*) ([-.0-9]*) ([-.0-9]*) (|[-0-9]*) (-|[-0-9]*) ([-0-9]*) ([-0-9]*) \"([^ ]*) ([^ ]*) (- |[^ ]*)\" \"([^\"]*)\" ([A-Z0-9-]+) ([A-Za-z0-9.-]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^\"]*)\" ([-.0-9]*) ([^ ]*) \"([^\"]*)\" \"([^\"]*)\" \"([^ ]*)\" \"([^\s]+?)\" \"([^\s]+)\" \"([^ ]*)\" \"([^ ]*)\"')
            LOCATION 's3://YOUR-BUCKET-NAME/AWSLogs/YOUR-ACCOUNT-NUMBER/elasticloadbalancing/us-east-1/';

-- ** EXAMPLE QUERIES ** --

-- View the first 100 access log entries in chronological order:

SELECT *
FROM alb_logs
ORDER by time ASC
LIMIT 100;

-- List clients in descending order, by the number of times that each client visited a specified URL:

SELECT client_ip, elb, request_url, count(*) as count from alb_logs
GROUP by client_ip, elb, request_url
ORDER by count DESC;

-- Shows the URLs visited by Chrome browser users:

SELECT request_url
FROM alb_logs
WHERE user_agent LIKE '%Chrome%'
LIMIT 10;

 

 

 

 

 

https://www.youtube.com/watch?v=zXSiPmBgQZQ 

 

728x90