Let's go through the following steps to create metric filters using AWS CLI:
- We first need to prepare our metric transformations, as follows:
[
{
"metricName": "HelloCountMetric",
"metricNamespace": "ServerlessProgrammingCookbook",
"metricValue": "1"
}
]
Save this into the metric-transformations.json file.
- Use the put-metric-filter command to create a metric filter using the metric-transformations.json file, as follows:
aws logs put-metric-filter
--log-group-name /aws/lambda/demo-lambda-with-cli
--filter-name 'HelloCountFilter'
--filter-pattern 'Hello'
--metric-transformations file://metric-transformations.json
--region us-east-1
--profile admin
- Execute the Lambda a few times, either from the console or from AWS CLI, as follows:
aws lambda invoke
--invocation-type RequestResponse
--function-name demo-lambda-with-cli
--log-type Tail
--payload '{"name": "Heartin"}'
--region us-east-1
--profile admin
outputfile.txt
The actual output of the invocation does not matter, as long as it is a success message.
- Verify the metric from the console by going through the following steps:
-
- Log in to AWS Console and go to CloudWatch.
- Click on Metrics in the sidebar.
- Click on Metrics with no dimensions. We should now see a new metric with the name HelloCountMetric. We can also add HelloCountMetric to the graph or create an alarm for it, as shown in the following screenshot: