0
0

More than 1 year has passed since last update.

Athenaを使ってCloudTrailログを検索する〜パーティション追加

Last updated at Posted at 2021-11-19

1、Athenaテーブルを作成する

image.png

ここで場所を指定して直接作成するか、SQLをコピーしてAthenaで作成する
image.png

今回はパーティションを追加するため、SQLをコピーして、パーティションを追加してAthenaで作成する

create_table.sql
CREATE EXTERNAL TABLE cloudtrail_logs_aws_service_log (
    eventVersion STRING,
    userIdentity STRUCT<
        type: STRING,
        principalId: STRING,
        arn: STRING,
        accountId: STRING,
        invokedBy: STRING,
        accessKeyId: STRING,
        userName: STRING,
        sessionContext: STRUCT<
            attributes: STRUCT<
                mfaAuthenticated: STRING,
                creationDate: STRING>,
            sessionIssuer: STRUCT<
                type: STRING,
                principalId: STRING,
                arn: STRING,
                accountId: STRING,
                userName: STRING>>>,
    eventTime STRING,
    eventSource STRING,
    eventName STRING,
    awsRegion STRING,
    sourceIpAddress STRING,
    userAgent STRING,
    errorCode STRING,
    errorMessage STRING,
    requestParameters STRING,
    responseElements STRING,
    additionalEventData STRING,
    requestId STRING,
    eventId STRING,
    resources ARRAY<STRUCT<
        arn: STRING,
        accountId: STRING,
        type: STRING>>,
    eventType STRING,
    apiVersion STRING,
    readOnly STRING,
    recipientAccountId STRING,
    serviceEventDetails STRING,
    sharedEventID STRING,
    vpcEndpointId STRING
)
COMMENT 'CloudTrail table for aws-service-log bucket'
PARTITIONED BY(region string, year string, month string) #パーティション
ROW FORMAT SERDE 'com.amazon.emr.hive.serde.CloudTrailSerde'
STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat'
OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat'
LOCATION 's3://<BUCKET-NAME>/AWSLogs/<account-id>/CloudTrail/'
TBLPROPERTIES ('classification'='cloudtrail');

完了したらパーティションが確認できる
image.png

2、パーティションをロードする

下記のコマンドを実行し、データを全部ロードする

load_all_data.sql
MSCK REPAIR TABLE <table-name>

image.png

全部ロードしたくない場合は下記のコマンドで指定するパーティションをロードする

load_data.sql
ALTER TABLE <table-name> 
ADD PARTITION (region='ap-northeast-1', year='2021', month='10') 
location 's3://<BUCKET-NAME>/AWSLogs/<account-id>/CloudTrail/ap-northeast-1/2021/10/'

3、ログを検索する

image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0