Use PutDashboard with an Amazon SDK or CLI
The following code examples show how to use PutDashboard.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code examples:
- .NET
-
- Amazon SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. /// <summary> /// Wrapper to create or add to a dashboard with metrics. /// </summary> /// <param name="dashboardName">The name for the dashboard.</param> /// <param name="dashboardBody">The metric data in JSON for the dashboard.</param> /// <returns>A list of validation messages for the dashboard.</returns> public async Task<List<DashboardValidationMessage>> PutDashboard(string dashboardName, string dashboardBody) { // Updating a dashboard replaces all contents. // Best practice is to include a text widget indicating this dashboard was created programmatically. var dashboardResponse = await _amazonCloudWatch.PutDashboardAsync( new PutDashboardRequest() { DashboardName = dashboardName, DashboardBody = dashboardBody }); return dashboardResponse.DashboardValidationMessages ?? new List<DashboardValidationMessage>(); }-
For API details, see PutDashboard in Amazon SDK for .NET API Reference.
-
- CLI
-
- Amazon CLI
-
To create a dashboard
The following
put-dashboardexample creates a dashboard namedDashboard-Ain the specified account.aws cloudwatch put-dashboard \ --dashboard-nameDashboard-A\ --dashboard-body '{"widgets":[{"height":6,"width":6,"y":0,"x":0,"type":"metric","properties":{"view":"timeSeries","stacked":false,"metrics":[["Namespace","CPUUtilization","Environment","Prod","Type","App"]],"region":"us-east-1"}}]}'Output:
{ "DashboardValidationMessages": [] }For more information, see Creating a CloudWatch dashboard
in the Amazon CloudWatch User Guide. -
For API details, see PutDashboard
in Amazon CLI Command Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. /** * Creates a new dashboard with the specified name and body. * * @param dashboardName the name of the dashboard to be created * @param dashboardBody the dashboard body, as JSON * @return a {@link CompletableFuture} representing the asynchronous operation of creating the dashboard */ public CompletableFuture<PutDashboardResponse> createDashboardAsync(String dashboardName, String dashboardBody) { PutDashboardRequest dashboardRequest = PutDashboardRequest.builder() .dashboardName(dashboardName) .dashboardBody(dashboardBody) .build(); return getAsyncClient().putDashboard(dashboardRequest) .handle((response, ex) -> { if (ex != null) { logger.info("Failed to create dashboard: {}", ex.getMessage()); throw new RuntimeException("Dashboard creation failed", ex); } else { // Handle the normal response case logger.info("{} was successfully created.", dashboardName); List<DashboardValidationMessage> messages = response.dashboardValidationMessages(); if (messages.isEmpty()) { logger.info("There are no messages in the new Dashboard."); } else { for (DashboardValidationMessage message : messages) { logger.info("Message: {}", message.message()); } } return response; // Return the response for further use } }); }-
For API details, see PutDashboard in Amazon SDK for Java 2.x API Reference.
-
- Kotlin
-
- SDK for Kotlin
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the Amazon Code Examples Repository
. suspend fun createDashboard( dashboardNameVal: String, dashboardBodyVal: String, ) { val dashboardRequest = PutDashboardRequest { dashboardName = dashboardNameVal dashboardBody = dashboardBodyVal } CloudWatchClient.fromEnvironment { region = REGION }.use { cwClient -> val response = cwClient.putDashboard(dashboardRequest) println("$dashboardNameVal was successfully created.") val messages = response.dashboardValidationMessages if (messages != null) { if (messages.isEmpty()) { println("There are no messages in the new Dashboard") } else { for (message in messages) { println("Message is: ${message.message}") } } } } }-
For API details, see PutDashboard
in Amazon SDK for Kotlin API reference.
-
- PowerShell
-
- 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 $dashBodyExample 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 Amazon Tools for PowerShell Cmdlet Reference (V4).
-
- Tools for PowerShell V5
-
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 $dashBodyExample 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 Amazon Tools for PowerShell Cmdlet Reference (V5).
-
For a complete list of Amazon SDK developer guides and code examples, see Using CloudWatch with an Amazon SDK. This topic also includes information about getting started and details about previous SDK versions.