In the same spirit as cookiecutter-aws-sam-python, but with a lot more opinions and therefore a lot less options. 😊 This is my personal, opinionated way of structuring python-based SAM apps.
- Lambda functions use python 3.8.
- Lambda functions have X-Ray enabled.
- pipenv is used for dependency management.
- VS Code is used for editing.
- flake8 and pydocstyle static analysis checks are run on Lambda function code.
- Lambda function code is unit tested with an enforced code line coverage minimum (85%).
- cfn-lint is run to validate the SAM template.
- Lambda functions support runtime-configurable log level via template parameters and environment variables.
- SAM CLI is used for build/package/local test/deploy/publish.
- App is published to the AWS Serverless Application Repository (SAR).
- Makefile is included with the following targets:
clean
- remove build artifacts.bootstrap
- run once after initializing from cookiecutter to lock and install dependencies.init
- used by CI build to install pre-requisites and locked dependencies.compile
- run linters on python code and SAM template, run sam build.build
- default target. Executescompile
target.package
- packages dependencies and uploads to S3, outputting a packaged template for deployment.publish
- create/update corresponding app in AWS SAR.
NOTE: This template assumes you have SAM CLI installed and the sam
command is on your path. SAM CLI installation instructions can be found here.
Here's my flow for starting a new SAM app. Note, I use a Macbook and have not tested this template on anything else. Theoretically, it should work on any *nix OS, maybe with minor tweaks.
- Create a new repository in GitHub.
- For the sake of this example, let's say you decided to name it my-sam-app.
- Do NOT have GitHub automatically create any files, e.g., README, etc.
- cd to your workspace.
sam init --location gh:jlhood/cookiecutter-python-sam
- When prompted for project name, enter the name of your GitHub repo, e.g., my-sam-app.
- Choose 'y' for push_to_github.
- Answer remaining questions.
- The cookiecutter template will automatically
- initialize the app from the template
- bootstrap dependencies
- initialize git and commit the app template changes
- push the changes to the GitHub remote repo
- Add LICENSE file through GitHub UI
- Click "Create new file"
- Name the new file LICENSE
- Click "Choose a license template"
- Follow the steps to create and commit the LICENSE file from a template
Now you're ready to start making changes and testing using sam local
:
make
sam local invoke --no-event
or deploy it like this:
PACKAGE_BUCKET=my-bucket make package
sam deploy --template-file .aws-sam/packaged-template.yml --stack-name my-stack --capabilities CAPABILITY_IAM
I like to setup CI via AWS CodeBuild for my SAM apps to ensure all PRs can be packaged and built successfully before merging them to master. If you use this cookiecutter template, setting up CI is simple, because I've published an app to the AWS Serverless Application Repository (SAR) that creates a CI CodeBuild project specifically designed to work out of the box with this cookiecutter template. To install, go to the app's SAR page and follow the instructions in the README.
This sample code is made available under the MIT license. See the LICENSE file.