Yet another AWS service to generate thumbnails. This one is based on AWS Lambda, S3, and SQS.
It can be deployed to AWS or LocalStack using the Serverless Framework.
It also includes:
- Unit tests.
- Functional tests, which are executed against LocalStack.
- Pre-commit hooks: Black, ISort, Flake8, and MyPy.
- A Makefile with useful commands.
- An image is stored to the images bucket
- The image creation event is queued into an SQS queue
- The lambda function tries to generate the thumbnails
-
- Success: Thumbnails are saved to the thumbnails bucket
- Error:
- The message is retried as many times as configured in the queue
- If it continues to fail, the message is sent to a DLQ
- You can manually invoke another lambda function that dequeues from the DLQ and sends the messages back to the original queue to be retried
The serverless.yml file contains the Serverless configuration to deploy the stack to either AWS or LocalStack.
The lambda functions are located in the functions package. Each AWS Lambda handler function is on a separate file. Common code is in the same package.
Unit tests are in the functions/tests package.
Integration tests are in the integration_tests package.
You can find useful commands in the Makefile.
Python requirements:
- The requirements.txt file contains the essential Python dependencies required by the application logic to run.
- The requirements.dev.txt file contains the Python dependencies you need to have installed in your environment to contribute to the application logic.
- The requirements.test.txt file contains the Python dependencies required to run tests.
npm install -g serverless
pip install localstack
Go to the root directory of this repo and install the plugins:
cd serverless_s3_pipeline
npm i
Follow these instructions to install the AWS CLI.
To interact with LocalStack through the AWS CLI, you can create a profile with dummy region and access key.
Add this to your ~/.aws/config
file:
[profile localstack]
region = us-east-1
output = json
And this to your ~/.aws/credentials
file:
[localstack]
aws_access_key_id = dummyaccesskey
aws_secret_access_key = dummysecretaccesskey
Start LocalStack:
localstack start
Deploy to LocalStack:
serverless deploy --stage local
You should get something like the following. Notice the endpoint URL:
✔ Service deployed to stack thumbnails-service-local
functions:
generate_thumbnails: thumbnails-service-local-generate_thumbnails
retry_from_dlq: thumbnails-service-local-retry_from_dlq
You can alternatively start localstack as a daemon and deploy with a single command:
make deploy_local
make install_git_hooks
make run_git_hooks
make create_virtualenv
make install_requirements
make install_dev_requirements
make install_test_requirements
# Or just
make install_all_requirements
make run_unit_tests
make run_integration_tests
Restarts LocalStack (if running) before deploying.
make deploy_local
make deploy_functions_local
make tail_generate_thumbnails_function_logs
make tail_retry_from_sqs_function_logs
make upload_test_images_to_s3
make show_messages_in_dlq
make retry_from_dql