AWS

CloudWatch Agent Laravelログ出力

AWS

 

環境

  • AWS EC2
  • Ubuntu

 

Ubuntu + Laravel

AWS Ubuntu18 + Laravelのログ集約

 

IAM

IAM
  ロールの作成 ->
  ポリシー -> CloudWatchAgentAdminPolicyにチェック

 

EC2にIAMロールを紐付け

 

 

CloudWatch Agent

 

$ wget https://s3.amazonaws.com/amazoncloudwatch-agent/ubuntu/amd64/latest/amazon-cloudwatch-agent.deb
$ sudo dpkg -i -E ./amazon-cloudwatch-agent.deb
$ sudo amazon-cloudwatch-agent-ctl -a start

 

初期コンフィグ

$ cat /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/default

{
	"agent": {
		"run_as_user": "cwagent"
	},
	"metrics": {
		"metrics_collected": {
			"mem": {
				"measurement": [
					"mem_used_percent"
				]
			},
			"disk": {
				"measurement": [
					"used_percent"
				],
				"resources": [
					"*"
				]
			}
		},
		"append_dimensions": {
			"ImageId": "${aws:ImageId}",
			"InstanceId": "${aws:InstanceId}",
			"InstanceType": "${aws:InstanceType}",
			"AutoScalingGroupName": "${aws:AutoScalingGroupName}"
		}
	}
}

 

Laravel用に改造

$ vi /opt/aws/amazon-cloudwatch-agent/etc/amazon-cloudwatch-agent.d/default

{
    "agent": {
        "run_as_user": "root"
    },
    "metrics": {
        "metrics_collected": {
            "mem": {
                "measurement": [
		    "mem_used_percent"
		]
	    },
	    "disk": {
	        "measurement": [
	            "used_percent"
		],
	        "resources": [
		    "*"
	        ]
	    }
	},
	"append_dimensions": {
		"ImageId": "${aws:ImageId}",
		"InstanceId": "${aws:InstanceId}",
		"InstanceType": "${aws:InstanceType}",
		"AutoScalingGroupName": "${aws:AutoScalingGroupName}"
	}
    },
    "logs": {
        "logs_collected": {
            "files": {
                "collect_list": [
                    {
                        "file_path": "/var/www/product-project/storage/logs/laravel*log",
                        "log_group_name": "sampleapp-prod_laravel",
                        "log_stream_name": "ec2-{instance_id}-laravel"
                    },
                    {
                        "file_path": "/var/www/product-project/storage/logs/request*log",
                        "log_group_name": "sampleapp-prod_nginx_request",
                        "log_stream_name": "ec2-{instance_id}-laravel-request"
                    },
                    {
                        "file_path": "/var/log/nginx/*.log*",
                        "log_group_name": "sampleapp-prod_nginx",
                        "log_stream_name": "ec2-{instance_id}-nginx-log"
                    },
                    {
                        "file_path": "/var/log/nginx/error.log",
                        "log_group_name": "sampleapp-prod_nginx_error",
                        "log_stream_name": "ec2-{instance_id}-nginx-error.log"
                    },
                    {
                        "file_path": "/var/log/php7.2-fpm.log",
                        "log_group_name": "sampleapp-prod_php-fpm",
                        "log_stream_name": "ec2-php-fpm.log"
                    },
                    {
                        "file_path": "/var/log/php7.2-fpm-slow.log",
                        "log_group_name": "sampleapp-prod_php-fpm_slow",
                        "log_stream_name": "ec2-{instance_id}-php-fpm-slow.log"
                    }
                ]
            }
        }
    }
}

 

再起動

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a stop
$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a start

$ sudo /opt/aws/amazon-cloudwatch-agent/bin/amazon-cloudwatch-agent-ctl -m ec2 -a status
{
  "status": "running",
  "starttime": "2020-10-01T03:19:47+00:00",
  "version": "1.247345.34b249191"
}

再起動してCloudWatch Agentが自動起動していたら寝て良し!

 

AWSコンソールでCloudWatch -> ログからロググループ”yuu3-stg”を確認してログが出力されている🐱

 

 

Amazonおすすめ

iPad 9世代 2021年最新作

iPad 9世代出たから買い替え。安いぞ!🐱 初めてならiPad。Kindleを外で見るならiPad mini。ほとんどの人には通常のiPadをおすすめします><

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)