Create an input parameter for your GetMetricData API operation.
Example operation:
cat metric-data-queries.json[
{
"Id": "e1",
"Expression": "m1 / m2",
"Label": "ErrorRate"
},
{
"Id": "m1",
"MetricStat": {
"Metric": {
"Namespace": "MyApplication",
"MetricName": "Errors",
"Dimensions": [
{
"Name": "FunctionName",
"Value": "MyFunc"
}
]
},
"Period": 300,
"Stat": "Sum",
"Unit": "Count"
},
"ReturnData": false
},
{
"Id": "m2",
"MetricStat": {
"Metric": {
"Namespace": "MyApplication",
"MetricName": "Invocations",
"Dimensions": [
{
"Name": "FunctionName",
"Value": "MyFunc"
}
]
},
"Period": 300,
"Stat": "Sum",
"Unit": "Count"
},
"ReturnData": false
}
]
Note: Replace MyApplication with your application name.
In the preceding operation, the input parameter has Invocations and Errors custom metrics. CloudWatch calculates the ErrorRate metric by the metric math of the other two metrics.
Use PutMetricData to publish sample metric data as custom metrics.
Example operation:
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:00:00Z$ aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:05:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:10:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:15:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Invocations --dimensions FunctionName=MyFunc --value 10 --unit Count --timestamp 2018-06-19T04:20:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 3 --unit Count --timestamp 2018-06-19T04:00:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 6 --unit Count --timestamp 2018-06-19T04:05:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 2 --unit Count --timestamp 2018-06-19T04:10:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 9 --unit Count --timestamp 2018-06-19T04:15:00Z
aws cloudwatch put-metric-data --namespace MyApplication --metric-name Errors --dimensions FunctionName=MyFunc --value 1 --unit Count --timestamp 2018-06-19T04:20:00Z
Note: To publish up to 20 metrics, add the --metric-data option to one PutMetricData operation under the same namespace, and then run the API.
Run the following get-metric-data AWS CLI command:
aws cloudwatch get-metric-data --metric-data-queries file://./metric-data-queries.json --start-time 2018-06-19T04:00:00Z --end-time 2018-06-19T04:30:00Z
Note: Replace //./metric-data-queries.json with your JSON file, 2018-06-19T04:00:00Z with your start time, and 2018-06-19T04:30:00Z with your end time.
Example output:
aws cloudwatch get-metric-data --metric-data-queries file://./metric-data-queries.json --start-time 2018-06-19T04:00:00Z --end-time 2018-06-19T04:30:00Z{
"MetricDataResults": [
{
"Timestamps": [
"2018-06-19T04:20:00Z",
"2018-06-19T04:15:00Z",
"2018-06-19T04:10:00Z",
"2018-06-19T04:05:00Z",
"2018-06-19T04:00:00Z"
],
"StatusCode": "Complete",
"Values": [
0.1,
0.9,
0.2,
0.6,
0.3
],
"Id": "e1",
"Label": "ErrorRate"
}
]
}
Note: In the preceding example output, CloudWatch calculates five data points with metric math and returns them as a time-ordered result. Because ReturnData is set to false, m1 and m2 aren't included in the response.