Skip to content

Conversation

@harsha-at-magicshake
Copy link

@harsha-at-magicshake harsha-at-magicshake commented Apr 24, 2025

Overview

This PR enhances the MongoDB backup Lambda function with the following features:

  • SNS Notification Integration: The Lambda now sends notifications to an AWS SNS topic on both backup success and failure events.
  • Configurable AWS Region: The AWS region is now configurable via environment variables, ensuring compatibility and proper functioning of the AWS SDK.
  • Improved Error Handling: All critical failure points (directory creation, mongodump, zipping, S3 upload, retention logic) now trigger both Slack and SNS notifications if configured.
  • Local Testing Script: Added test-local.js to allow local execution and debugging of the Lambda function.
  • .env and Documentation Updates: The .env file and README have been updated to reflect new configuration options and usage instructions.

Detailed Changes

1. index.js

  • Added SNS client initialization with region support:
    const sns = new AWS.SNS({ region: process.env.AWS_REGION || "us-east-1" });
  • Added a notifySNS helper function to send notifications to the configured SNS topic.
  • Modified error handling and success completion logic to call notifySNS with appropriate messages.
  • Ensured all notifications are sent asynchronously and errors in notification delivery are logged but do not interrupt the backup flow.

2. .env

  • Added SNS_TOPIC_ARN for specifying the SNS topic to send notifications to.
  • Added AWS_REGION for specifying the AWS region used by the SDK.

3. test-local.js

  • Added a script to allow running and testing the Lambda backup logic locally.
  • Loads environment variables from .env using dotenv.
  • Calls the Lambda handler directly, simulating an AWS Lambda execution environment.

How to Use

  1. Configure Environment Variables:

    • Set SNS_TOPIC_ARN to your SNS topic ARN (e.g., arn:aws:sns:us-east-1:xxxxxxxxxxxx:mongodb-backup-events).
    • Set AWS_REGION to your preferred AWS region (e.g., us-east-1).
  2. SNS Setup:

    • Create an SNS topic and subscribe your email or other endpoints.
    • Confirm your subscription as required.
  3. Deploy & Test:

    • Deploy the updated Lambda function.
    • Trigger the Lambda (manually or via schedule) and verify you receive SNS notifications for both success and failure cases.
    • To test locally, run:
      node test-local.js

Example .env Additions

SNS_TOPIC_ARN=arn:aws:sns:us-east-1:xxxxxxxxxxxx:mongodb-backup-events
AWS_REGION=us-east-1

Files Changed

  • index.js – Main Lambda logic, SNS integration, region support, improved error handling.
  • .env – Added SNS and region configuration variables.
  • test-local.js – Added for local development and debugging.

Notes

  • Slack notifications remain supported and are unaffected by these changes.
  • If SNS_TOPIC_ARN is not set, SNS notifications are skipped with a warning.
  • If AWS_REGION is not set, it defaults to us-east-1.

@harsha-at-magicshake harsha-at-magicshake changed the title added local script to test, with SNS topic Add SNS Notification Support, Region Configuration, and Local Testing Script to MongoDB Backup Lambda Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant