Amazon Athena를 사용하여 Application Load Balancer 액세스 로그를 분석하려면 어떻게 해야 하나요?
Amazon Athena를 사용하여 Application Load Balancer 액세스 로그를 분석하고 싶습니다.
간략한 설명
Elastic Load Balancing은 기본적으로 액세스 로그를 활성화하지 않습니다. 액세스 로그를 활성화하는 경우 Amazon Simple Storage Service(Amazon S3) 버킷을 지정해야 합니다. Athena는 Application Load Balancer 및 Classic Load Balancer 액세스 로그를 분석하여 Amazon S3 버킷에 로그를 저장합니다.
**참고: ** 이 해결 방법은 Application Load Balancer에만 적용됩니다.
해결 방법
Application Load Balancer 로그용 데이터베이스 및 테이블 생성
다음 단계를 완료합니다.
-
Athena 콘솔을 엽니다.
-
데이터베이스를 생성하려면 Query Editor에서 다음 명령을 실행합니다.
CREATE DATABASE alb_db
**참고: **Amazon S3 버킷과 동일한 AWS 리전에 데이터베이스를 생성하는 것이 가장 좋습니다.
-
데이터베이스에서 Application Load Balancer logs에 대한 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 int, 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, conn_trace_id string ) PARTITIONED BY(day 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-alb-logs-directory>/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/' TBLPROPERTIES( "projection.enabled" = "true", "projection.day.type" = "date", "projection.day.range" = "2022/01/01,NOW", "projection.day.format" = "yyyy/MM/dd", "projection.day.interval" = "1", "projection.day.interval.unit" = "DAYS", "storage.location.template" = "s3://<your-alb-logs-directory>/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/${day}" )
**참고: ** 앞의 쿼리에서 테이블 이름과 S3 위치를 테이블 이름과 S3 위치로 바꿉니다. projection.day.range의 경우 2022/01/01을 시작 날짜로 바꿉니다. 쿼리 성능을 개선하려면 파티션 프로젝션으로 테이블을 만드세요.
-
AWS Glue Data Catalog에 저장된 파티션이 있는 테이블을 생성하려면 다음 쿼리를 실행합니다.
CREATE EXTERNAL TABLE IF NOT EXISTS alb_logs_partitioned(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 int, 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, conn_trace_id string ) PARTITIONED BY(day 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-alb-logs-directory>/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/'
**참고: ** 앞의 쿼리에서 테이블 이름과 S3 위치를 테이블 이름과 S3 위치로 바꿉니다.
-
(선택 사항) 파티션을 로드하려면 다음 ALTER TABLE ADD PARTITION DDL 문을 실행합니다.
ALTER TABLE alb_logs_partitioned ADD PARTITION (day = '2022/05/21') LOCATION's3://<your-alb-logs-directory>/AWSLogs/<ACCOUNT-ID>/elasticloadbalancing/<REGION>/2022/05/21/'
**참고: ** 테이블이 파티션 투영을 사용하는 경우 파티션 로드 단계를 건너뜁니다. Application Load Balancer 로그에 AWS Glue 크롤러를 사용하지 마세요.
-
탐색 창의 테이블에서 테이블 미리 보기를 선택합니다.
-
Results 창에서 Application Load Balancer 접속 로그의 데이터를 봅니다.
**참고: ** 테이블에서 SQL 문을 실행하려면 Query editor를 사용합니다.
관련 정보
관련 콘텐츠
- 질문됨 2년 전lg...
- AWS 공식업데이트됨 일 년 전
- AWS 공식업데이트됨 3달 전