go/cloudwatch/README.md
These examples demonstrate how to perform the following tasks in your default AWS Region using your default credentials:
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.
Each operation is in a sub-folder. Some operations depend upon results from another operation. For example, to create a target, you need the ARN of the Lambda function that is invoked.
Most unit tests for the operations require that you fill in some values in *config.json".
This operation creates a new metric in a namespace.
go run CreateCustomMetric.go -n NAMESPACE -m METRIC-NAME -u UNITS [-v VALUE] -dn DIMENSION-NAME -dv DIMENSION-VALUE
where:
You must supply similar values in config.json for the unit test.
The unit test:
This operation creates an IAM role that grants permission to CloudWatch Events as step one of the workflow of creating an event.
go run CreateRole -p POLICY -r ROLE
where:
You must supply similar values in config.json for the unit test.
The unit test:
This operation creates a rule that watches for events on a schedule as step two of the workflow of creating an event.
go run CreateRule -r RULE -a ROLE-ARN -s SCHEDULE`
where:
rate(5 minutes) to have the rule run every five minutes.You must supply similar values in config.json for the unit test.
The unit test:
This operation creates an event target that is the resource that is invoked when the rule is triggered as step five of the workflow of creating an event.
go run CreateTarget.go -r RULE -l LAMBDA-ARN -t TARGET
where:
You must supply a Lambda ARN value in config.json for the unit test.
The unit test:
This operation deletes an alarm.
go run DeleteAlarm.go -a ALARM
where:
You can supply a similar value in config.json for the unit test.
The unit test:
This operation lists your Amazon CloudWatch alarms.
go run DescribeAlarms.go
This operation disables an alarm.
go run DisableAlarm -a ALARM
where:
You can supply a similar value in config.json for the unit test.
The unit test:
This operation creates and enables an alarm when the CPU utilization of an Amazon EC2 instance goes above 70%, triggering a reboot of the instance.
go run EnableAlarm -n INSTANCE-NAME -i INSTANCE-ID -a ALARM-NAME
where:
You must supply similar values in config.json for the unit test.
The unit test:
This operation lists the log events for a log stream in a log group.
go run GetLogEvents.go -l LOG-GROUP -s LOG-STREAM [-l LIMIT]
where:
You can supply similar values in config.json for the unit test.
The unit test:
The main.go file in this folder logs any CloudWatch event it receives. Use the packageLambda.bat file for Windows or the packageLambda.sh file to create main.zip from main.go. Creating the ZIP file is step three of the workflow of creating an event. Note that you still must upload the Lambda function to a bucket before you can use it as a target. See UploadLambdaFunction.
packageLambda.bat OR packageLambda.sh
This operation lists up to 500 of your metrics.
go run ListMetrics.go
This operation sends an event as the final step of the workflow of creating an event. It sends the event that is defined in event.json.
go run SendEvent.go -l LAMBDA-ARN
where:
You must supply a similar value in config.json for the unit test.
The unit test:
This operation uploads a Lambda function to an Amazon S3 bucket as step four of the workflow of creating an event.
go run UploadLambdaFunction.go -z ZIP-FILE -b BUCKET -f LAMBDA-FUNCTION [-h HANDLER] -a ROLE-ARN [-r RUNTIME]
where:
You can supply similar values in config.json for the unit test. If you do not, the unit test creates and deletes these resources.
The unit test:
Unit tests should delete any resources they create. However, they might result in charges to your AWS account.
To run the unit tests, navigate to a sub-folder and enter:
go test
You should see something like the following, where PATH is the path to folder containing the Go files:
PASS
ok PATH 6.593s
If you want to see any log messages, enter:
go test -test.v
You should see some additional log messages. The last two lines should be similar to the previous output shown.