Skip to content

Instantly share code, notes, and snippets.

@rioner
Last active October 11, 2018 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rioner/fe5d53efac1c241d4abfcb8dec125ec1 to your computer and use it in GitHub Desktop.
Save rioner/fe5d53efac1c241d4abfcb8dec125ec1 to your computer and use it in GitHub Desktop.
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
client = boto3.client('athena')
response = client.start_query_execution(
QueryString="ALTER TABLE ap_northeast_1\nADD PARTITION (dt='" + now.strftime("%Y%m%d") + "')\nlocation 's3://バケット名/AWSLogs/アカウント番号/vpcflowlogs/ap-northeast-1/" + now.strftime("%Y/%m/%d") + "';",
QueryExecutionContext={
'Database': 'データベース名'
},
ResultConfiguration={
'OutputLocation': 's3://aws-athena-query-results-アカウント番号-ap-northeast-1/'
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment