0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Fluentd を Amazon Linux 2023 に導入してログを Amazon S3 に送信させるには

0
Last updated at Posted at 2026-03-10

概要

案件で、EC2サーバー(Amazon Linux 2023)のログを収集して保管するという要件があり、そこで Fluentdを活用しました。Amazon Linux 2023 環境において Fluentd をインストール・設定し、システムログをリアルタイムで Amazon S3 に送信するための手順を記載しています。導入に際して詰まった点や解決方法についてもまとめました。


前提条件

  • OS: Amazon Linux 2023
  • AWS権限: 対象S3バケットへの読み書き権限を持つIAMロール
  • ネットワーク: EC2インスタンスがS3 APIにアクセス可能

インストール手順

1. Fluentd パッケージのインストール

# fluent-package をインストール
curl -fsSL https://fluentd.cdn.cncf.io/sh/install-amazon2023-fluent-package6-lts.sh | sh

期待される出力例:

Installed:
fluent-package-6.0.1-1.amzn2023.x86_64

Installation completed. Happy Logging!
fluent-package 6.0.1 fluentd 1.19.1

2. S3 プラグインのインストール

sudo /opt/fluent/bin/fluent-gem install fluent-plugin-s3

期待される出力:

Fetching fluent-plugin-s3-1.8.3.gem
Successfully installed fluent-plugin-s3-1.8.3

設定手順

3. ログ出力ディレクトリの作成と権限設定

# バッファとログ用ディレクトリを作成
sudo mkdir -p /var/log/fluent/buffer
sudo mkdir -p /var/run/fluent

# root ユーザーが所有するように設定
sudo chown -R root:root /var/log/fluent
sudo chown -R root:root /var/run/fluent
sudo chmod -R 755 /var/log/fluent
sudo chmod -R 755 /var/run/fluent

注意点:

  • fluent:fluent ユーザーが存在しない場合は root:root で設定してください
  • バッファディレクトリへの書き込み権限が必須です

4. Fluentd 設定ファイルの作成

/etc/fluent/fluentd.conf に以下の内容を記載します。

設定ファイルの例:

  • バケット名を your-log-bucket に置き換えてください
  • サーバー名を YourServerName に置き換えてください
  • リージョンを your-region に置き換えてください

(1) syslog 出力設定例

# ==========================================
# 1. /var/log/messages
# ==========================================
<source>
  @type tail
  path /var/log/messages
  pos_file /var/log/fluent/messages.pos
  tag syslog.messages
  <parse>
    @type syslog
  </parse>
</source>

<match syslog.messages>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/system-logs/messages/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-messages
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

(2) secure ログ出力設定例

# ==========================================
# 2. /var/log/secure
# ==========================================
<source>
  @type tail
  path /var/log/secure
  pos_file /var/log/fluent/secure.pos
  tag syslog.secure
  <parse>
    @type syslog
  </parse>
</source>

<match syslog.secure>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/system-logs/secure/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-secure
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

(3) systemd journal 出力設定例

# ==========================================
# 3. journalctl (systemd journal)
# ==========================================
<source>
  @type systemd
  tag journal
  read_from_head true
  <storage>
    @type local
    persistent true
    path /var/log/fluent/systemd.pos
  </storage>
  <entry>
    field_map {"MESSAGE": "message", "_SYSTEMD_UNIT": "unit", "_PID": "pid", "_HOSTNAME": "hostname"}
  </entry>
</source>

<match journal>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/system-logs/journal/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-journal
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

(4) その他ログファイル出力設定例

# ==========================================
# 4. /var/log/配下のその他のログファイル
# ==========================================
# Amazon関連のログ
<source>
  @type tail
  path /var/log/amazon/**/*.log
  pos_file /var/log/fluent/amazon.pos
  tag varlog.amazon
  <parse>
    @type none
  </parse>
</source>

# audit ログ
<source>
  @type tail
  path /var/log/audit/audit.log
  pos_file /var/log/fluent/audit.pos
  tag varlog.audit
  <parse>
    @type none
  </parse>
</source>

# chrony ログ
<source>
  @type tail
  path /var/log/chrony/*.log
  pos_file /var/log/fluent/chrony.pos
  tag varlog.chrony
  <parse>
    @type none
  </parse>
</source>

# dynatrace ログ
<source>
  @type tail
  path /var/log/dynatrace/**/*.log
  pos_file /var/log/fluent/dynatrace.pos
  tag varlog.dynatrace
  <parse>
    @type none
  </parse>
</source>

