Creating metric filters from AWS CLI

Let's go through the following steps to create metric filters using AWS CLI:

  1. We first need to prepare our metric transformations, as follows:
[
{
"metricName": "HelloCountMetric",
"metricNamespace": "ServerlessProgrammingCookbook",
"metricValue": "1"
}
]

Save this into the metric-transformations.json file.

  1. 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
  1. 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.

  1. Verify the metric from the console by going through the following steps:
    1. Log in to AWS Console and go to CloudWatch.
    2. Click on Metrics in the sidebar.
    3. 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:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.73.127