0
votes

I have to send logs from AWS EC2 server to the AWS CloudWatch logs. For this, I have created a CloudWatch configuration file for collecting logs such as

{
    "agent": {
            "run_as_user": "root"
    },
    "logs": {
            "logs_collected": {
                    "files": {
                            "collect_list": [
                                    {
                                            "file_path": "/var/log/testing.log",
                                            "log_group_name": "/project/development",
                                            "log_stream_name": "{instance_id}_testing-logs"
                                    }
                            ]
                    }
            }
    }
}

I have created an application in AWS CodeDeploy named project with three groups inside that namely development, staging, and production. I want CloudWatch to use the naming convention such as /project/development and similarly to name it for staging and production to name its log group log_group_name. For this, I do not want to create three separate files for each environment.

Is there any CloudWatch variable available for CodeDeploy which I can use it to name log_group_name like there are naming variables available for {instance_id} which automatically picks the Instance Id.

2

2 Answers

1
votes

Check out the CloudWatch Agent Configuration File: Logs Section documentation. For either the log_group_name or log_stream_name field,

as part of the name, you can use {instance_id}, {hostname}, {local_hostname}, and {ip_address} as variables within the name.

If you want to use CodeDeploy specific variables in your CloudWatch Logs name, CodeDeploy do make available a set of environment variables for hook scripts. So the idea is to do a CodeDeploy deployment of your CloudWatch agent installation and create your CloudWatch agent configuration file using the CodeDeploy variables.

0
votes

log_stream_name Specifies the destination log stream. You can use a literal string or predefined variables ({instance_id}, {hostname}, {ip_address}), or combination of both to define a log stream name. A log stream is created automatically if it doesn't already exist.

CloudWatch logs can only access those variables above.

Take a read of the documentation to see more details.