Using an AWS lambda to check the price of item and get informed when it drops under a certain threshold by e-mail
This tutorial guides you through creating an AWS Lambda function to monitor product prices on Amazon and send alerts when the price drops below a predefined threshold. Notifications will be sent via AWS SNS, per e-mail.
The entire setup will incur no additional cost, it is well within the AWS free tier.
Prerequisites:
AWS Account with access to AWS Lambda, EventBridge and SNS.
The Lambda Function
Below is the Python code for the Lambda function:
import json
import requests
import re
from bs4 import BeautifulSoup
import boto3 # For SNS notifications
# Function to handle the Lambda event
def lambda_handler(event, context):
# Define the product to monitor
products = [
{
"url": 'https://www.amazon.de/-/en/AMD-Ryzen-7800X3D-Technology-Architecture/dp/B0BTZB7F88/',
"name": "AMD Ryzen 7 7800X3D",
"threshold": 600.0 # Price threshold in Euros
}
]
messages = []
for product in products:
price = check_price_amazon(product["url"])
if price is not None and price < product["threshold"]:
# Send notification
send_sns_notification(price, product["url"], product["name"])
messages.append(f'Price for {product["name"]} dropped to {price} EUR! Notification sent.')
else:
messages.append(f'Price for {product["name"]} is still above the threshold.')
return {
'statusCode': 200,
'body': json.dumps(' '.join(messages))
}
# Function to check the price on Amazon
def check_price_amazon(url):
HEADERS = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive"
}
try:
response = requests.get(url, headers=HEADERS, timeout=10)
if response.status_code == 200:
soup = BeautifulSoup(response.content, 'html.parser')
price_whole = soup.find('span', {'class': 'a-price-whole'})
price_fraction = soup.find('span', {'class': 'a-price-fraction'})
if price_whole and price_fraction:
# Clean the price string by removing non-numeric characters
price_whole_cleaned = re.sub(r'[^\d]', '', price_whole.text)
price_fraction_cleaned = re.sub(r'[^\d]', '', price_fraction.text)
# Combine whole and fractional parts into a valid float
price = float(f"{price_whole_cleaned}.{price_fraction_cleaned}")
return price
elif response.status_code == 503:
print("Server returned 503. Possible bot detection.")
except requests.exceptions.RequestException as e:
print(f"Request failed: {e}")
return None
# Function to send a notification via AWS SNS
def send_sns_notification(price, product_url, product_name):
sns_client = boto3.client('sns')
topic_arn = 'SNS_TOPIC_ARN' # Replace with your SNS Topic ARN
message = f"The price for {product_name} has dropped to {price} EUR. Check it out here: {product_url}"
response = sns_client.publish(
TopicArn=topic_arn,
Message=message,
Subject="Price Alert!"
)
print(f"SNS notification sent with response: {response}")
Configuration
1. Set Lambda Timeout
-
Set the Lambda timeout to 10 seconds:
- Open the Lambda function in the AWS Console.
- Go to Configuration > General Configuration.
- Set Timeout to 10 seconds.
2. Add Layers to the lambda
Add the following layers for requests and BeautifulSoup. Use the appropriate Python runtime version and region-specific ARNs from the Klayers repository.
3. Add SNS Permissions to Lambda Role
Open the IAM Role assigned to your Lambda function and add the SNS publish to it:
4. Set Up SNS
- Step 1: Go to the SNS Console.
- Step 2: Create a new standard topic.
- Sub-step: Provide a unique name.
- Sub-step: Copy the Topic ARN and replace SNS_TOPIC_ARN in the Lambda code with it.
- Step 3: Configure subscriptions.
- Create a new subscription to the SNS Topic.
- Choose Email as the protocol and provide your email address.
- Confirm the subscription via the confirmation email.
5. Add an EventBridge Trigger to the Lambda
Configure an EventBridge Rule to trigger the Lambda periodically (daily in the example):
6. Testing the Lambda function:
Open the Lambda test console and use an empty json event.
Execute the function and verify the subscribed email.
7. Results:
WHEN AND IF the price of your item/items drops bellow the set threshold, you will receive a mail like this: