In today’s fast-paced world, maintaining a healthy lifestyle often requires timely reminders, whether for taking medication, drinking water, or simply standing up to stretch. By leveraging AWS Lambda and Gmail, you can create a serverless email reminder system that keeps you on track. This guide will walk you through building a serverless email reminder using AWS Lambda, Python, and Gmail.
Introduction: Leveraging AWS Lambda for Health Reminders
AWS Lambda is a powerful, serverless compute service that allows you to run code in response to events and automatically manages the underlying compute resources. With Lambda, you can create a highly scalable reminder system without worrying about server management. By integrating with Gmail, you can send timely email reminders to yourself or others, ensuring that health goals are met consistently.
Preparing Your Gmail Account for Automated Emails
To send emails via Gmail, you’ll need to set up your Gmail account for automated access. This typically involves enabling “Less Secure Apps” or creating an App Password if you have 2-Step Verification enabled. Here’s how to get started:
- Enable Less Secure Apps (Not Recommended):
- Log into your Gmail account.
- Navigate to your Google Account settings.
- Under the “Security” tab, find the “Less secure app access” section and turn it on.
- Generate an App Password (Recommended):
- Log into your Gmail account and go to “Security” in your Google Account.
- Under “Signing in to Google,” select “App Passwords.”
- Generate an app password and note it down in your Lambda function.
Sending Test Emails with Python’s smtplib
Before deploying to AWS Lambda, it’s a good idea to test sending emails locally using Python’s smtplib. Here’s a simple Python script to send a test email:
import smtplib
from email.mime.text import MIMEText
def send_test_email():
sender_email = “your_email@gmail.com”
receiver_email = “recipient_email@gmail.com”
password = “your_app_password”
msg = MIMEText(“This is a test email from your reminder system.”)
msg[‘Subject’] = “Health Reminder”
msg[‘From’] = sender_email
msg[‘To’] = receiver_email
with smtplib.SMTP_SSL(‘smtp.gmail.com’, 465) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
send_test_email()
Run this script to ensure your Gmail setup is working correctly.
Creating Your Reminder Function in AWS Lambda
Next, you’ll move the functionality to AWS Lambda. To start:
- Log in to the AWS Management Console and navigate to AWS Lambda.
- Create a new Lambda function and choose Python as the runtime.
- Copy the email-sending code from your local test script into the Lambda function editor.
Here’s a simplified version of Lambda:
import smtplib
from email.mime.text import MIMEText
import os
def lambda_handler(event, context):
sender_email = os.environ[‘SENDER_EMAIL’]
receiver_email = os.environ[‘RECEIVER_EMAIL’]
password = os.environ[‘EMAIL_PASSWORD’]
msg = MIMEText(“Time to drink water! Stay hydrated.”)
msg[‘Subject’] = “Health Reminder”
msg[‘From’] = sender_email
msg[‘To’] = receiver_email
with smtplib.SMTP_SSL(‘smtp.gmail.com’, 465) as server:
server.login(sender_email, password)
server.sendmail(sender_email, receiver_email, msg.as_string())
return {
‘statusCode’: 200,
‘body’: ‘Reminder sent successfully!’
}
Scheduling Hourly Reminders with CloudWatch EventBridge
AWS CloudWatch EventBridge allows you to trigger Lambda functions on a schedule. To set up an hourly reminder:
- Navigate to CloudWatch in the AWS Console.
- Go to “Rules” under “Events” and click “Create rule.”
- Set the event source to “EventBridge” and choose a schedule pattern.
- For an hourly schedule, use rate(1 hour).
- Set the target as the Lambda function you created.
This setup ensures your Lambda function runs every hour and sends an email reminder as scheduled.
Protecting Your Gmail Credentials with Environment Variables
Storing sensitive information such as email credentials in code is risky. Instead, use Lambda environment variables to store and access these securely:
- Go to your Lambda function configuration.
- Under the “Environment variables” section, add three variables: SENDER_EMAIL, RECEIVER_EMAIL, and EMAIL_PASSWORD.
- Enter the appropriate values, ensuring that EMAIL_PASSWORD is the App Password you generated earlier.
This approach keeps your credentials secure while making them easily accessible in your Lambda function.
Testing Your Reminder and Extending Functionality
With everything set up, it’s time to test your reminder:
- Manually trigger the Lambda function from the AWS Console and check if the email is received.
- Review the CloudWatch logs to ensure there are no errors.
To extend the functionality, consider adding different types of reminders or integrating with other AWS services like SNS for SMS reminders.
Conclusion: Building Custom Reminders for a Healthier Lifestyle
Creating a serverless email reminder system with AWS Lambda and Gmail is a straightforward yet powerful way to automate health habits. By leveraging cloud infrastructure, you can scale this system effortlessly and adapt it to various needs, ensuring you stay on track with your health goals.