Story behind blog
I have to develop a automation where I need to collect the data from the AWS cloudwatch, after reading lots of very jargon documentation, researched over the internet I didn't found anything that can help.
after around 2 day's of struggle I made it 😁
let me share it with you how ?????
before directly jump into retrieving the data we need some information to shape the JSON
file.
-
prerequisite:-
Namespace
MetricName
Dimensions
Period
Statistics
Unit
these are most essential information we need.don't worry I will let you know how
call the below API to list all the metrics available.
aws cloudwatch list-metrics --output json
Note:- if you have multiple regions please make sure you are export that aws profile first otherwise, it will list the metric in the default region you had specified
if aws profile sounds new to you please go to the below link
AWS profile
once called list-metrics API you will get something like
{
"Namespace": "AWS/EC2",
"MetricName": "your metrics name",
"Dimensions": [
{
"Name": "InstanceId",
"Value": "your instance id"
}
]
}
you will get the three essential things Namespace
MetricName
Dimensions
by calling API.
Note:- in case you are retrieving AWS/ELB
metrics do not specify any region in Dimensions
it will give less data as the load is distributed between regions
{
"Name": "Region",
"Value": "ap-southeast-1"
}
- Once you selected the metrics and have all the needed information
it's time to create a JSON
file to generate the skeleton call aws cloudwatch get-metric-statistics --generate-cli-skeleton
>>>> aws cloudwatch get-metric-statistics --generate-cli-skeleton
{
"Namespace": "",
"MetricName": "",
"Dimensions": [
{
"Name": "",
"Value": ""
}
],
"StartTime": "1970-01-01T00:00:00",
"EndTime": "1970-01-01T00:00:00",
"Period": 0,
"Statistics": [
"Sum"
],
"ExtendedStatistics": [
""
],
"Unit": "Terabits"
}
Note:- if you are using math expression or percentile value then use ExtendedStatistics
otherwise remove it and if you know the Unit
then use it otherwise removed it.
4.
create a json file ex. metrics.json
below is the example of json
{
"Namespace": "AWS/ApplicationELB",
"MetricName": "Your Metrics name",
"Dimensions": [
{
"Name": "TargetGroup",
"Value": "your_target_group_name"
},
{
"Name": "LoadBalancer",
"Value": "your_loadbalancer_name"
}
],
"StartTime": "2020-12-30T12:20:00",
"EndTime": "2020-12-30T12:25:00",
"Period": 86400,
"Statistics": [
"Sum"
],
"Unit": "Count"
}
Note:- time is ISO 8601
standard
you can use this website to generate timestamp timestamp
- final step call the API
aws cloudwatch get-metric-statistics --cli-input-json file://metrics.json --output json
we can use the different --output
formate json
table
text
yaml
Output:-
"Label": "your metrics name",
"Datapoints": [
{
"Timestamp": "2020-12-30T12:20:00+00:00",
"SampleCount": 351.0,
"Unit": "Count"
}
]
}
this is it you did your job, please let me know any suggestion or question.
धन्यवाद
see you later
Top comments (0)