CloudWatch examples using Tools for PowerShell V4 - AWS Tools for PowerShell (version 4)

The AWS Tools for PowerShell version 5 (V5) is in preview. To see V5 content, which is subject to change, and try out the new version, see the version 5 (preview) user guide. For specific information about breaking changes and migrating to V5, see the migration topic in that guide.

CloudWatch examples using Tools for PowerShell V4

The following code examples show you how to perform actions and implement common scenarios by using the AWS Tools for PowerShell V4 with CloudWatch.

Actions are code excerpts from larger programs and must be run in context. While actions show you how to call individual service functions, you can see actions in context in their related scenarios.

Each example includes a link to the complete source code, where you can find instructions on how to set up and run the code in context.

Topics

Actions

The following code example shows how to use Get-CWDashboard.

Tools for PowerShell V4

Example 1: Returns the arn the body of the specified dashboard.

Get-CWDashboard -DashboardName Dashboard1

Output:

DashboardArn DashboardBody ------------ ------------- arn:aws:cloudwatch::123456789012:dashboard/Dashboard1 {...
  • For API details, see GetDashboard in AWS Tools for PowerShell Cmdlet Reference (V4).

The following code example shows how to use Get-CWDashboardList.

Tools for PowerShell V4

Example 1: Returns the collection of dashboards for your account.

Get-CWDashboardList

Output:

DashboardArn DashboardName LastModified Size ------------ ------------- ------------ ---- arn:... Dashboard1 7/6/2017 8:14:15 PM 252

Example 2: Returns the collection of dashboards for your account whose names start with the prefix 'dev'.

Get-CWDashboardList -DashboardNamePrefix dev
  • For API details, see ListDashboards in AWS Tools for PowerShell Cmdlet Reference (V4).

The following code example shows how to use Remove-CWDashboard.

Tools for PowerShell V4

Example 1: Deletes the specified dashboard, promoting for confirmation before proceeding. To bypass confirmation add the -Force switch to the command.

Remove-CWDashboard -DashboardName Dashboard1
  • For API details, see DeleteDashboards in AWS Tools for PowerShell Cmdlet Reference (V4).

The following code example shows how to use Write-CWDashboard.

Tools for PowerShell V4

Example 1: Creates or updates the dashboard named 'Dashboard1' to include two metric widgets side by side.

$dashBody = @" { "widgets":[ { "type":"metric", "x":0, "y":0, "width":12, "height":6, "properties":{ "metrics":[ [ "AWS/EC2", "CPUUtilization", "InstanceId", "i-012345" ] ], "period":300, "stat":"Average", "region":"us-east-1", "title":"EC2 Instance CPU" } }, { "type":"metric", "x":12, "y":0, "width":12, "height":6, "properties":{ "metrics":[ [ "AWS/S3", "BucketSizeBytes", "BucketName", "amzn-s3-demo-bucket" ] ], "period":86400, "stat":"Maximum", "region":"us-east-1", "title":"amzn-s3-demo-bucket bytes" } } ] } "@ Write-CWDashboard -DashboardName Dashboard1 -DashboardBody $dashBody

Example 2: Creates or updates the dashboard, piping the content describing the dashboard into the cmdlet.

$dashBody = @" { ... } "@ $dashBody | Write-CWDashboard -DashboardName Dashboard1
  • For API details, see PutDashboard in AWS Tools for PowerShell Cmdlet Reference (V4).

The following code example shows how to use Write-CWMetricData.

Tools for PowerShell V4

Example 1: Creates a new MetricDatum object, and writes it to Amazon Web Services CloudWatch Metrics.

### Create a MetricDatum .NET object $Metric = New-Object -TypeName Amazon.CloudWatch.Model.MetricDatum $Metric.Timestamp = [DateTime]::UtcNow $Metric.MetricName = 'CPU' $Metric.Value = 50 ### Write the metric data to the CloudWatch service Write-CWMetricData -Namespace instance1 -MetricData $Metric
  • For API details, see PutMetricData in AWS Tools for PowerShell Cmdlet Reference (V4).