Skip to main content

Configuring a CloudWatch Alarm for Lambda Invocation Errors with SNS Notifications

Hands-On Lab

 

Photo of

Training Architect

Length

00:30:00

Difficulty

Intermediate

AWS Lambda allows us to create functions and just worry about managing our code, while AWS handles the underlying infrastructure. In this hands-on lab, we will create an alarm that notifies us via AWS SNS when a Lambda function invocation fails. We'll be working with the following features of AWS Lambda: - Lambda Console - Function Code - Execution Roles - CloudWatch Alarms - AWS SNS

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Configuring a CloudWatch Alarm for Lambda Invocation Errors with SNS Notifications

Introduction

AWS Lambda allows us to create functions and just worry about managing our code, while AWS handles the underlying infrastructure. In this hands-on lab, we will create an alarm that notifies us via AWS SNS when a Lambda function invocation fails.

We'll be working with the following features of AWS Lambda:

  • Lambda Console
  • Function Code
  • Execution Roles
  • CloudWatch Alarms
  • AWS SNS

Log in to the AWS Management Console using the credentials provided on the lab instructions page. Make sure you are using the us-east-1 (N. Virginia) region.

Create a Lambda Function

  1. In the AWS Management Console, navigate to the Lambda service.
  2. Click Create a function.
  3. Choose Author from scratch.
  4. In the Author from scratch menu, configure the following settings:
    • Name: Failure
    • Runtime: Python 3.6
    • Role: Choose an existing role
    • Existing role: lambda_exec_role_LACA
  5. Click Create function.
  6. Scroll down the page to the code editor.
  7. Select and delete the default code.
  8. Paste in the code that was provided in the lab instructions.
  9. Click Save.

Add a Subscription to an SNS Topic

  1. Navigate to the SNS service in a new browser tab.
  2. Click Topics in the left sidebar.
  3. Click the LambdaAlert function to open it.
  4. Click Create subscription.
  5. In the Create subscription menu, configure the following options:
    • Protocol: SMS
    • Endpoint: (Enter your cell phone number, including the country and area code.)
  6. Click Create subscription.
  7. Click the refresh icon under Subscriptions to verify that the subscription has been confirmed.

Create a CloudWatch Alarm to Monitor the Lambda Function

  1. Navigate to the CloudWatch service in the AWS Management Console.
  2. Click Alarms in the left sidebar.
  3. Click Create Alarm.
  4. Under Lambda Metrics, click Across All Functions.
  5. Select Errors.
  6. Click Next.
  7. On the Create Alarm page, configure the following settings:
    • Name: Failure
    • Description: Failure
    • Is >=: 2
    • For 1 out of: 1 datapoints
    • Treat missing data as: missing
    • Period: 5 Minutes
    • Statistic: Sum
  8. Under Actions, click + Notification, and configure the following settings:
    • Whenever this alarm: State is ALARM
    • Send notification to: LambdaAlert
  9. Click Create Alarm.

Create a Scheduled Event to Trigger the Lambda Function

  1. In the CloudWatch Management Console, click Events in the left sidebar.
  2. Click Create rule.
  3. Under Event Source, select Schedule.
  4. In the Fixed rate of field, type "2".
  5. Under Targets, click + Add target, and configure the following settings:
    • Function: Failure
  6. Click Configure details.
  7. On the Configure rule details page, configure the following settings:
    • Name: Failure_Rule
    • Description: Failure_Rule
    • State: Enabled
  8. Click Create rule.
  9. Click Alarms in the left sidebar.
  10. Wait a few minutes for the alarm state to change to ALARM.
  11. You should receive an SMS alert message from AWS on your cell phone.

Conclusion

Congratulations, you've successfully completed this hands-on lab!