Skip to content

Commit

Permalink
Cleanup Cloudwatch examples (awsdocs#4034)
Browse files Browse the repository at this point in the history
* Clean up CloudWatch examples.

- Refactor all actions
- Add integration tests
- Move actions from src/ to actions/
- Delete actions directory
- Refactor client lib

* Fix test naming.

* update metadata and comments

* update vite config and fix some test issues

* add libs package
  • Loading branch information
cpyle0819 authored Jan 5, 2023
1 parent 57042f0 commit 31cac4f
Show file tree
Hide file tree
Showing 41 changed files with 1,853 additions and 581 deletions.
34 changes: 17 additions & 17 deletions .doc_gen/metadata/cloudwatch_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
cloudwatch_ListMetrics:
title: List &CW; metrics using an &AWS; SDK
title_abbrev: List metrics
synopsis: list &CWlong; metrics.
synopsis: list the metadata for &CWlong; metrics. To get data for a metric, use the GetMetricData, or GetMetricStatistics actions.
category:
languages:
.NET:
Expand Down Expand Up @@ -61,10 +61,10 @@ cloudwatch_ListMetrics:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.metrics.listMetricsV3
- javascript.v3.cloudwatch.actions.ListMetrics
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-getting-metrics.html#cloudwatch-examples-getting-metrics-listing
Expand All @@ -77,7 +77,7 @@ cloudwatch_ListMetrics:
cloudwatch_PutMetricData:
title: Put data into a &CW; metric using an &AWS; SDK
title_abbrev: Put data into a metric
synopsis: put data into a &CWlong; metric.
synopsis: publish metric data points to &CWlong;.
category:
languages:
Java:
Expand Down Expand Up @@ -119,10 +119,10 @@ cloudwatch_PutMetricData:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.metrics.putMetricDataV3
- javascript.v3.cloudwatch.actions.PutMetricData
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-getting-metrics.html#cloudwatch-examples-getting-metrics-publishing-custom
Expand Down Expand Up @@ -171,7 +171,7 @@ cloudwatch_GetMetricStatistics:
cloudwatch_PutMetricAlarm:
title: Create a &CW; alarm that watches a metric using an &AWS; SDK
title_abbrev: Create an alarm that watches a metric
synopsis: create an &CWlong; alarm that watches a metric.
synopsis: create or update an &CWlong; alarm and associate it with the specified metric, metric math expression, anomaly detection model, or Metrics Insights query.
category:
languages:
Kotlin:
Expand Down Expand Up @@ -230,10 +230,10 @@ cloudwatch_PutMetricAlarm:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.alarms.putMetricAlarmV3
- javascript.v3.cloudwatch.actions.PutMetricAlarm
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-putmetricalarm
Expand Down Expand Up @@ -297,10 +297,10 @@ cloudwatch_DescribeAlarmsForMetric:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.alarms.describeAlarmsV3
- javascript.v3.cloudwatch.actions.DescribeAlarms
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-describing
Expand Down Expand Up @@ -397,10 +397,10 @@ cloudwatch_EnableAlarmActions:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.alarms.enableAlarmActionsV3
- javascript.v3.cloudwatch.actions.EnableAlarmActions
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-using-alarm-actions.html#cloudwatch-examples-using-alarm-actions-enabling
Expand Down Expand Up @@ -481,10 +481,10 @@ cloudwatch_DisableAlarmActions:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.alarms.disableAlarmActionsV3
- javascript.v3.cloudwatch.actions.DisableAlarmActions
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-using-alarm-actions.html#cloudwatch-examples-using-alarm-actions-disabling
Expand Down Expand Up @@ -557,10 +557,10 @@ cloudwatch_DeleteAlarms:
excerpts:
- description: Create the client in a separate module and export it.
snippet_tags:
- cloudwatch.JavaScript.createclientv3
- javascript.v3.cloudwatch.client
- description: Import the SDK and client modules and call the API.
snippet_tags:
- cw.JavaScript.alarms.deleteAlarmsV3
- javascript.v3.cloudwatch.actions.DeleteAlarms
- sdk_version: 2
github: javascript/example_code/cloudwatch
sdkguide: sdk-for-javascript/v2/developer-guide/cloudwatch-examples-creating-alarms.html#cloudwatch-examples-creating-alarms-deleting
Expand Down
101 changes: 41 additions & 60 deletions javascriptv3/example_code/cloudwatch/README.md
Original file line number Diff line number Diff line change
@@ -1,79 +1,60 @@
# Amazon Cloudwatch JavaScript SDK v3 code examples
## Purpose
The code examples in this directory demonstrate how to work with Amazon CloudWatch
# Cloudwatch examples for the AWS SDK for JavaScript (v3)

## Overview

These code examples demonstrate how to work with Amazon CloudWatch
using the AWS SDK for JavaScript (v3).

Amazon CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes.
Amazon CloudWatch provides a reliable, scalable, and flexible monitoring solution that you can start using within minutes.
You no longer need to set up, manage, and scale your own monitoring systems and infrastructure.

## ⚠️ Important

- Running this code might result in charges to your AWS account.
- Running the tests might result in charges to your AWS account.
- We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
- This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).

## Code examples
### API examples
- [Delete CloudWatch alarms](src/deleteAlarms.js)
- [Describe CloudWatch alarms](src/describeAlarms.js)
- [Disable CloudWatch alarm actions](src/disableAlarmActions.js)
- [Enable CloudWatch alarm actions](src/enableAlarmActions.js)
- [List CloudWatch metrics](src/listMetrics.js)
- [Put CloudWatch metric alarms](src/putMetricAlarm.js)
- [Put CloudWatch metric data](src/putMetricData.js)

**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).

## Important
### Single Actions

- As an AWS best practice, grant this code least privilege, or only the
permissions required to perform a task. For more information, see
[Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)
in the *AWS Identity and Access Management User Guide*.
- This code has not been tested in all AWS Regions. Some AWS services are
available only in specific AWS Regions. For more information, see the
[AWS Regional Services List](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/)
on the AWS website.
- Running this code might result in charges to your AWS account.
- [Create an alarm that watches a metric](actions/putMetricAlarm.js)
- [Delete alarms](actions/deleteAlarms.js)
- [Describe alarms for a metric](actions/describeAlarms.js)
- [Disable alarm actions](actions/disableAlarmActions.js)
- [Enable alarm actions](actions/enableAlarmActions.js)
- [List metrics](actions/listMetrics.js)
- [Put data into a metric](actions/putMetricData.js)

## Running the code
**Note**: All code examples are written in ECMAscript 6 (ES6). For guidelines on converting to CommonJS, see
[JavaScript ES6/CommonJS syntax](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/sdk-examples-javascript-syntax.html).

### Prerequisites
- An AWS account. To create an account, see [How do I create and activate a new AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/) on the AWS Premium Support website.
- AWS credentials. For details, see [Setting credentials in Node.js](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html) in the
*AWS SDK for Javascript (v3) Developer Guide*.
## Run the examples

