forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup Cloudwatch examples (awsdocs#4034)
* 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
Showing
41 changed files
with
1,853 additions
and
581 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
javascriptv3/example_code/cloudwatch/actions/delete-alarms.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
23
javascriptv3/example_code/cloudwatch/actions/describe-alarms.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
23 changes: 23 additions & 0 deletions
23
javascriptv3/example_code/cloudwatch/actions/disable-alarm-actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
23 changes: 23 additions & 0 deletions
23
javascriptv3/example_code/cloudwatch/actions/enable-alarm-actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
javascriptv3/example_code/cloudwatch/actions/list-metrics.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.