<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: prashann05gupta</title>
    <description>The latest articles on DEV Community by prashann05gupta (@prashann05gupta).</description>
    <link>https://dev.to/prashann05gupta</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F551259%2F79343950-dcdb-4877-bb34-1f4c8f0c0670.png</url>
      <title>DEV Community: prashann05gupta</title>
      <link>https://dev.to/prashann05gupta</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prashann05gupta"/>
    <language>en</language>
    <item>
      <title>
get-metric-statistics Using AWS CLI</title>
      <dc:creator>prashann05gupta</dc:creator>
      <pubDate>Wed, 06 Jan 2021 13:09:25 +0000</pubDate>
      <link>https://dev.to/prashann05gupta/get-metric-statistics-using-aws-cli-563o</link>
      <guid>https://dev.to/prashann05gupta/get-metric-statistics-using-aws-cli-563o</guid>
      <description>&lt;h3&gt;
  
  
  &lt;strong&gt;Story behind blog&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;after around 2 day's of struggle I made it 😁&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/xThuWaFtpt9fwyw824/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/xThuWaFtpt9fwyw824/giphy.gif"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let me share it with you how ?????&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;before directly jump into retrieving the data we need some information to shape the &lt;code&gt;JSON&lt;/code&gt; file.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;prerequisite:-&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;Namespace&lt;/code&gt; &lt;code&gt;MetricName&lt;/code&gt; &lt;code&gt;Dimensions&lt;/code&gt; &lt;code&gt;Period&lt;/code&gt; &lt;code&gt;Statistics&lt;/code&gt; &lt;code&gt;Unit&lt;/code&gt;&lt;br&gt;
these are most essential information we need.&lt;/p&gt;
&lt;h5&gt;
  
  
  don't worry I will let you know how
&lt;/h5&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;call the below API to list all the metrics available.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws cloudwatch list-metrics --output json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:-&lt;/strong&gt; &lt;code&gt;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&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;if aws profile sounds new to you please go to the below link &lt;br&gt;
&lt;a href="https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html"&gt;AWS profile&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;once called list-metrics API you will get something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; {
            "Namespace": "AWS/EC2",
            "MetricName": "your metrics name",
            "Dimensions": [
                {
                    "Name": "InstanceId",
                    "Value": "your instance id"
                }
            ]
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;you will get the three essential things &lt;code&gt;Namespace&lt;/code&gt; &lt;code&gt;MetricName&lt;/code&gt; &lt;code&gt;Dimensions&lt;/code&gt; by calling API.&lt;br&gt;
&lt;strong&gt;Note:-&lt;/strong&gt; in case you are retrieving &lt;code&gt;AWS/ELB&lt;/code&gt; metrics do not specify any region in &lt;code&gt;Dimensions&lt;/code&gt; it will give less data as the load is distributed between regions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;               {
                    "Name": "Region",
                    "Value": "ap-southeast-1"
                }

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;Once you selected the metrics and have all the needed information &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;it's time to create a &lt;code&gt;JSON&lt;/code&gt; file to generate the skeleton &lt;em&gt;call&lt;/em&gt; &lt;code&gt;aws cloudwatch get-metric-statistics --generate-cli-skeleton&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt;&amp;gt; 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"
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:-&lt;/strong&gt; if you are using math expression or percentile value then use &lt;code&gt;ExtendedStatistics&lt;/code&gt; otherwise remove it and if you know the &lt;code&gt;Unit&lt;/code&gt; then use it otherwise removed it.&lt;/p&gt;

&lt;p&gt;4.&lt;br&gt;
 create a json file ex. &lt;code&gt;metrics.json&lt;/code&gt; below is the example of json&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
{
    "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"
}  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Note:-&lt;/strong&gt; time is &lt;code&gt;ISO 8601&lt;/code&gt; standard &lt;br&gt;
you can use this website to generate timestamp &lt;a href="https://timestampgenerator.com/"&gt;timestamp&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;final step call the API
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;     aws cloudwatch get-metric-statistics --cli-input-json file://metrics.json --output json
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;we can use the different &lt;code&gt;--output&lt;/code&gt; formate &lt;code&gt;json&lt;/code&gt; &lt;code&gt;table&lt;/code&gt; &lt;code&gt;text&lt;/code&gt; &lt;code&gt;yaml&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Output:-&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
        "Label": "your metrics name",
        "Datapoints": [
            {
                "Timestamp": "2020-12-30T12:20:00+00:00",
                "SampleCount": 351.0,
                "Unit": "Count"
            }
        ]
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is it you did your job, please let me know any suggestion or question.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;धन्यवाद&lt;/code&gt; see you later&lt;/p&gt;

</description>
      <category>aws</category>
      <category>cloudwatch</category>
      <category>json</category>
      <category>api</category>
    </item>
  </channel>
</rss>
