본문 바로가기

⭐ AWS/Athena

ALB Access 로그를 S3에 저장 후 Athena를 통한 분석

1. ALB Access log 활성화

2022.07.22 - [⭐ AWS/ELB (ALB, NLB, CLB)] - ALB 액세스 로그 활성화 하기

 

ALB 액세스 로그 활성화 하기

# ALB에서 액세스 로그를 활성화 하는 방법에 대해서 알아본다. 1. 생성된 ALB를 클릭하여 아래와 같이 이동한다. 속성 편집을 클릭하여 편집 창으로 이동한다. 2. Access 로그 활성화 아래와 같이 Acce

may9noy.tistory.com

2. Athena에서 S3 ALB Access log 테이블 생성

CREATE EXTERNAL TABLE IF NOT EXISTS log_analysis ( # 테이블 명 입력
            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://alb-accesslog/AWSLogs/12345678910/elasticloadbalancing/ap-northeast-2/';
            # s3 저장소 주소, 계정번호, 리전정보등만 변경하여 입력한다. 나머지 정보는 바꾸지 않는다.

3. 쿼리조회

--  bcheck ALB log 조회하는 쿼리 입니다.
SELECT * FROM "alb_log_analysis"."log_analysis"
WHERE "client_ip" = '검색ip주소'
ORDER by "time" DESC;

# 참고로 Athena에서 주석은 컨트롤 + / 이다.