In Elastic Beanstalk how do I send custom logs to CloudWatch on the Amazon Linux 2023 platform?

199 views Asked by At

I'm deploying an Elastic Beanstalk app on Amazon Linux 2023. I'm trying to configure the new Amazon CloudWatch Agent to forward custom logs to cloudwatch. This used to be done by adding some config files into an awslogs directory, but that has been deprecated for the new agent. This new agent has a config file at /opt/amazon/amazon-cloudwatch-agent/etc/beanstalk.json. I can see the eb-engine starting the agent with this config file so I figured I can just overwrite with my config. I have confirmed that I'm writing to the file but then it's overwritten with the defaults again before the agent is started. I'm guessing Elastic Beanstalk's scripts add in their default config just before starting the agent

How do I do this with the new agent with Elastic Beanstalk extension configs or platform hooks?

1

There are 1 answers

0
andylee On

Config file below uses custom config file of new amazon-cloudwatch-agent

container_commands:   01_append_logs_config:
    command: |
      amazon-cloudwatch-agent-ctl -a append-config -m ec2 -c file:/tmp/custom-cloudwatch-config.json -s

files:   "/tmp/custom-cloudwatch-config.json":
    mode: "000600"
    owner: root
    group: root
    content: |
      {
        "logs": {
          "logs_collected": {
            "files": {
              "collect_list": [
                {
                  "file_path": "/var/log/logfilename.log",
                  "log_group_name": "`{"Fn::Join":["/", ["/aws/elasticbeanstalk", { "Ref":"AWSEBEnvironmentName" }, "var/log/logfilename.log"]]}`",
                  "log_stream_name": "{instance_id}"
                },
                
              ]
            }
          }
        }
      }