Skip to content

Instantly share code, notes, and snippets.

@rioner
rioner / lambda_function.py
Last active October 11, 2018 12:44
VPC Flow LogsをAthenaで分析できるようにpartitionを区切るやつ
import boto3
import datetime
def lambda_handler(event, context):
# タイムゾーンの生成
JST = timezone(timedelta(hours=+9), 'JST')
now = datetime.now(JST)
print(now.strftime("%Y/%m/%d")) #2018/10/02
CREATE DATABASE データベース名
CREATE EXTERNAL TABLE IF NOT EXISTS データベース名.テーブル名 (
version int,
account string,
interfaceid string,
sourceaddress string,
destinationaddress string,
sourceport int,
destinationport int,
protocol int,
numpackets int,
ALTER TABLE ap_northeast_1
ADD PARTITION (dt='2018/10/01')
location 's3://バケット名/AWSLogs/アカウント番号/vpcflowlogs/ap-northeast-1/2018/10/01';
SELECT * FROM "データベース名"."テーブル名"
where
dt = '2018/10/01'
order by numbytes desc limit 10
try:
# 処理
expect Exception as e:
if str(e) = 'エラー文':
pass
else:
print(e)
try:
# 処理
expect client.expections.ClientError as e:
pass