如何使用 Athena 分析 Amazon VPC 流程日誌?
我想使用 Amazon Athena 分析 Amazon Virtual Private Cloud (Amazon VPC) 流程日誌。
解決方法
您可以使用 Athena 主控台查詢編輯器建立資料庫、為VPC 流程日誌建立資料表,並執行範例查詢。接著,使用流程日誌分析網路流量模式,並找出整個 Amazon VPC 網路中的威脅與風險。
使用 Athena 主控台查詢編輯器建立資料庫
開啟 Athena 主控台查詢編輯器,然後執行以下命令:
CREATE DATABASE test_db_vpclogs;
注意: 將 test_db_vpclogs 替換為您要用於資料庫的名稱。
重要: 最佳實務是在與存放流程日誌的 Amazon Simple Storage Service (Amazon S3) 儲存貯體相同的 AWS 區域中建立資料庫。
在資料庫中為流程日誌建立資料表
開啟 Athena console (Athena 主控台)。接著,在導覽窗格中選擇 Query editor (查詢編輯器)。在 Athena 主控台查詢編輯器中,執行類似以下範例的命令:
CREATE EXTERNAL TABLE IF NOT EXISTS test_table_vpclogs ( version int, account_id string, interface_id string, srcaddr string, dstaddr string, srcport int, dstport int, protocol bigint, packets bigint, bytes bigint, start bigint, `end` bigint, action string, log_status string, vpc_id string, subnet_id string, instance_id string, tcp_flags int, type string, pkt_srcaddr string, pkt_dstaddr string, az_id string, sublocation_type string, sublocation_id string, pkt_src_aws_service string, pkt_dst_aws_service string, flow_direction string, traffic_path int ) PARTITIONED BY (region string, date string, hour string) ROW FORMAT SERDE 'org.apache.hadoop.hive.ql.io.parquet.serde.ParquetHiveSerDe' STORED AS INPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.parquet.MapredParquetOutputFormat' LOCATION 's3://amzn-s3-demo-bucket/prefix/AWSLogs/{account_id}/vpcflowlogs/' TBLPROPERTIES ( "EXTERNAL"="true", "skip.header.line.count" = "1", "projection.enabled" = "true", "projection.region.type" = "enum", "projection.region.values" = "us-east-1,us-west-2,ap-south-1,eu-west-1", "projection.date.type" = "date", "projection.date.range" = "2021/01/01,NOW", "projection.date.format" = "yyyy/MM/dd", "projection.hour.type" = "integer", "projection.hour.range" = "00,23", "projection.hour.digits" = "2", "storage.location.template" = "s3://amzn-s3-demo-bucket/prefix/AWSLogs/${account_id}/vpcflowlogs/${region}/${date}/${hour}" )
注意: 將 test_table_vpclogs 替換為您要用於資料表的名稱。
請務必修改 LOCATION 參數,使其指向包含日誌資料的 Amazon S3 儲存貯體。將 projection.region.values 更新為您存有 VPC 流程日誌的 AWS 區域。將 projection.date.range 的值設定為資料中最早可用日誌的日期。例如,如果日誌起始日期為 2021 年 1 月 1 日,請將 projection.date.range 設定為 '2021/01/01,NOW'。請依據日誌的實際起始日期修改此值,以確保資料投影準確。上述命令使用分區投影建立資料表、將資料表分區,並自動填入各分區。如果投影的分區不存在於 Amazon S3 中,Athena 仍會投影該分區。最佳實務是在查詢中使用分區屬性。
針對流程日誌資料表執行 SQL 陳述式
開啟 Athena console (Athena 主控台)。接著,在導覽窗格中選擇 Query editor (查詢編輯器)。使用 Athena 主控台查詢編輯器,對資料表執行 SQL 陳述式。您可以儲存查詢、檢視先前的查詢,或下載 .csv 格式的查詢結果。
範例查詢
注意: 在以下範例查詢中,將 test_table_vpclogs 替換為您的資料表名稱。請根據您的使用案例修改欄值及其他變數。
若要依時間順序檢視指定期間內的前 100 筆流程日誌項目,請執行以下查詢:
SELECT * FROM test_table_vpclogs WHERE day >= '2021/02/01' AND day < '2021/02/28' ORDER BY day ASC LIMIT 100;
若要檢視指定期間內收到最多 HTTPS 封包的 10 部目的地伺服器,請執行以下查詢:
SELECT SUM(packets) AS packetcount, dstaddr FROM test_table_vpclogs WHERE dstport = 443 AND day >= '2021/03/01' AND day < '2021/03/31' GROUP BY dstaddr ORDER BY packetcount DESC LIMIT 10;
若要檢查系統在指定時間範圍內建立的日誌,請執行以下查詢:
SELECT interface_id, srcaddr, action, protocol, to_iso8601(from_unixtime(start)) AS start_time, to_iso8601(from_unixtime("end")) AS end_time FROM test_table_vpclogs WHERE DAY >= '2021/04/01' AND DAY < '2021/04/30';
若要檢視指定時間範圍內特定來源 IP 位址的流程日誌,請執行以下查詢:
SELECT *FROM test_table_vpclogs WHERE srcaddr = '10.117.1.22' AND day >= '2021/02/01' AND day < '2021/02/28';
若要列出指定時間範圍內拒絕的 TCP 連線,請執行以下查詢:
SELECT day, interface_id, srcaddr, action, protocol FROM test_table_vpclogs WHERE action = 'REJECT' AND protocol = 6 AND day >= '2021/02/01' AND day < '2021/02/28' LIMIT 10;
若要檢視以 10.117 開頭之 IP 位址範圍的流程日誌,請執行以下查詢:
SELECT *FROM test_table_vpclogs WHERE split_part(srcaddr,'.', 1)='10' AND split_part(srcaddr,'.', 2) ='117'
若要檢視指定時間範圍內特定目的地 IP 位址的流程日誌,請執行以下查詢:
SELECT *FROM test_table_vpclogs WHERE dstaddr = '10.0.1.14' AND day >= '2021/01/01' AND day < '2021/01/31'
相關資訊
This article was reviewed and updated on 2026-02-20.
相關內容
已提問 1 個月前
AWS 官方已更新 10 個月前
