Modern applications such as those running on microservices architectures generate large volumes of data in the form of metrics, logs, and events. Amazon CloudWatch enables you to collect, access, and correlate this data on a single platform from across all your AWS resources, applications, and services that run on AWS.
Amazon CloudWatch enables you to set alarms and automate actions based on either predefined threshold. You can also perform metric math on your data to derive operational and utilization insights. You can also use CloudWatch Events for serverless to trigger workflows with services like AWS Lambda, and Amazon SNS.
In our blog, How to trigger Lambda function using Amazon CloudWatch events and configure CloudWatch alarm to get email notifications using Amazon SNS (Part 1)? we have discussed Amazon CloudWatch, its benefits, features, use cases, composition, CloudWatch logs, CloudWatch synthetics, how does it work, etc. In this blog, we will demonstrate how to trigger lambda functions using CloudWatch events, also how to configure CloudWatch alarms that monitor the lambda function, and send notifications via email when it fails using AWS SNS.
Amazon CloudWatch
Amazon CloudWatch is a monitoring and observability service geared toward DevOps engineers, developers, SREs, and IT managers. To monitor your applications, respond to system-wide performance changes, optimize resource use, and receive a single view of operational health, CloudWatch provides data and actionable insights. It gathers monitoring and operational data in the form of logs, metrics, and events to give you a single view of AWS resources, applications, and services running on both AWS and on-premises servers. To keep your apps running smoothly, you can use CloudWatch to identify abnormal behavior in your environments, trigger alarms, analyze logs and metrics side by side, take automated actions, troubleshoot issues, and find insights.
AWS Lambda
AWS Lambda is a serverless compute service that allows you to run code without having to provision or manage servers, write workload-aware cluster scaling logic, keep event integrations up to date, or manage runtimes. You can run code for nearly any form of application or backend service with Lambda, and you don’t have to worry about administration. Simply upload your code as a ZIP file or container image, and Lambda will assign compute execution power and run your code based on the incoming request or event, at any scale.
You can use over 200 AWS services and SaaS applications to trigger your code, or you can call it directly from any web or mobile app. You may develop Lambda functions in whatever language you choose (Node.js, Python, Go, Java, and more) and build, test, and deploy them using both serverless and container tools like AWS SAM or Docker CLI.
Hands-On
Step 1: Create a Lambda function
Step 2: Test the lambda function
Step 3: Create an SNS topic and subscribe to it
Step 4: Configure the alarm
Step 5: Test the alarm
Create a Lambda function
Sign in to the AWS Management Console and open the AWS Lambda console, choose to Create function
Choose to use a blueprint, Enter canary in the search bar. Choose the lambda-canary blueprint, and then choose Configure.
Then, set the function name, and create a new role from AWS policy templates.
Create a new rule, and enter the setting details as shown below
Enter the environment variables, and click on Create function
CloudWatch Events emits an event every minute, based on the schedule expression. The event triggers the Lambda function, which verifies that the expected string appears on the specified page.
Test the lambda function
Open the functions page, choose the function, and click on Test
Create a new event using the CloudWatch Events event template, click on Create
Invoke the event, on successful execution, the output is shown below
The code is launching the website (site env variable) and checking the value of expected env variable i.e. (What is AWS Lambda?), if it can find the expected value the function is successfully executed, if not then the lambda function fails.
Create SNS topic and subscribe to it
Open SNS console, choose Create topic
Enter the name, choose Create subscription
Create the subscription with the following settings, in endpoint enter the email address where you want to get the notifications
Configure the alarm
Open CloudWatch console, click on Create alarm under Alarms
Create an alarm with lambda-canary error metrics as shown below
Select the statistic as Sum
Choose the threshold as: Whenever Error is >=1, click on Next
Select the SNS topic created, to send the notifications
Give a name to the alarm, click on Next
This will configure CloudWatch alarm with the desired lambda error metric and send the notification to SNS if the threshold value is >=1.
Test the alarm
Update the function configuration to cause the function to return an error, which triggers the alarm
Open the lambda function console page, choose the workfall-lambda-canary function, click on Configuration to edit environment variables.
Click on Edit, set expected to 404, save it
Wait for a minute for the alarm to trigger and get email from Amazon SNS.
Conclusion
In this blog, we learned how to trigger lambda functions based on scheduled patterns using CloudWatch events and configure the CloudWatch alarm to send email notifications via SNS if the lambda function emits an error. In our upcoming blogs, we will discuss more use cases of Amazon CloudWatch. Stay tuned to keep getting all updates about our upcoming new blogs on AWS and relevant technologies.
Meanwhile …
Keep Exploring -> Keep Learning -> Keep Mastering
This blog is part of our effort towards building a knowledgeable and kick-ass tech community. At Workfall, we strive to provide the best tech and pay opportunities to AWS-certified talents. If you’re looking to work with global clients, build kick-ass products while making big bucks doing so, give it a shot at workfall.com/partner today.