AWS EC2のログをCloudWatch Logsに出力

  • 2019年7月27日
  • 優, AWS
  • 5520view
優

 

個人開発したりしていて思うのですが、WEBの技術は一つ一つの膨大な単純作業の積み重ねだなと感じます。

はい!

そんなわけでEC2のApacheログをCloudWatchに出力しようと思います。

 

環境

  • ALB配下のAWS EC2

 

フロー

  1. EC2 Apacheのログ設定
  2. EC2ロールの作成と割り当て
    EC2にCloudWatchにアップロードできる権限を割り当てる
  3. awslogsをEC2にインストールし出力

 

 

EC2 Apacheのログ設定

 

remoteipモジュールがあるか確認

# httpd -M | grep remoteip
 remoteip_module (shared)

 

グローバル設定ファイル

# vi /etc/httpd/conf/httpd.conf

・・・

<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
+   LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-Proto}i\"" elb-customlog

 

 

# vi /etc/httpd/conf.d/GlobalSetting.conf

# ファビコンのログを出さない
SetEnvIf Request_URI "\.(ico)$" nolog

# 画像やJSのログを出さない
SetEnvIf Request_URI "\.(gif|jpg|png|ico|jpeg|js|css)$" nolog


CustomLog logs/access_log common env=!nolog

 

バーチャルホスト設定ファイル

# vi  /etc/httpd/conf.d/www.example.net.conf



<VirtualHost *:80>
  ServerName www.example.net
  DocumentRoot /var/www/vhosts/www.example.net/httpdocs
  ErrorLog logs/www.example.net-error.log
  CustomLog logs/www.example.net-access.log elb-customlog env=!nolog

  ## ALB対策
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{HTTP:X-Forwarded-Port} !^443$
    RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
    RewriteRule ^(.*)?$ https://www.example.net.jp$1 [R=301,L]
  </IfModule>

  <Directory "/var/www/vhosts/www.example.net/httpdocs">
    AllowOverride All
    Require all granted
  </Directory>
</VirtualHost>

 

 

構文チェック

# httpd -t
Syntax OK

 

Apacheに設定の反映

# systemctl restart httpd
# systemctl status httpd

 

EC2ロール作成とCloudWatch操作できるポリシーの割り当て

 

CloudWatchフルアクセスポリシーをEC2ロールにつける

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "logs:*"
            ],
            "Effect": "Allow",
            "Resource": "*"
        }
    ]
}

 

 

awslogsのインストール

 

# sudo yum install -y awslogs

 

リージョン変更 東京リージョンへ

]# vi /etc/awslogs/awscli.conf


[plugins]
cwlogs = cwlogs
[default]
region = us-east-1

↓変更

[plugins]
cwlogs = cwlogs
[default]
region = ap-northeast-1

 

 

ログ出力対象ファイルの設定

# vi /etc/awslogs/awslogs.conf

・・・

[/var/log/messages]
datetime_format = %b %d %H:%M:%S
file = /var/log/messages
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /var/log/messages


※下記を追加

[HttpdAccessLog]
datetime_format = %d/%b/%Y:%H/%M/%S
file = /var/log/httpd/www.example.net-access.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /HttpdAccessLog

[HttpdErrorLog]
datetime_format = %d/%b/%Y:%H/%M/%S
file = /var/log/httpd/www.example.net-error.log
buffer_duration = 5000
log_stream_name = {instance_id}
initial_position = start_of_file
log_group_name = /HttpdErrorLog

バーチャルホストのアクセスログ、エラーログを対象にします。

 

設定の反映と起動

# systemctl restart awslogsd
# systemctl enable awslogsd
# systemctl status awslogsd

 

これでCloudWatch Logsのロググループに出力されます。

お疲れ様です。

 

 

 

 

最新情報をチェックしよう!
>システム構築・保守に特化した会社です。

システム構築・保守に特化した会社です。

システムの構築・保守運用「システムガーディアン」 社内システム担当が欲しいが、専属で雇うほどの仕事量はない。 必要な時に必要なだけ頼りたいというお悩みを持つ企業様へ専門知識を持って対応を行っております。 サーバから各種システムまで自社・他社で構築されたシステムに対してサポートを行っております。

CTR IMG