AWS Cloudwatch Snapshot Graphs Alert Context
Code samples related to "Reduce Time to Resolution with Amazon CloudWatch Snapshot Graphs and Alerts” blog post published on the AWS DevOps blog. This sample demonstrates how to integrate CloudWatch snapshot graphs with email alerts to add more context to the alert.
Note that whilst the CloudWatch snapshot graphs API is available in all public regions, the email service used in this sample (Simple Email Service) is only available in a limited number of regions. See the AWS service availability matrix for a full list of SES regions. To run this sample in a region that is not supported by SES you will need to use a different email service.
This sample code is made available under a modified MIT license. See the LICENSE file.
This project includes code that is intended to run as an AWS Lambda function. Full instructions can be found in the associated blog post on the AWS DevOps blog.
Setup the Lambda:
- Download the repository.
- Run 'npm install' to create dependencies.
- ZIP the entire project 'zip -r snapshotgraphsalarmdemo.zip ./*'
- Create a new NodeJS Lambda function and upload the ZIP as the source code.
- Set handler to emailer.myHandler.
- Increase execution timeout to 30s.
- This Lambda function requires an IAM role with permissions for Amazon SES, Amazon SNS (readonly) and Amazon CloudWatch (readonly). This is in addition to the AWSLambdaBasicExecutionRole.
- Set the following environment variables - EMAIL_TO_ADDRESS, EMAIL_FROM_ADDRESS, MAIL_SERVER_REGION. Note that the email addresses have to be verified in the Amazon Simple Email Service before emails will be sent. SES is only available in a limited number of regions and you can run the Lambda and SES in different regions by specifying MAIL_SERVER_REGION.
Building the solution:
- Create an SNS topic.
- Create a CloudWatch alarm and set the SNS topic as the target.
- Subscribe the Lambda to the SNS topic.
When the CloudWatch alarm is triggered the Lambda function will send an email to the address set in the Lambda environment variable EMAIL_TO_ADDRESS.
- Check that Simple Email Service (SES) is available in your region.
- Check that your email address has been verified in Amazon Simple Email Service.
- View the Lambda log file in CloudWatch.
See the CloudWatch API documentation for GetMetricWidgetImage for more information.