# sssd ログ
<source>
  @type tail
  path /var/log/sssd/*.log
  pos_file /var/log/fluent/sssd.pos
  tag varlog.sssd
  <parse>
    @type none
  </parse>
</source>

# その他の /var/log 直下の .log ファイル
<source>
  @type tail
  path /var/log/*.log
  pos_file /var/log/fluent/varlog.pos
  tag varlog.misc
  path_key filepath
  <parse>
    @type none
  </parse>
</source>

# varlogタグのログをS3に出力(ログ種別ごとにパスを分ける)
<match varlog.amazon>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/application-logs/amazon/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-amazon
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

<match varlog.audit>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/application-logs/audit/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-audit
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

<match varlog.chrony>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/application-logs/chrony/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-chrony
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

<match varlog.dynatrace>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/application-logs/dynatrace/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-dynatrace
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

<match varlog.sssd>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/application-logs/sssd/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-sssd
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

<match varlog.misc>
  @type s3
  s3_bucket your-log-bucket
  s3_region your-region
  path YourServerName/application-logs/misc/%Y/%m/%d/
  s3_object_key_format %{path}%{time_slice}_%{index}.%{file_extension}
  store_as gzip
  check_object false
  check_bucket false
  <buffer time>
    @type file
    path /var/log/fluent/buffer/s3-misc
    timekey 600
    timekey_wait 1m
  </buffer>
  <format>
    @type json
  </format>
</match>

5. 設定ファイルの検証

sudo /opt/fluent/bin/fluentd --dry-run -c /etc/fluent/fluentd.conf

期待される出力:

[info]: parsing config file is succeeded path="/etc/fluent/fluentd.conf"
[info]: finished dry run mode

systemd サービス設定

6. root ユーザー実行の設定

systemd のオーバーライド設定を作成し、Fluentd を root で実行するよう指定します。

# ディレクトリを作成
sudo mkdir -p /etc/systemd/system/fluentd.service.d/

# オーバーライド設定ファイルを作成
echo "[Service]" | sudo tee /etc/systemd/system/fluentd.service.d/override.conf
echo "User=root" | sudo tee -a /etc/systemd/system/fluentd.service.d/override.conf
echo "Group=root" | sudo tee -a /etc/systemd/system/fluentd.service.d/override.conf

重要: このステップが無いと、非 root ユーザーで起動され、ファイル操作の権限エラーが発生します。

7. systemd 設定のリロードと サービス起動

# 設定を再読み込み
sudo systemctl daemon-reload

# サービスを起動
sudo systemctl start fluentd.service

# ステータス確認
sudo systemctl status fluentd.service

期待される出力:

● fluentd.service - fluentd: All in one package of Fluentd
     Loaded: loaded (/usr/lib/systemd/system/fluentd.service; disabled; preset: disabled)
    Drop-In: /etc/systemd/system/fluentd.service.d
             └─override.conf
     Active: active (running) since Mon 2026-02-02 08:08:14 UTC; 1min ago

AWS IAM 権限設定

8. IAM ロールポリシーの修正

重要: Fluentd が S3 にアクセスするには、以下の権限が必要です。

your-account-id を AWS アカウント ID に、your-log-bucket を S3 バケット名に置き換えてください。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "AccessToS3BucketAndObjects",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:PutObjectAcl",
                "s3:PutObjectVersionAcl",
                "s3:PutObjectTagging",
                "s3:PutObjectVersionTagging",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::your-log-bucket",
                "arn:aws:s3:::your-log-bucket/*"
            ]
        }
    ]
}

注意点:

  • s3:ListBucket アクションには、末尾に /* が無い バケットレベルのARN を指定してください
  • オブジェクトレベルのリソース (/*) にのみ権限があると、ListBucket エラーが発生します

ログの確認

9. リアルタイムログ確認

sudo tail -f /var/log/fluent/fluentd.log

期待される出力:

2026-02-02 08:12:11 +0000 [info]: #0 following tail of /var/log/messages
2026-02-02 08:12:11 +0000 [info]: #0 following tail of /var/log/secure
2026-02-02 08:12:11 +0000 [info]: #0 fluentd worker is now running worker=0

S3 エラーが出ないこと確認:

AccessDenied エラーが表示されないことが正常動作の指標です

自動起動設定

10. サービスの自動起動設定

# サービスをシステム起動時に自動起動するよう設定
sudo systemctl enable fluentd.service

# 設定確認
sudo systemctl is-enabled fluentd.service

期待される出力:

enabled

S3 のディレクトリ構造

Fluentd により以下のディレクトリ構造で S3 にログが保存されます。

s3://your-log-bucket/YourServerName/
├── system-logs/
│   ├── messages/YYYY/MM/DD/
│   ├── secure/YYYY/MM/DD/
│   └── journal/YYYY/MM/DD/
└── application-logs/
    ├── amazon/YYYY/MM/DD/
    ├── audit/YYYY/MM/DD/
    ├── chrony/YYYY/MM/DD/
    ├── dynatrace/YYYY/MM/DD/
    ├── sssd/YYYY/MM/DD/
    └── misc/YYYY/MM/DD/

トラブルシューティング

A. サービスが起動しない場合

# 詳細エラーを確認
sudo journalctl -xeu fluentd.service

# dry-run で設定を検証
sudo /opt/fluent/bin/fluentd --dry-run -c /etc/fluent/fluentd.conf

B. S3 へのアクセスが拒否される場合

  • IAM ロールポリシーを確認し、s3:ListBucket 権限がバケットレベルで付与されているか確認
  • EC2 インスタンスに正しい IAM ロールがアタッチされているか確認
  • バケット名とリージョンが正確に設定されているか確認

C. ファイル権限エラーが発生する場合

# ディレクトリ権限を再確認
ls -la /var/log/fluent/
ls -la /var/run/fluent/

# 所有者を root に変更
sudo chown -R root:root /var/log/fluent /var/run/fluent

まとめ

本手順により、Fluentd を使用してシステムログを Amazon S3 にリアルタイムで連携できます。ご参考になれば幸いです。

重要なポイント:

  1. ✅ root ユーザー実行の設定が必須
  2. ✅ IAM ロールポリシーでバケットレベルの s3:ListBucket 権限が必須
  3. ✅ ディレクトリ権限の適切な設定がファイル操作の前提条件
  4. --dry-run で設定検証を行うことで、本番環境での失敗を防止可能
  5. ✅ バケット名、サーバー名、リージョンをお客様環境に合わせて置き換え必須

参考:

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?