The purpose of this project is to create an AWS-based serverless application that scrapes the prices of Sunspel's Riviera polo shirts on a daily basis and send me an email when there is a discount. The core functionality is an AWS Lambda function that is scheduled to run on a daily basis using an EventBridge rule, AWS SAM is used to create all the necessary AWS resources to get this application up and running.
This repo contains source code and supporting files for a serverless application that can be deploy with the SAM CLI. It includes the following files and folders.
- sunspel-discount-notifier - Code for the application's Lambda function.
- template.yaml - A template that defines the application's AWS resources.
The application uses several AWS resources, including Lambda functions, an EventBridge rule, and an SNS topic. These resources are defined in the template.yaml
file in this project.
Example email content sent by the application when a discount is found.
Minimum discount percent value is set to 50%.
-
An AWS account
-
SAM CLI - Install the SAM CLI
-
Python 3 - Install Python 3
-
Docker - Install Docker community edition
-
An pre-existing S3 bucket
-
A JSON file named containing email details with the following structure:
{ "sender_email": "[email protected]" # Address to send emails from as string "password": "passwordvalue" # Password for sender_email as string "host": "mail.example.com" # Email host server as string "port": 587 # Port number needed to communicate with host server as integer "receiver_email": "[email protected]" # Address to receive emails (can be same as sender_email) as string }
The following steps need to be taken in the template.yaml
file before the application can be deployed:
- Replace the value of the
EMAIL_SECRET_BUCKET
global environment variable with your pre-existing S3 bucket name. - Similarly, replace the value of the
EMAIL_SECRET_JSON_KEY
global environment variable with the name of your JSON file containing email details placed in the root of your pre-existing S3 bucket. - (Optional) Adjust the value of
MIN_DISCOUNT_PERC
to your preference, by default this is set to 50% meaning that an email will only be sent once a product reaches a discount of 50%.
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
Build this application with the sam build --use-container
command. The use-container
option makes it so that the build happens inside a Lambda-like container.
sam build --use-container
For each of the Lambda functions, the SAM CLI installs dependencies defined in requirements.txt
, creates a deployment package, and saves it in the .aws-sam/build
folder.
To deploy the application for the first time, run the following command in your shell after building:
sam deploy --guided
This command will package and deploy this application to AWS, with a series of prompts:
- Stack Name: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching this project's function.
- AWS Region: The AWS region you want to deploy this app to.
- SNS Email Parameter: The email address to send execution failure notifications.
- Confirm changes before deploy: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes.
- Allow SAM CLI IAM role creation: Many AWS SAM templates, including this one, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the
CAPABILITY_IAM
value forcapabilities
must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass--capabilities CAPABILITY_IAM
to thesam deploy
command. - Save arguments to samconfig.toml: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run
sam deploy
without parameters to deploy changes to this application.
To delete the deployed application, use the AWS CLI. Assuming you used this project name for the stack name, you can run the following:
aws cloudformation delete-stack --stack-name sunspel-discount-notifier