Amazon CloudWatch is a monitoring service for Amazon Web Services (AWS) that provides robust monitoring of your entire AWS infrastructure resources and applications. It can collect data, gain insights, and alert users to fix problems within applications and resources if required. Amazon CloudWatch can also hold and represent your custom data in a graph. It is not necessarily limited to data from your AWS infrastructure and resources.

Before I begin, I'm assuming you have an AWS account and have installed AWS Tools for PowerShell on your computer. You'll also need to set up your AWS credentials and necessary Identity and Access Management (IAM) policies granting you programmatic access to CloudWatch. Also helpful is to learn more about configuring CloudWatch with PowerShell.

AWS CloudWatch

AWS CloudWatch

MetricDatum ^

The Amazon CloudWatch application programming interface (API) contains several data types used by various CloudWatch operations, and before pushing any data to CloudWatch, you first have to initialize an object of the MetricDatum data type in the Amazon CloudWatch API.

MetricDatum encapsulates information like Metric Name, Unit, Value, and Timestamp, sending it to CloudWatch to create a metric. To create a MetricDatum you have to define an object of the Amazon.CloudWatch.Model.MetricDatum type from the AWS software development kit (SDK) for .NET as in the following example:

$result = Invoke-RestMethod https://www.reddit.com/r/powershell/about.json
$value = $result.Data.accounts_active
$data = New-Object Amazon.CloudWatch.Model.MetricDatum
$data.Timestamp = (Get-Date).ToUniversalTime()
$data.MetricName = "/r/powershell"
$data.Unit = "Count"
$data.Value = $value
Create a MetricDatum

Create a MetricDatum

Write custom metric data to Amazon CloudWatch ^

Once you're ready to publish the MetricDatum, you can use the Write-CWMetricData command in the AWS Tools for PowerShell. This calls the PutMetricData API to write metrics to Amazon CloudWatch.

Write-CWMetricData -Namespace "Active Users" -MetricData $data

The Write-CWMetricData cmdlet is very straightforward and easy to use, but understanding the two mandatory parameters of this command can be helpful.

Namespace

CloudWatch stores all metrics inside namespaces. You can define custom namespaces per your requirements to save any custom metric like "Active Users" in our example or choose an AWS Namespace. You can distinguish between metrics by saving them in different namespaces.

Metric data

Every metric data point is a MetricDatum .NET object and must have a name, but the Value, Timestamp, and Unit properties are optional. You can publish one or more metric data points to AWS CloudWatch at once through the cmdlet Write-CWMetricData since it accepts an array of <Amazon.CloudWatch.Model.MetricDatum[]> objects as input.

So let me write a small script that can capture some data points. I'm specifically looking for the number of "Active Users" on various topics (subreddits) on the website reddit.com. Once I have the data points, I'll write them to a custom data metric (MetricDatum) and then publish it to AWS CloudWatch using Write-CWMetricData.

Import-Module AWSPowerShell

# initialize AWS creds and regions for programmatic access
$param = @{
    Region = 'ap-south-1'
    AccessKey = 'YOUR ACCESS KEY'
    SecretKey = 'YOUR SECRET KEY'
}

Initialize-AWSDefaults @param -Verbose

While($true)
{
    $Names = 'PowerShell','AWS','Azure','Security','sysadmin','machinelearning'
    foreach ($SubReddit in $Names) {
        $result = Invoke-RestMethod https://www.reddit.com/r/$SubReddit/about.json
        $value = $result.Data.accounts_active
        $data = New-Object Amazon.CloudWatch.Model.MetricDatum
        $data.Timestamp = (Get-Date).ToUniversalTime()
        $data.MetricName = "/r/$SubReddit"
        $data.Unit = "Count"
        $data.Value = $value
        if($data.value){
            Write-CWMetricData -Namespace "Active Users" -MetricData $data
        }
    }

    Write-Host "[$([datetime]::Now)] Published metric data points to Amazon CloudWatch" -foreground Yellow
    Start-Sleep -Seconds 30
}
Publish custom metrics to AWS

Publish custom metrics to AWS

Amazon CloudWatch console ^

After publishing the custom metric data, it goes to the metrics on the AWS CloudWatch console under the namespace to which you wrote it. Navigate to:

AWS CloudWatch Console >  Metrics > All metrics > Custom Namespaces > Active Users

Select all the metrics you want to watch, and you'll see the data points of all the selected metrics plotted on the graph. You can even hover over the graph and see the metric values at a specific point of time.

Amazon CloudWatch console

Amazon CloudWatch console

Conclusion ^

AWS Tools for PowerShell lets you form and write your custom metrics to Amazon CloudWatch with ease and makes metrics searchable and insightful. These Amazon CloudWatch metrics can integrate with a variety of other AWS services to achieve objectives. For example, you can publish your custom metrics to AWS CloudWatch and set up CloudWatch alarms to notify you when meeting specific conditions. You can even perform some automated tasks.

0 Comments

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account