Build a Production-Ready SMS Scheduler with Fastify, AWS, and Vonage - code-examples -

Frequently Asked Questions

You schedule SMS messages using EventBridge by creating a one-time rule with an "at" schedule expression specifying the desired send time in UTC. This rule targets an AWS SNS topic, which then triggers your application's webhook to send the SMS via Vonage. The application uses the AWS SDK to interact with EventBridge and create these scheduled rules.
AWS SNS acts as a decoupling mechanism between the scheduler (EventBridge) and the SMS sending logic. When the scheduled time arrives, EventBridge publishes a message to the designated SNS topic. This message contains the SMS details (phone number, message content), which are then forwarded to your application's webhook for processing and delivery via Vonage. This architecture enhances reliability.
Fastify is a high-performance Node.js web framework chosen for its speed and efficiency. It provides a robust foundation for building the API endpoints needed to handle scheduling requests and process incoming webhook notifications from AWS SNS. Its plugin architecture and schema validation capabilities also contribute to a more organized and maintainable codebase.
Ngrok is beneficial during development and testing when your local Fastify server isn't publicly accessible. Ngrok creates a secure tunnel, providing a public URL that AWS SNS can use to deliver webhook notifications to your locally running server, enabling efficient local development and testing with AWS services.
The provided example demonstrates one-time SMS scheduling using the 'at' expression in EventBridge. For recurring schedules, you'd modify the EventBridge rule to use a 'cron' or 'rate' expression instead of 'at', specifying the desired recurrence pattern. The rest of the architecture (SNS, webhook, Vonage integration) would remain the same.
You'll need a Node.js environment, an AWS account with access to EventBridge and SNS, AWS credentials configured for your application, a Vonage account with a purchased phone number, and a publicly accessible URL for the webhook (ngrok for development, public domain/IP for production). Also, necessary Node packages and AWS resource setup are required.
The application validates the provided 'sendAt' timestamp to ensure it's in the future. If the timestamp is in the past, the scheduling request is rejected with a 400 Bad Request error, preventing attempts to schedule messages for past times.
The PUBLIC_WEBHOOK_URL is essential for AWS SNS to deliver notifications to your Fastify application. It specifies the publicly accessible base URL where your '/webhook/sns' endpoint resides. This URL is used by SNS to send subscription confirmations and notification messages containing SMS details for processing.
Securing the webhook requires verifying the message signature to ensure authenticity and prevent unauthorized requests. This involves verifying the signature against AWS's public key. While not fully implemented in the example, it's crucial for production to confirm the message originates from AWS SNS.
AWS credentials (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY) are required for the application to interact with AWS services. These can be configured as environment variables or placed in the standard ~/.aws/credentials file. Ensure these credentials have the necessary IAM permissions to manage EventBridge rules, targets, and interact with your SNS topic.
You'll need your Vonage API Key, API Secret, and a Vonage phone number capable of sending SMS. These should be obtained from your Vonage API Dashboard and set in the .env file to allow the application to send SMS messages.
You can use any AWS region that supports SNS and EventBridge. The chosen region should ideally be close to your target audience for lower latency and should be consistent throughout your AWS configuration. Specify your chosen region in the AWS_REGION environment variable.