1. Clone the [AWS Code Samples repo](https://github.com/awsdocs/aws-doc-sdk-examples) to your local environment.
See [the Github documentation](https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/cloning-a-repository) for
instructions.
### Prerequisites

2. Install the dependencies listed in the package.json.
1. [Set up AWS SDK for JavaScript](../README.md).
1. Run `npm i`.

**Note**: These dependencies include the client modules for the AWS services that this example requires,
which is *@aws-sdk/client-cloudwatch*.
```
npm install node -g
cd javascriptv3/example_code/cloudwatch
npm install
```
3. In your text editor, update user variables specified in the ```Inputs``` section of the sample file.
### Instructions

4. Run sample code:
```
cd src
node [example name].js
```
**Run a single action**
1. Run `node ./actions/<fileName>`.
OR
1. Import `./actions/fileName` into another module.

## Unit tests
## Tests

`Unit tests<./tests>`_ are provided for most examples, using the `Jest <https://jestjs.io/>`_ framework.
⚠️ Running the tests might result in charges to your AWS account.

For example, to run tests on the cloudwatch folder, enter the following sequence of commands at the command prompt:
1. Run `npm i`.
1. Run `npm run integration-test`.

```
npm install node -g
cd javascriptv3/example_code/cloudwatch/tests
npm install
npm test
```
## Additional resources

## Resources
- [AWS SDK for JavaScript v3 repo](https://github.com/aws/aws-sdk-js-v3) .
- [AWS SDK for JavaScript v3 Developer Guide](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/cloudwatch-examples.html)
- [AWS SDK for JavaScript v3 API Reference Guide - Amazon Cloudwatch client module](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudwatch/index.html)
- [Amazon CloudWatch User Guide](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/WhatIsCloudWatch.html)
- [Amazon CloudWatch API reference](https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/Welcome.html)
- [Amazon CloudWatch Client - AWS SDK for JavaScript (v3)](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-cloudwatch/index.html)

Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0
23 changes: 23 additions & 0 deletions javascriptv3/example_code/cloudwatch/actions/delete-alarms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// snippet-start:[javascript.v3.cloudwatch.actions.DeleteAlarms]
import { DeleteAlarmsCommand } from "@aws-sdk/client-cloudwatch";
import { client } from "../libs/client.js";

const run = async () => {
const command = new DeleteAlarmsCommand({
AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm.
});

try {
return await client.send(command);
} catch (err) {
console.error(err);
}
};

export default run();
// snippet-end:[javascript.v3.cloudwatch.actions.DeleteAlarms]
23 changes: 23 additions & 0 deletions javascriptv3/example_code/cloudwatch/actions/describe-alarms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// snippet-start:[javascript.v3.cloudwatch.actions.DescribeAlarms]
import { DescribeAlarmsCommand } from "@aws-sdk/client-cloudwatch";
import { client } from "../libs/client.js";

const run = async () => {
const command = new DescribeAlarmsCommand({
AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm.
});

try {
return await client.send(command);
} catch (err) {
console.error(err);
}
};

export default run();
// snippet-end:[javascript.v3.cloudwatch.actions.DescribeAlarms]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// snippet-start:[javascript.v3.cloudwatch.actions.DisableAlarmActions]
import { DisableAlarmActionsCommand } from "@aws-sdk/client-cloudwatch";
import { client } from "../libs/client.js";

const run = async () => {
const command = new DisableAlarmActionsCommand({
AlarmNames: process.env.CLOUDWATCH_ALARM_NAME, // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm.
});

try {
return await client.send(command);
} catch (err) {
console.error(err);
}
};

export default run();
// snippet-end:[javascript.v3.cloudwatch.actions.DisableAlarmActions]
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// snippet-start:[javascript.v3.cloudwatch.actions.EnableAlarmActions]
import { EnableAlarmActionsCommand } from "@aws-sdk/client-cloudwatch";
import { client } from "../libs/client.js";

const run = async () => {
const command = new EnableAlarmActionsCommand({
AlarmNames: [process.env.CLOUDWATCH_ALARM_NAME], // Set the value of CLOUDWATCH_ALARM_NAME to the name of an existing alarm.
});

try {
return await client.send(command);
} catch (err) {
console.error(err);
}
};

export default run();
// snippet-end:[javascript.v3.cloudwatch.actions.EnableAlarmActions]
31 changes: 31 additions & 0 deletions javascriptv3/example_code/cloudwatch/actions/list-metrics.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

// snippet-start:[javascript.v3.cloudwatch.actions.ListMetrics]
import { ListMetricsCommand } from "@aws-sdk/client-cloudwatch";
import { client } from "../libs/client.js";

export const run = async () => {
// Use the AWS console to see available namespaces and metric names. Custom metrics can also be created.
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/viewing_metrics_with_cloudwatch.html
const command = new ListMetricsCommand({
Dimensions: [
{
Name: "LogGroupName",
},
],
MetricName: "IncomingLogEvents",
Namespace: "AWS/Logs",
});

try {
return await client.send(command);
} catch (err) {
console.error(err);
}
};

export default run();
// snippet-end:[javascript.v3.cloudwatch.actions.ListMetrics]
Loading

0 comments on commit 31cac4f

Please sign in to comment.