|
| 1 | +# Account level alerting using Amazon CloudWatch, AWS Lambda and Amazon SNS |
| 2 | + |
| 3 | +This pattern demonstrates an alerting system that leverages an AWS CloudWatch Logs Account-level Subscription filter to trigger a Lambda function, which in turn sends an SNS notification when a specified pattern (in this case, "Exception") is matched in log events across any log group within the account. Currently, there is no out-of-the-box offering from AWS that allows customers to be alerted on specific event patterns across their entire account. |
| 4 | + |
| 5 | +By implementing this pattern, customers can proactively monitor and receive notifications for critical events or errors, such as exceptions or any specific pattern, that may occur in any of their applications or services. This centralized approach to log monitoring and alerting eliminates the need to configure individual alerting mechanisms for each log group or application, thereby streamlining the process and ensuring consistent alerting across the entire AWS account. |
| 6 | + |
| 7 | +Important: |
| 8 | +* This application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 9 | + |
| 10 | +* Additionally, There is a risk of causing an infinite recursive loop with subscription filters that can lead to a large increase in ingestion billing if not addressed. To mitigate this risk, we recommend that you use selection criteria in your account-level subscription filters to exclude log groups that ingest log data from resources that are part of the subscription delivery workflow. Please see [Account-level subscription filters risk](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/SubscriptionFilters-AccountLevel.html) |
| 11 | + |
| 12 | +## Requirements |
| 13 | + |
| 14 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 15 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 16 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 17 | +* [AWS Serverless Application Model](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) (AWS SAM) installed |
| 18 | + |
| 19 | +## Deployment Instructions |
| 20 | + |
| 21 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 22 | + ``` |
| 23 | + git clone https://github.com/aws-samples/serverless-patterns |
| 24 | + ``` |
| 25 | +2. Change directory to the pattern directory: |
| 26 | + ``` |
| 27 | + cd cloudwatch-account-subscription-Lambda-sns-notification |
| 28 | + ``` |
| 29 | +3. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 30 | + ``` |
| 31 | + sam build |
| 32 | +
|
| 33 | + sam deploy --guided --parameter-overrides EmailAddress='[email protected]' LambdaFunctionName='NotificationLambda' |
| 34 | + ``` |
| 35 | +4. During the prompts: |
| 36 | + * Enter a stack name |
| 37 | + * Enter the desired AWS Region |
| 38 | + * Allow SAM CLI to create IAM roles with the required permissions. |
| 39 | +
|
| 40 | + Once you have run `sam deploy --guided` mode once and saved arguments to a configuration file (samconfig.toml), you can use `sam deploy` in future to use these defaults. |
| 41 | +
|
| 42 | +5. Note the outputs from the SAM deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 43 | +
|
| 44 | +## How it works |
| 45 | +
|
| 46 | +The key components of this pattern are: |
| 47 | +
|
| 48 | +1. AWS CloudWatch Logs Account-level Subscription Filter: This filter is configured to match the desired pattern ("Exception" in this case) and send the matching log events to a designated AWS Lambda function. |
| 49 | +
|
| 50 | +2. AWS Lambda Function: This function processes the log events received from the subscription filter and sends a notification to an SNS topic when the specified pattern is matched. |
| 51 | +
|
| 52 | +3. AWS Simple Notification Service (SNS) Topic: This SNS topic receives the notifications from the Lambda function and delivers them to subscribed endpoints, such as email addresses or mobile devices, enabling real-time alerting for critical events. |
| 53 | +
|
| 54 | +
|
| 55 | +This pattern deploys an account-level subscription filter that monitors all existing and new log groups for a matching log pattern of "Exception". This allows you to catch any event containing the "Exception" pattern across all applications within your AWS account. Optionally, you can produce an exception in any application's logs to test this pattern. |
| 56 | +*This pattern creates a new SNS topic with an email subscription. Please confirm the email verification by clicking on the "Confirm subscription" link sent via Amazon SNS.* |
| 57 | +
|
| 58 | +## Testing |
| 59 | +To test this pattern, which monitors all log groups for the "Exception" pattern and sends SNS notifications, follow these steps: |
| 60 | +
|
| 61 | + - Choose an application generating logs in your AWS account. |
| 62 | + - Intentionally introduce an exception or error condition that logs an "Exception" message. |
| 63 | + - Verify the log event containing "Exception" is written to CloudWatch Logs. |
| 64 | + - Monitor for an SNS email notification containing details like the log group name, log stream name, and the log event with the "Exception" pattern. |
| 65 | +
|
| 66 | +This pattern automatically monitors new log groups created, ensuring comprehensive coverage across your AWS account. |
| 67 | +
|
| 68 | +## Cleanup |
| 69 | + |
| 70 | +1. Delete the stack |
| 71 | + ```bash |
| 72 | + aws cloudformation delete-stack --stack-name STACK_NAME |
| 73 | + ``` |
| 74 | +1. Confirm the stack has been deleted |
| 75 | + ```bash |
| 76 | + aws cloudformation list-stacks --query "StackSummaries[?contains(StackName,'STACK_NAME')].StackStatus" |
| 77 | + ``` |
| 78 | +---- |
| 79 | +Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 80 | +
|
| 81 | +SPDX-License-Identifier: MIT-0 |
0 commit comments