These examples demonstrates how to perform several Amazon CloudWatch operations using version 2 of the AWS SDK for Go.
You must have an AWS account, and have your default credentials and AWS Region configured as described in Configuring the AWS SDK for Go in the AWS SDK for Go Developer Guide.
This example creates a new Amazon CloudWatch metric in a namespace.
go run CreateCustomMetricv2.go -n NAMESPACE -m METRIC-NAME -s SECONDS -dn DIMENSION-NAME -dv DIMENSION-VALUE
- NAMESPACE is the namespace for the metric.
- METRIC-NAME is the name of the metric.
- SECONDS is the number of seconds for the metric.
- DIMENSION-NAME is the name of the dimension.
- DIMENSION-VALUE is the value of the dimension.
The unit test accepts similar values in config.json.
This example enables the specified Amazon CloudWatch alarm.
go run CreateEnableMetricAlarmv2.go -n INSTANCE-NAME -i INSTANCE-ID -a ALARM-NAME
- INSTANCE-NAME is the name of the Amazon Elastic Compute Cloud (Amazon EC2) instance for which the alarm is enabled.
- INSTANCE-ID is the ID of the Amazon EC2 instance for which the alarm is enabled.
- ALARM-NAME is the name of the alarm.
The unit test accepts similar values in config.json.
This example displays a list of your Amazon CloudWatch alarms.
go run DescribeAlarmsv2.go
This example disables an Amazon CloudWatch alarm.
go run DisableAlarmv2.go -a ALARM-NAME
- ALARM-NAME is the name of the alarm to disable.
The unit test accepts a similar value in config.json.
This example displays the name, namespace, and dimension name of your Amazon CloudWatch metrics.
go run ListMetricsv2.go
This example sends an Amazon CloudWatch event to Amazon EventBridge.
go run PutEventv2.go -l LAMBDA-ARN -f EVENT-FILE
- LAMBDA-ARN is the ARN of the AWS Lambda function of which the event is concerned.
- EVENT-FILE is the local file specifying details of the event to send to Amazon EventBridge.
The unit test accepts similar values in config.json.
This example displays the metric data points for the provided input in the given time-frame.
go run CreateCustomMetricv2.go -mN METRIC-NAME -n NAMESPACE -dn DIMENSION-NAME -dv DIMENSION-VALUE -id ID -dM DIFFINMINUTES -s STAT -p PERIOD
- NAMESPACE is the namespace for the metric.
- METRIC-NAME is the name of the metric.
- DIMENSION-NAME is the name of the dimension.
- DIMENSION-VALUE is the value of the dimension.
- ID is a short name used to tie the object to the results in the response
- DIFFINMINUTES is the difference in minutes for which the metrics are requested
- STAT is the Statistic to return i.e. SUM, COUNT, AVERAGE etc
- PERIOD is the granularity, in seconds, of the returned data points
The unit test accepts similar values in config.json
- We recommend that you grant this code least privilege, or at most the minimum permissions required to perform the task. For more information, see 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 Regions.
- Running this code might result in charges to your AWS account.
Unit tests should delete any resources they create. However, they might result in charges to your AWS account.
To run a unit test, enter:
go test
You should see something like the following, where PATH is the path to the folder containing the Go files:
PASS
ok PATH 6.593s
If you want to see any log messages, enter:
go test -v
You should see some additional log messages. The last two lines should be similar to the previous output shown.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. SPDX-License-Identifier: Apache-2.0