1
0

More than 3 years have passed since last update.

EC2(AmazonLinux2)のログをKinesisFireHoseに流してS3へ蓄積する

Last updated at Posted at 2020-02-15

まずはEC2からKinesisへのfullアクセスロールを用意。

ついでS3バケットとFireHoseのストリーム作成。FireHoseのIAMロールはここで作成できる。

以下、EC2の設定。
ここではApacheへのアクセスログを題材としている。

$ sudo yum -y install httpd
$ sudo systemctl enable httpd
$ sudo systemctl start httpd

$ sudo vi /etc/httpd/conf/httpd.conf

:set numberとすることで行数を表示できる。:197で197行目に飛べる。

197行目に追記
LogFormat "time:%t\tforwardedfor:%{X-Forwarded-For}i\thost:%h\treq:%r\tstatus:%>s\tsize:%b\treferer:%{Referer}i\tua:%{User-Agent}i\treqtime_microsec:%D\tcache:%{X-Cache}o\truntime:%{X-Runtime}o\tvhost:%{Host}i" ltsv
219行目に追記
LogFormat "time:%t\tforwardedfor:%{X-Forwarded-For}i\thost:%h\treq:%r\tstatus:%>s\tsize:%b\treferer:%{Referer}i\tua:%{User-Agent}i\treqtime_microsec:%D\tcache:%{X-Cache}o\truntime:%{X-Runtime}o\tvhost:%{Host}i" ltsv
$ sudo systemctl restart httpd
$ sudo chmod 755 /var/log/httpd
$ sudo chmod 644 /var/log/httpd/access_log
$ sudo yum install –y https://s3.amazonaws.com/streaming-data-agent/aws-kinesis-agent-latest.amzn1.noarch.rpm

$ sudo vi /etc/aws-kinesis/agent.json
/etc/aws-kinesis/agent.json
{
  "cloudwatch.emitMetrics": false,
  "firehose.endpoint": "https://firehose.ap-northeast-1.amazonaws.com",
  "flows": [
    {
      "filePattern": "/var/log/httpd/access_log_ltsv",
      "deliveryStream": "作成したstream名"
    }
  ]
}
$ sudo systemctl restart aws-kinesis-agent
$ sudo systemctl enable aws-kinesis-agent
aws-kinesis-agent.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig aws-kinesis-agent on

$ sudo tail -f /var/log/aws-kinesis-agent/aws-kinesis-agent.log
2020-02-15 13:48:42.192+0000  (FileTailer[fh:xxxx:/var/log/httpd/access_log_ltsv].MetricsEmitter RUNNING) com.amazon.kinesis.streaming.agent.tailing.FileTailer [INFO] FileTailer[fh:xxxx:/var/log/httpd/access_log_ltsv]: Tailer Progress: Tailer has parsed 3 records (985 bytes), transformed 0 records, skipped 0 records, and has successfully sent 0 records to destination.
2020-02-15 13:48:42.198+0000  (Agent.MetricsEmitter RUNNING) com.amazon.kinesis.streaming.agent.Agent [INFO] Agent: Progress: 3 records parsed (985 bytes), and 0 records sent successfully to destinations. Uptime: 120058ms
2020-02-15 13:48:51.186+0000  (sender-0) com.amazon.kinesis.streaming.agent.UserDefinedCredentialsProvider [INFO] No custom implementation of credentials provider present in the config file
2020-02-15 13:49:12.193+0000  (FileTailer[fh:xxxx:/var/log/httpd/access_log_ltsv].MetricsEmitter RUNNING) com.amazon.kinesis.streaming.agent.tailing.FileTailer [INFO] FileTailer[fh:xxxx:/var/log/httpd/access_log_ltsv]: Tailer Progress: Tailer has parsed 3 records (985 bytes), transformed 0 records, skipped 0 records, and has successfully sent 3 records to destination.
2020-02-15 13:49:12.198+0000  (Agent.MetricsEmitter RUNNING) com.amazon.kinesis.streaming.agent.Agent [INFO] Agent: Progress: 3 records parsed (985 bytes), and 3 records sent successfully to destinations. Uptime: 150058ms

しばらくすると○ records sent successfully to destinationsとなりFireHose側に流れてきたことが確認できる。その後約5分してS3へデータが飛んだ。どうやらS3への送信は若干のラグがあるらしい。

Monitoring

image.png

追記

FireHoseで吸わせてS3に置いたtsv形式のログデータをAthenaからクエリをかけてみた。
参考) https://www.tdi.co.jp/miso/amazon-athena-s3-sql

Athenaコンソールの[create table]からs3を選択し、DB名、テーブル名を決めたのち、対象のバケットのアドレスを指定(今回のように日にち毎にファイルが複数に別れても指定したディレクトリ配下のデータを全て整形してくれるようだ。)

ファイル形式をtsvで選択し、カラム名とデータ型を一挙に指定する。(カラム数が多いのならbulkを選んでやったほうが楽。)

作成後、SELECT * FROM テーブル名で一覧表示

image.png

ちゃんと出てきた。
ログファイルのフォーマットを自分で整形してあげれば、今後はEC2吐くログをAthenaで読ませることができそう。


参考

1
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
1
0