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 ログのデータベースとテーブルを作成する

次の手順を実行します。

  1. Athena コンソールを開きます。
  2. クエリエディタで次のコマンドを実行して、データベースを作成します。
    CREATE DATABASE alb_db
    注: Amazon S3 バケットと同じ AWS リージョンにデータベースを作成するのがベストプラクティスです。
  3. このデータベースで、Application Load Balancer ログ用の 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 を開始日に置き換えてください。クエリのパフォーマンスを向上させるためには、パーティションプロジェクションを使用してテーブルを作成します。
  4. AWS Glue データカタログに保存されているパーティションを含むテーブルを作成するには、次のクエリを実行します。
    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 ロケーションに置き換えてください。
  5. (オプション) パーティションをロードするには、次の 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 クローラーを使用しないでください。
  6. ナビゲーションペインの [テーブル] で、[テーブルをプレビュー] を選択します。
  7. [結果] ウィンドウには、Application Load Balancer のアクセスログのデータが表示されます。
    注: テーブルで SQL ステートメントを実行するには、クエリエディタを使用してください。

関連情報

アクセスログのエントリ

Application Load Balancer のログに対するクエリ実行

AWS公式
AWS公式更新しました 8ヶ月前
コメントはありません

関連するコンテンツ