This provides a templates for aws lambda test in local environment using localstack or local server instances (i.e. dynamoDB). This examples shows only fraction of the services localstack provides. The full list of services are here. This template provides the basic settings for local test which can be used in different projects with minimal configuration changes. Details for the configuration changes is explained How to use this template section below.
-
Docker - need for the local lambda function testing with sam
git clone https://github.com/BIAD/nci-aws-lambda-localstack-template.git
cd lambda-localstack-template
Set up git hooks (pre-commit, pre-push) - This is one time setting. Doesn't have to be repeated every time.
This will copy the pre-commit, pre-push files under .git/hooks
> git config --local init.templatedir './.git_templates'
> git init
Pre commit: change the format using Black and Pylint checking (Will fail to commit if pylint score is not 10.
Pre push: check the unittest and code coverage (Will fail to push if either unittest fails or code coverage is less than 95%
For special cases, if you still need to commit, push the code use --no-verify option on git commands
> git commit --no-verify
> git push origin [branch_name] --no-verify
export PYTHONPATH=$PWD/src:$PWD/tests:$PYTHONPATH
If virtual environment is used below, you can set the above path in .venv/bin/activate file
Local test set up using aws sam and localstack (with python3 venv) - virtual environment setting is optional.
-
set the virtual environment (optional, you can use other virtual env tools or without it)
cd lambda-localstack-templatepython3 -m venv .venvsource .venv/bin/activate- activate virtual environmentpip install -r requirements.txt- one time setting (If install fails with xcun error, run xcode-select --install) -
Now it is ready to set up the test below.
-
After all the test is done. get out of virtual environment.
deactivate
- goal: With minimal configuation change, testing environment can be set up for different project.
- There are two main configuration - One for localstack and one for sam. If starting and stop scrip need to be changed as well if you want to reuse the script.
- AWS SAM guick start guide
- SAM template
- SQS
- Lambda function best practice
- Take advantage of Execution Context reuse to improve the performance of your function.
- Use AWS Lambda Environment Variables to pass operational parameters to your function.
- Control the dependencies in your function's deployment package.
- Minimize your deployment package size to its runtime necessities.
- Reduce the time it takes Lambda to unpack deployment packages.
- Minimize the complexity of your dependencies.
- Pros and cons of monorepo
- Directory structure for multiple Lambdas
- Documenting REST API
- Mocking AWS stack locally
- LocalStack
- LocalStack with aws sam
- LocalStack with docker
- Lambda debugging
- Scheduling lambda periodically
- Pyinstrument (python profiling)
- S3 event message structure
- aws xray python