如何使用 Amazon Athena 分析我的 Application Load Balancer 存取日誌?

3 分的閱讀內容
0

我想使用 Amazon Athena 分析我的 Application Load Balancer 存取日誌。

簡短說明

根據預設,Elastic Load Balancing (ELB) 不會啟用存取日誌啟用存取日誌時,請務必指定 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 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 位置。
  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 官方已更新 9 個月前