New user sign up using AWS Builder ID
New user sign up using AWS Builder ID is currently unavailable on re:Post. To sign up, please use the AWS Management Console instead.
Amazon Athena で Application Load Balancer のアクセスログを分析する方法を教えてください。
所要時間3分
1
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 コンソールを開きます。
- クエリエディタで次のコマンドを実行して、データベースを作成します。
注: Amazon S3 バケットと同じ AWS リージョンにデータベースを作成するのがベストプラクティスです。CREATE DATABASE alb_db
- このデータベースで、Application Load Balancer ログ用の alb_logs テーブルを作成します。
クエリの例:
注: 前出のクエリでは、テーブル名と S3 ロケーションを、実際のテーブル名と S3 ロケーションに置き換えてください。projection.day.range の場合は、2022/01/01 を開始日に置き換えてください。クエリのパフォーマンスを向上させるためには、パーティションプロジェクションを使用してテーブルを作成します。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}" )
- AWS Glue データカタログに保存されているパーティションを含むテーブルを作成するには、次のクエリを実行します。
注: 前出のクエリでは、テーブル名と S3 ロケーションを、実際のテーブル名と S3 ロケーションに置き換えてください。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>/'
- (オプション) パーティションをロードするには、次の ALTER TABLE ADD PARTITION DDL ステートメントを実行します。
注: テーブルがパーティションプロジェクションを使用している場合は、パーティションをロードする手順をスキップしてください。Application Load Balancer のログに AWS Glue クローラーを使用しないでください。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 のアクセスログのデータが表示されます。
注: テーブルで SQL ステートメントを実行するには、クエリエディタを使用してください。
関連情報
コメントはありません
関連するコンテンツ
- 質問済み 3ヶ月前lg...
- 質問済み 4ヶ月前lg...
- AWS公式更新しました 10ヶ月前
- AWS公式更新しました 4ヶ月前