본문 바로가기
AWS

[AWS] Athena로 S3에 적재된 로그 검색하기

by chan10 2023. 4. 18.

Athena는 길고 복잡한 로그를 SQL문을 통해 DB형식으로 검색해서 가독성 및 검색 기능을 향상 시켜주는 서비스입니다.

수백/수천개의 압축파일로 저장되어 있는 로그 파일을 한번에 볼 수 있도록 하고 원하는 조건에 따른 데이터 검색도 가능해 매우 유용합니다.

데이터 베이스 생성 -> 테이블 / 컬럼 생성 -> 검색 순서로 진행됩니다.

 

쿼리 결과를 저장할 버킷을 선택 후 저장합니다.

 

Access Log를 조회하기 전 데이터베이스, 테이블을 생성해야 합니다.

CREATE DATABASE 명령어로 Access Log를 다루는 데이터베이스를 생성합니다.

CREATE DATABASE alb_log_db;

 

생성한 데이터 베이스를 선택하고 아래의 쿼리문을 입력 실행하면 테이블이 생성됩니다.

컬럼은 ALB Access Log에 출력되는 항목들을 각각 컬럼으로 생성하여 쿼리문 조회 시 이용될 수 있도록 합니다.

(아래 코드문에서 주석은 지우고 적용하기..!!)

CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs_partition_projection (
	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
)
PARTITIONED BY(YEAR INT, MONTH INT, DAY INT) -- 파티션 컬럼
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://front-alb-log/front-alb-1/AWSLogs/172535786537/elasticloadbalancing/ap-northeast-2/' -- 로그 파일이 있는 경로
TBLPROPERTIES (
	'projection.enabled' = 'true',
	-- projection 사용하기
	'projection.day.digits' = '2',
	-- 일짜를 01, 02, 03 형식으로 사용
	'projection.day.range' = '01,31',
	-- 01일에서 31일 까지
	'projection.day.type' = 'integer',
	-- int 타입
	'projection.month.digits' = '2',
	-- 월별을 01, 02, 03 형식으로 사용
	'projection.month.range' = '01,12',
	-- 01월에서 12월 까지
	'projection.month.type' = 'integer',
	-- int 타입
	'projection.year.digits' = '4',
	-- 년도를 4글자로 사용
	'projection.year.range' = '2021,2023',
	-- 2021 ~ 2023 까지 스캔 
	'projection.year.type' = 'integer',
	-- int 타입
	"storage.location.template" = "s3://front-alb-log/front-alb-1/AWSLogs/172535786537/elasticloadbalancing/ap-northeast-2/${year}/${month}/${day}" -- 경로 형식
)

 

테이블 생성이 완료되었다면 SELECT문을 이용해 로그를 조회할 수 있습니다.

로그 조회 시 조건을 지정하여 조회할 수 있으며 아래 사진은 특정 IP에서 호출한 로그만 조회하는 쿼리문 입니다.

 

[참고사이트]

https://aws.amazon.com/ko/premiumsupport/knowledge-center/athena-analyze-access-logs/

https://docs.aws.amazon.com/ko_kr/athena/latest/ug/partition-projection-supported-types.html

https://inpa.tistory.com/entry/AWS-%F0%9F%93%9A-Athena%EB%A1%9C-ELBALB-%EB%A1%9C%EA%B7%B8-%EA%B0%84%ED%8E%B8%ED%95%98%EA%B2%8C-%EB%B6%84%EC%84%9D%ED%95%98%EA%B8%B0