Elastic Beanstalk Single Container Docker - use awslogs logging driver

痞子三分冷 提交于 2019-12-02 17:15:33

I'm posting here the answer I received from AWS support:

As Elastic Beanstalk Single Container environment will save the stdout and stderr on /var/log/eb-docker/containers/eb-current-app/ by default, and as the new solution stack allows you the option to stream log to cloudwatch, automating the configuration of the AWSLogs agent on the instances, what I recommend to do is to add an ebextension to add the stdout and stderr logs files to the cloudwatch configuration and use the already configured agent to stream those files to cloudwatch logs. instead of touching the pre-hooks , which is nor supported by AWS as hooks may change from solution stack version to another.

Regarding the error you are seeing "logs" command is supported only for "json-file" and "journald" logging drivers (got: awslogs)" this error is from how docker works, when it is configured to send logs to other driver beside json-file or journald it will not be able to display logs locally as it does not have a local copy of them.

### BEGIN .ebextensions/logs.config
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

files:
  "/etc/awslogs/config/stdout.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      [docker-stdout]
      log_group_name=/aws/elasticbeanstalk/environment_name/docker-stdout
      log_stream_name={instance_id}
      file=/var/log/eb-docker/containers/eb-current-app/*-stdouterr.log

commands:
  "00_restart_awslogs":
    command: service awslogs restart

### END .ebextensions/logs.config

I was able to expand on the previous answer for a multi container elastic beanstalk environment as well as inject the environment name. I did have to grant the correct permission in the ec2 role to be able to create the log group. You can see if it is working by looking in:

/var/log/awslogs.log

this goes in .ebextensions/logs.config

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: 14

files:
  "/etc/awslogs/config/stdout.conf":
    mode: "000755"
    owner: root
    group: root
    content: |
      [/var/log/containers/docker-stdout]
      log_group_name=/aws/elasticbeanstalk/`{ "Ref" : "AWSEBEnvironmentName" }`/docker-stdout.log
      log_stream_name={instance_id}
      file=/var/log/containers/*-stdouterr.log

commands:
  "00_restart_awslogs":
    command: service awslogs restart
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!