A serverless application using python3, Dynamo DB, AWS Secrets Manager, SNS & S3
Uses activity data from Strava APIs to generate reports for an athlete Uses access tokens to authorize access to the APIs Runs on a schedule (every hour? TBD) to pull down the latest activities and store them in a DDB table All reports are generated from data held in DDB Any new activities cause the reports to be re-generated Reports are published to a public S3 bucket for presentation
Uses docker to build packages compatible with Lambda service
cron(Minutes Hours Day-of-month Month Day-of-week Year)
All fields are required and time zone is UTC only.
Field | Values | Wildcards |
---|---|---|
Minutes | 0-59 | , - * / |
Hours | 0-23 | , - * / |
Day-of-month | 1-31 | , - * ? / L W |
Month | 1-12 or JAN-DEC | , - * / |
Day-of-week | 1-7 or SUN-SAT | , - * ? / L # |
Year | 192199 | , - * / |
In below example, we use cron
syntax to define schedule
event that will trigger our cronHandler
function every second minute every Monday through Friday
functions:
cronHandler:
handler: handler.run
events:
- schedule: cron(0/2 * ? * MON-FRI *)
Detailed information about cron expressions in available in official AWS docs.
This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc.
In order to deploy with dashboard, you need to first login with:
serverless login
and then perform deployment with:
serverless deploy
After running deploy, you should see output similar to:
Deploying aws-python-scheduled-cron-project to stage dev (us-east-1)
✔ Service deployed to stack aws-python-scheduled-cron-project-dev (205s)
functions:
rateHandler: aws-python-scheduled-cron-project-dev-rateHandler (2.9 kB)
cronHandler: aws-python-scheduled-cron-project-dev-cronHandler (2.9 kB)
There is no additional step required. Your defined schedules becomes active right away after deployment.
In order to test out your functions locally, you can invoke them with the following command:
serverless invoke local --function rateHandler
After invocation, you should see output similar to:
INFO:handler:Your cron function aws-python-scheduled-cron-dev-rateHandler ran at 15:02:43.203145
In case you would like to include 3rd party dependencies, you will need to use a plugin called serverless-python-requirements
. You can set it up by running the following command:
serverless plugin install -n serverless-python-requirements
Running the above will automatically add serverless-python-requirements
to plugins
section in your serverless.yml
file and add it as a devDependency
to package.json
file. The package.json
file will be automatically created if it doesn't exist beforehand. Now you will be able to add your dependencies to requirements.txt
file (Pipfile
and pyproject.toml
is also supported but requires additional configuration) and they will be automatically injected to Lambda package during build process. For more details about the plugin's configuration, please refer to official documentation.