OPS

Athena 특정 일자 LB 엑세스 로그 조회

찻잔속청개구리 2025. 6. 15. 04:37
반응형

개요

실무에서 ELB 이슈가 발생할 때 LB 엑세스로그를 꼭 찾아보게 되는데, Athena를 이용하면 자료를 편하게 쿼리할 수 있다. 

Query editor에 넣을 쿼리문도 아래에 첨부했으니, <AWS ID>, <ELB로그 적재되는 S3 버킷 위치>만 각자의 환경에 맞게 치환해서 쓰자.

방법

Athena 특정일자 LB 엑세스 로그 조회

1. Amazon Athena > Query editor > settings 쿼리 결과의 위치 등록

 

2. Amazon Athena > Query editor 에서 아래 쿼리를 넣고 Run

CREATE EXTERNAL TABLE `alb_access_logs`(
  `type` string COMMENT '', 
  `time` string COMMENT '', 
  `elb` string COMMENT '', 
  `client_ip` string COMMENT '', 
  `client_port` int COMMENT '', 
  `target_ip` string COMMENT '', 
  `target_port` int COMMENT '', 
  `request_processing_time` double COMMENT '', 
  `target_processing_time` double COMMENT '', 
  `response_processing_time` double COMMENT '', 
  `elb_status_code` int COMMENT '', 
  `target_status_code` string COMMENT '', 
  `received_bytes` bigint COMMENT '', 
  `sent_bytes` bigint COMMENT '', 
  `request_verb` string COMMENT '', 
  `request_url` string COMMENT '', 
  `request_proto` string COMMENT '', 
  `user_agent` string COMMENT '', 
  `ssl_cipher` string COMMENT '', 
  `ssl_protocol` string COMMENT '', 
  `target_group_arn` string COMMENT '', 
  `trace_id` string COMMENT '', 
  `domain_name` string COMMENT '', 
  `chosen_cert_arn` string COMMENT '', 
  `matched_rule_priority` string COMMENT '', 
  `request_creation_time` string COMMENT '', 
  `actions_executed` string COMMENT '', 
  `redirect_url` string COMMENT '', 
  `lambda_error_reason` string COMMENT '', 
  `target_port_list` string COMMENT '', 
  `target_status_code_list` string COMMENT '', 
  `classification` string COMMENT '', 
  `classification_reason` string COMMENT '', 
  `conn_trace_id` string COMMENT '')
PARTITIONED BY ( 
  `day` string)
ROW FORMAT SERDE 
  'org.apache.hadoop.hive.serde2.RegexSerDe' 
WITH SERDEPROPERTIES ( 
  '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]+)\" \"([^ ]*)\" \"([^ ]*)\" ?([^ ]*)?') 
STORED AS INPUTFORMAT 
  'org.apache.hadoop.mapred.TextInputFormat' 
OUTPUTFORMAT 
  'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION
  's3://<ELB로그 적재되는 S3 버킷 위치>/AWSLogs/<AWS ID>/elasticloadbalancing/ap-northeast-2'
TBLPROPERTIES (
  'projection.day.format'='yyyy/MM/dd', 
  'projection.day.interval'='1', 
  'projection.day.interval.unit'='DAYS', 
  'projection.day.range'='2022/01/01,NOW', 
  'projection.day.type'='date', 
  'projection.enabled'='true', 
  'storage.location.template'='s3://<ELB로그 적재되는 S3 버킷 위치>/AWSLogs/<AWS ID>/elasticloadbalancing/ap-northeast-2/${day}', 
  'transient_lastDdlTime'='1749455682')

 

3. 쿼리 결과 확인

 

4. 새 쿼리를 열어 WHERE 를 이용해 특정 일자 '2025/06/09' 의 ELB 엑세스만 추출

SELECT * FROM "default"."alb_access_logs" WHERE day = '2025/06/09'

 

5. Download results CSV로 다운로드

 

반응형