This example demonstrates how to build a lightweight, serverless API using AWS SAM (Serverless Application Model) while integrating real-time error monitoring and email alerts using CloudWatch Alarms and SNS. This pattern is ideal for production-ready serverless applications that need observability without heavy tooling.


Project Structure

The project includes the following core components:

  • functions/: Contains the Lambda function that handles HTTP requests:
    • app.mjs: Returns a simple response (e.g., “Hello World”).
  • template.yaml: The SAM template defining resources including:
    • Lambda Function (hello handler)
    • API Gateway (to expose the function via HTTP)
    • CloudWatch Alarm (monitors API 5XX errors)
    • SNS Topic & Subscription (sends email alerts)

How It Works

This architecture sets up an API and monitors its availability in real time:

  1. HTTP Request Handling:
    • A user sends a GET request to the /hello endpoint.
    • This triggers a Lambda function (app.mjs) via API Gateway.
  2. Error Monitoring:
    • If the API Gateway returns a 5XX error (e.g., internal server error), a CloudWatch Alarm is triggered.
    • The alarm is configured to fire when at least one 5XX error occurs in a 60-second window.
  3. Email Notifications:
    • The alarm action is tied to an SNS Topic.
    • An email subscription on the topic sends a notification to a pre-configured email address when the alarm state is ALARM.

This setup ensures any runtime failures in the API layer are immediately communicated to the dev/ops team via email, improving visibility and response time.


Project Repository: https://github.com/OmarMakled/aws-sam-alarm-api