Adding custom variable in cloudwatch agent config
Asked Answered
S

1

10

I have the following config that I generate as part of user data for my EC2 instance

{
        "agent": {
                        "run_as_user": "root"
        },
        "logs": {
                "logs_collected": {
                        "files": {
                        "collect_list": [
                                {
                                "file_path": "/var/log/hapee-2.0/lb-access*",
                                "log_group_name" : <insert-region-here>
                                "log_stream_name": "haproxy-{instance_id}"
                                }
                        ]
                }
                }
        },
       "metrics": {
                "metrics_collected": {
                    "net": {
                    "measurement": [
                        "net_packets_recv",
                    "net_packets_sent"
                    ],
                    "metrics_collection_interval": 60
                },
                "mem": {
                        "measurement": [
                                "used_percent"
                        ]
                }
        }

        }
} 

As the log_group_name I want to be able to append the region that the instance is running in. I understand that the region is part of the instance meta data but can I use it in the cloudwatch config?

Saltzman answered 15/4, 2020 at 18:16 Comment(0)
A
0

According to the docs on CloudWatch Agent Configuration File Details the variables it can directly interpret in the logs.log_stream_name parameter are: {instance_id}, {hostname}, {local_hostname}, and {ip_address}.

Since CloudWatch log are region-specific, I'll assume you're doing some cross-region dashboarding or log-shipping (otherwise, having a region value in the log stream would be redundant).

Generating the configuration json file via any number of template engines would likely be the best way to add in custom variable values not directly supported by the CloudWatch agent's config variables. We do this in our CI/CD pipeline while creating the instance via Terraform's templatefile function, (which we then write to the instance via cloud-init user data) but there are many popular template engines for whatever programming or configuration language you happen to be using.

Appealing answered 29/2 at 17:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.