Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to CloudWatch Laravel logs deployed in AWS Elastic Beanstalk?

I have a PHP Laravel Worker deployed in AWS Elastic Beanstalk & wanted to stream the /var/app/current/storage/logs/*.log to CloudWatch. However the solutions I came across are either for Forge deployments or vanilla EC2 instances.

Would appreciate any help in this matter.

like image 949
Sam Avatar asked Apr 15 '17 11:04

Sam


1 Answers

You need to configure CloudWatch agent as described here:

  • Grant IAM Permissions for the CloudWatch Logs Agent;
  • Deploy your app zipped together with .ebextensions/logs.config file.

I assume that logs.config should look like this

option_settings:
  - namespace: aws:elasticbeanstalk:cloudwatch:logs
    option_name: StreamLogs
    value: true
  - namespace: aws:elasticbeanstalk:cloudwatch:logs
    option_name: DeleteOnTerminate
    value: false
  - namespace: aws:elasticbeanstalk:cloudwatch:logs
    option_name: RetentionInDays
    value: 7

packages:
  yum:
    awslogs: []

files:
  "/etc/awslogs/awscli.conf" :
    mode: "000600"
    owner: root
    group: root
    content: |
      [plugins]
      cwlogs = cwlogs
      [default]
      region = `{"Ref":"AWS::Region"}`

  "/etc/awslogs/awslogs.conf" :
    mode: "000600"
    owner: root
    group: root
    content: |
      [general]
      state_file = /var/lib/awslogs/agent-state

  "/etc/awslogs/config/logs.conf" :
    mode: "000600"
    owner: root
    group: root
    content: |
      [/var/app/current/storage/logs]
      log_group_name = `{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/app/current/storage/logs"]]}`
      log_stream_name = {instance_id}
      file = /var/app/current/storage/logs/*.log

commands:
  "01":
    command: chkconfig awslogs on
  "02":
    command: service awslogs restart

Please refer to /var/log/awslogs.log at your ec2 instance for troubleshooting.

like image 163
Pavel Bely Avatar answered Nov 15 '22 07:11

Pavel Bely