- Newest
- Most votes
- Most comments
Unfortunately, CloudWatch Metrics Insights does not directly support filtering Lambda metrics by tags. The approach you're trying with SCHEMA and SEARCH expressions in your CloudFormation template won't work as expected for tag-based filtering of Lambda functions.
However, there are alternative approaches you can consider to achieve your goal of creating alarms for multiple Lambda functions grouped by tags:
-
Use a Lambda function to dynamically create and update alarms:
- Create a Lambda function that periodically queries the Lambda API to get all functions with specific tags.
- This function can then create or update CloudWatch alarms for each matching Lambda function.
- Trigger this Lambda function on a schedule using EventBridge.
-
Use AWS Config rules:
- Create a custom AWS Config rule that identifies Lambda functions with specific tags.
- Use the rule's output to trigger a Lambda function that creates or updates alarms.
-
Utilize CloudFormation custom resources:
- Create a custom resource in your CloudFormation template.
- Implement a Lambda function as the backend for this custom resource.
- The Lambda function can query for tagged Lambda functions and create alarms accordingly.
-
Implement a CI/CD pipeline:
- As part of your deployment process, query for Lambda functions with specific tags.
- Dynamically generate CloudFormation templates with individual alarms for each function.
These approaches require more setup but offer the flexibility to create alarms based on tags. The best choice depends on your specific requirements and existing infrastructure.
For your CloudFormation template, instead of using tag-based filtering, you would need to explicitly list the Lambda function names or use one of the dynamic approaches mentioned above. Unfortunately, there's no direct way in CloudFormation to create a single alarm that monitors multiple Lambda functions based on tags.
Sources
Tags on Cloudwatch alarm for opsgenie | AWS re:Post
Create a CloudWatch alarm based on a metric math expression - Amazon CloudWatch
answered 2 years ago
Hi, you are right that Metrics Insights does not currently support filtering metrics by tags. The usual recommendation in your case is
- if you can express the same value than your tags as a dimension, as CloudWatch SQL engine and SEARCH functions are able to filter on dimension values, so it would be natively supported. It is however only possible on custom metrics, where you have full control over the dimensions and their values, vended metrics are defined by the services that emit them.
- if your resources are stable or it is easy for you to automate actions on the lifecycle on your resources, implement a lambda function to create, update or delete alarm definitions when the resources you want to alarm on vary, that’s pretty much what point 1 in the previous answer above corresponds to.
- if your resources are very dynamic like autoscaled instances, use external sources querying with a custom lambda that retrieves the list of resources from the tags by querying the resource group API, and use that list of resources to call CloudWatch GetMetricData for the corresponding metrics, do any preaggregation if you need, and forward the data in the output of the lambda.
That being said, both of those options 2 and 3 require some custom work. If you need it built-in as a standard feature and can’t go for option 1, the best way is if you have access to AWS support, to open a case to raise a feature request.
Relevant content
asked 2 years ago
- AWS OFFICIALUpdated a year ago
