Knowledge Center Monthly Newsletter - March 2025
Stay up to date with the latest from the Knowledge Center. See all new and updated Knowledge Center articles published in the last month and re:Post’s top contributors.
如何使用 Amazon Athena 分析 VPC 流日志?
我想使用 Amazon Athena 分析我的 Amazon Virtual Private Cloud(Amazon VPC)流日志。
简短描述
使用 Amazon VPC 流日志分析网络流量模式,并识别 Amazon VPC 网络中的威胁和风险。
解决方法
使用 Athena 分析 Amazon VPC 流日志
要使用 Athena 分析 Amazon VPC 流日志,请完成以下步骤:
-
使用 Amazon Athena 控制台查询编辑器运行以下命令来创建数据库。将 test_db_vpclogs 替换为您数据库的名称:
CREATE DATABASE test_db_vpclogs;
**重要说明:**最佳做法是在与存储流日志的 Amazon Simple Storage Service(Amazon S3)存储桶相同的 AWS 区域中创建数据库。
-
在数据库中,运行以下命令为 VPC 流日志创建表。将 test_table_vpclogs 替换为您表的名称。此外,修改 LOCATION 参数以指向包含您的日志数据的 Amazon S3 存储桶:
CREATE EXTERNAL TABLE `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, `day` string) ROW FORMAT DELIMITED FIELDS TERMINATED BY ' ' STORED AS INPUTFORMAT 'org.apache.hadoop.mapred.TextInputFormat' OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' LOCATION 's3://awsexamplebucket/awsexampleprefix/awsexamplelogs/1111222233334444/vpcflowlogs/' TBLPROPERTIES ( 'projection.day.format'='yyyy/MM/dd', 'projection.day.range'='2021/01/01,NOW', 'projection.day.type'='date', 'projection.enabled'='true', 'projection.region.type'='enum', 'projection.region.values'='us-east-1,us-west-2,ap-south-1,eu-west-1', 'skip.header.line.count'='1', 'storage.location.template'='s3://awsexamplebucket/awsexampleprefix/awsexamplelogs/1111222233334444/vpcflowlogs/${region}/${day}' )
**注意:**前面的命令使用分区投影来创建表、对表进行分区并自动填充分区。如果在 Amazon S3 中不存在投影分区,则 Athena 仍会对该分区进行投影。最佳做法是在查询中使用分区的属性。
-
使用 Amazon 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;
要查看指定时间段内收到前 10 个 HTTP 数据包的服务器,请运行类似于以下的查询:
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;
前面的查询计算了在 HTTPS 端口 443 上收到的数据包的数量,并且按照目标 IP 地址对其进行分组。然后,它会返回前一周的前 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'
相关信息
Analyzing VPC Flow Logs using Amazon Athena, and Amazon QuickSight

相关内容
- AWS 官方已更新 1 年前
- AWS 官方已更新 2 个月前
- AWS 官方已更新 1 年前