CloudWatch Log Metric Filters

AWS CloudWatch Dashboard Image

Yesterday, I successfully configured distinct metric filters for specific CloudWatch log groups within our AWS lab environment. After verifying their effectiveness, I realized the need to replicate these filters across both our AWS staging and production environments.

** I understand that AWS CloudWatch offers cross-account capabilities, but let’s assume for a moment that utilizing them isn’t feasible or recommended, as was the case in my situation. Additionally, there are instances, like mine, where swiftly integrating your CDK and deployment process to accommodate these requirements may not be feasible. While it’s true that log group metric filters can be added through AWS CDK during the deployment phase, implementing such changes entails additional steps and approvals for staging and production updates. Nevertheless, I wanted to highlight that my solution could potentially offer a time-saving alternative in certain scenarios.

To expedite this process, I leveraged the AWS CLI tool. This involved exporting designated log group metric filters from one AWS account and subsequently transferring them to another account. Additionally, I utilized the tool to import metric filters from a JSON file, which had been previously exported from log metric filters.

You can find this script here in Gist:

https://gist.github.com/hyavari/9d55f1fe4e0f608eaa8f5c415cc6a73a

Usage is not complicated. You only need SSO profiles (that in my case, I regularly use them.)

for example:

for importing a JSON file including filters you can do like this:

1
./cloudwatch_metric_importer.sh -i --input metrics.json --profile lab001 --region us-east-1

or if you want to export (copy) from one account then import to another account:

1
./cloudwatch_metric_importer.sh -e --profile lab001 --region us-east-1 --log-group groupName001 --dst-profile staging001 --dst-region us-east-2

I hope this helps you save time!

Always there is place for improvement, so please do not hesitate to contribute them.

Take care ;)

[1] CloudWatch Metrics - https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/MonitoringLogData.html

[2] CloudWatch Metric Filters Regex - https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html

updatedupdated2024-02-162024-02-16