<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>CloudWatch · Emmanuel Tsouris</title>
    <link>https://www.emmanueltsouris.com/tags/cloudwatch/</link>
    <description>Places, culture, memory, and how ordinary things work. Why is it like that?</description>
    <language>en-us</language>
    <managingEditor>Emmanuel Tsouris</managingEditor>
    <webMaster>Emmanuel Tsouris</webMaster>
    <copyright>© 2026 Emmanuel Tsouris</copyright><lastBuildDate>Sun, 26 Nov 2023 05:30:03 +0000</lastBuildDate>
    <atom:link href="https://www.emmanueltsouris.com/tags/cloudwatch/index.xml" rel="self" type="application/rss+xml"/>
    <item>
      <title>Enhance AWS Monitoring by Emitting Custom CloudWatch Metrics with PowerShell</title>
      <link>https://www.emmanueltsouris.com/posts/aws-monitoring-custom-cloudwatch-metrics-powershell/</link>
      <pubDate>Sun, 26 Nov 2023 05:30:03 +0000</pubDate>
      <guid isPermaLink="true">https://www.emmanueltsouris.com/posts/aws-monitoring-custom-cloudwatch-metrics-powershell/</guid>
      <dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Emmanuel Tsouris</dc:creator>
      <description>In the world of cloud computing, monitoring and analytics play a pivotal role. Amazon CloudWatch stands out as a robust monitoring service for AWS cloud resources and the applications you run on AWS. Let’s dive into a practical example of how to use PowerShell to emit metrics to Amazon CloudWatch, to give you better observability.
