This solution enables you to customize CloudWatch alarm email alerts using Amazon Simple Email Service (Amazon SES) and AWS Lambda. You will be able to extract the relevant bits of information that you need from the alarm message and display them in an easy-to-read HTML format.
The diagram shows the following workflow:
- Amazon CloudWatch alarm state changes and sends notification alert to the associated SNS topic.
- Amazon SNS will invoke the subscribed Lambda function.
- Lambda function will parse the alarm message and call the Amazon SES API.
- Amazon SES will apply the HTML formatting based on a specified template and send the formatted email.
-
AWS account & IAM User or Role
-
AWS CLI v2 installed.
-
A verified Amazon SES identity which will be used as the sender email.
-
Clone the amazon-cloudwatch-alarm-formatted-email github repository to your computer using the following command:
git clone https://github.com/aws-samples/amazon-cloudwatch-alarm-formatted-email
-
Configure the AWS credential keys to authenticate and authorize the AWS CLI commands on your account.
-
Verify an email identity in Amazon SES. This is the email address that you will use as sender:
aws ses verify-email-identity --email-address <Your email address>
After issuing this command, you should recive an email for the verification request. You will need to click on the URL link to confirm that you are authorized to use this email address.
-
Create an S3 bucket that will be used to store and access the CWAlarmFormattedEmail Lambda function deployment package if you don't already have one.
-
Create a zip file containing the CWAlarmFormattedEmail Lambda function code located in the src directory. This is the deployment package of the AWS Lambda function.
For MAC:
zip -j amazon-cloudwatch-alarm-formatted-email.zip src/*
For Windows (Powershell):
Compress-Archive ./src/* amazon-cloudwatch-alarm-formatted-email.zip
-
Copy the amazon-cloudwatch-alarm-formatted-email.zip file to your S3 bucket:
aws s3 cp amazon-cloudwatch-alarm-formatted-email.zip s3://<bucket name>
-
Deploy the CloudFormation stack that will create the:
- AWS Lambda function along with its IAM execution role
- Amazon SES HTML template email
- Amazon SNS topic along with its topic policy
aws cloudformation create-stack --stack-name amazon-cloudwatch-alarm-formatted-email \ --template-body file://cloudformation/cwalarm-formatted-email.cfn.json \ --capabilities CAPABILITY_IAM CAPABILITY_AUTO_EXPAND \ --parameters ParameterKey=pS3DeploymentBucket,ParameterValue=<S3 bucket that contains Lambda zip package> \ ParameterKey=pS3DeploymentKey,ParameterValue=<S3 key of the Lambda deployment package (.zip)> \ ParameterKey=pEmailSource,ParameterValue=<Sender email address. Should be a verified identity in SES> \ ParameterKey=pEmailToAddress,ParameterValue=<Receiver (To:) email address> \ ParameterKey=pEmailCcAddress,ParameterValue=<Receiver (Cc:) email address> \ ParameterKey=pEmailReplyToAddress,ParameterValue=<Reply To email address> \ --region <enter your aws region id, e.g. "us-east-1">
After the deployment, you should be able to test out the soltuion by creating a CloudWatch Alarm based on EC2 metric and associating it to the SNS topic deployed by the CloudFormation stack.
Once the alarm is triggered, you should receive an HTML formatted email notification to the address specified in the pEmailToAddress
CloudFormation paramter.
Note: Although most of the time each message will be delivered to your application exactly once, the distributed nature of Amazon SNS and transient network conditions could result in occasional, duplicate messages at the subscriber end.
To change how the email looks, you need to update the HTML code in the HtmlPart
property found in the Cloudformation stack cwalarm-formatted-email.cfn.json
The CWAlarmFormattedEmail Lambda function extracts some common values from the alarm event message (account-id, region etc.) and an EC2 specific value (instance-id). To use the solution with non-EC2 metrics, make sure to update the Lambda function code.
Amazon SNS provides in-transit encryption by default. It is recommended to also enable server side encryption using AWS KMS Customer managed CMK.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.