</description>
      
      <content:encoded><![CDATA[<p>In the world of cloud computing, monitoring and analytics play a pivotal role. Amazon CloudWatch stands out as a robust monitoring service for AWS cloud resources and the applications you run on AWS. Let&rsquo;s dive into a practical example of how to use PowerShell to emit metrics to Amazon CloudWatch, to give you better observability.</p>
<h2 id="the-need-for-custom-metrics">The Need for Custom Metrics<a class="heading-anchor" href="#the-need-for-custom-metrics" aria-label="Link to this section"><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 14a4.5 4.5 0 0 0 6.6.4l2.5-2.5a4.6 4.6 0 0 0-6.5-6.5l-1.4 1.4"/><path d="M14 10a4.5 4.5 0 0 0-6.6-.4L4.9 12a4.6 4.6 0 0 0 6.5 6.5l1.4-1.4"/></svg></a></h2>
<p>Custom metrics in CloudWatch allow you to track operational health and performance of your systems. From tracking the number of active users to monitoring the throughput of a transaction process, custom metrics can provide invaluable insights into your application&rsquo;s behavior. At scale, you can use aggregate metrics to keep an eye on thousands or millions of processes, best of all you can let the systems do the work.</p>
<h2 id="powershell-and-cloudwatch-a-powerful-combination">PowerShell and CloudWatch: A Powerful Combination<a class="heading-anchor" href="#powershell-and-cloudwatch-a-powerful-combination" aria-label="Link to this section"><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 14a4.5 4.5 0 0 0 6.6.4l2.5-2.5a4.6 4.6 0 0 0-6.5-6.5l-1.4 1.4"/><path d="M14 10a4.5 4.5 0 0 0-6.6-.4L4.9 12a4.6 4.6 0 0 0 6.5 6.5l1.4-1.4"/></svg></a></h2>
<p>Early in my career at AWS, I wrote a lot of PowerShell to run on Windows Server EC2 instances at scale. PowerShell, with its versatility and ease of use, is an excellent tool for interacting with AWS services, including CloudWatch. By scripting your metrics, you can automate monitoring tasks, leading to more efficient and reliable operations.</p>
<h2 id="sample-script">Sample Script<a class="heading-anchor" href="#sample-script" aria-label="Link to this section"><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 14a4.5 4.5 0 0 0 6.6.4l2.5-2.5a4.6 4.6 0 0 0-6.5-6.5l-1.4 1.4"/><path d="M14 10a4.5 4.5 0 0 0-6.6-.4L4.9 12a4.6 4.6 0 0 0 6.5 6.5l1.4-1.4"/></svg></a></h2>
<p>Our example involves a PowerShell script that emits a distinct metric to CloudWatch. Let&rsquo;s break it down:</p>
<ol>
<li>
<p><strong>Creating a Metric Datum</strong>: We start by creating an instance of the <a href="https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_MetricDatum.html" rel="noopener"><code>MetricDatum</code></a> class. This object represents a data point for a CloudWatch metric.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="nv">$metric1</span> <span class="p">=</span> <span class="nb">New-Object</span> <span class="n">Amazon</span><span class="p">.</span><span class="py">CloudWatch</span><span class="p">.</span><span class="py">Model</span><span class="p">.</span><span class="py">MetricDatum</span>
</span></span></code></pre></div></li>
<li>
<p><strong>Setting Metric Properties</strong>: We then set various properties of this metric, such as <code>TimeStamp</code>, <code>MetricName</code>, <code>Unit</code>, and <code>Value</code>. These properties define what the metric is about and its measurement.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="nv">$metric1</span><span class="p">.</span><span class="py">TimeStamp</span> <span class="p">=</span> <span class="p">(</span><span class="nb">Get-Date</span><span class="p">).</span><span class="py">ToUniversalTime</span><span class="p">()</span>
</span></span><span class="line"><span class="cl"><span class="nv">$metric1</span><span class="p">.</span><span class="py">MetricName</span> <span class="p">=</span> <span class="s2">&#34;TestMetric&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">$metric1</span><span class="p">.</span><span class="py">Unit</span> <span class="p">=</span> <span class="s2">&#34;Count&#34;</span>
</span></span><span class="line"><span class="cl"><span class="nv">$metric1</span><span class="p">.</span><span class="py">Value</span> <span class="p">=</span> <span class="mf">42</span>
</span></span></code></pre></div></li>
<li>
<p><strong>Namespace Specification</strong>: A namespace is like a container for CloudWatch metrics. We specify this to categorize and isolate our custom metrics. I used my first initial here, you can use whatever makes sense for your use case.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="nv">$namespace1</span> <span class="p">=</span> <span class="s2">&#34;MyPowerShellScript/Test&#34;</span>
</span></span></code></pre></div></li>
<li>
<p><strong>Emitting the Metric</strong>: Finally, we use the <code>Write-CWMetricData</code> cmdlet to send the metric data to CloudWatch.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-powershell" data-lang="powershell"><span class="line"><span class="cl"><span class="nb">Write-CWMetricData</span> <span class="n">-NameSpace</span> <span class="nv">$namespace1</span> <span class="n">-MetricData</span> <span class="nv">$metric1</span>
</span></span></code></pre></div></li>
</ol>
<h2 id="benefits-and-applications">Benefits and Applications<a class="heading-anchor" href="#benefits-and-applications" aria-label="Link to this section"><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 14a4.5 4.5 0 0 0 6.6.4l2.5-2.5a4.6 4.6 0 0 0-6.5-6.5l-1.4 1.4"/><path d="M14 10a4.5 4.5 0 0 0-6.6-.4L4.9 12a4.6 4.6 0 0 0 6.5 6.5l1.4-1.4"/></svg></a></h2>
<p>This script is particularly useful for:</p>
<ul>
<li>Automating the emission of custom metrics.</li>
<li>Efficiently monitoring different aspects of your application.</li>
<li>Integrating into larger automation scripts for comprehensive monitoring solutions.</li>
<li>Creating CloudWatch Alarms from these metrics, which can trigger other actions.</li>
</ul>
<h2 id="conclusion">Conclusion:<a class="heading-anchor" href="#conclusion" aria-label="Link to this section"><svg class="icon" viewBox="0 0 24 24" aria-hidden="true"><path d="M10 14a4.5 4.5 0 0 0 6.6.4l2.5-2.5a4.6 4.6 0 0 0-6.5-6.5l-1.4 1.4"/><path d="M14 10a4.5 4.5 0 0 0-6.6-.4L4.9 12a4.6 4.6 0 0 0 6.5 6.5l1.4-1.4"/></svg></a></h2>
<p>Using PowerShell to interact with Amazon CloudWatch allows for flexible and powerful monitoring of AWS resources and applications. Custom scripts, like the one we&rsquo;ve explored, provide a scalable and efficient method to manage cloud monitoring, enhancing your cloud management capabilities.</p>
<aside class="book-cta">
  <h2>Pro PowerShell for Amazon Web Services</h2>
  <p>The book I wrote about automating AWS with PowerShell — EC2, S3, CloudWatch, IAM and the rest, worked through end to end. Second edition, from Apress.</p>
  <p><a class="btn" href="https://amzn.to/4iKOQMj" rel="noopener sponsored">Find it on Amazon</a></p>
  <p class="affiliate-disclosure">Affiliate link. If you buy through it I may earn a small commission at no extra cost to you.</p>
</aside>

]]></content:encoded>
    </item>
  </channel>
</rss>
