Skip to content

craigtubb57/zappa-template

Repository files navigation

Python AWS Docker Shell Script

Zappa Template

Get going fast with a new Zappa project.


Setup

  1. Fork this repo
  2. Create a new repo using this template
  3. Clone the new repo
  4. Run ./init.sh
  5. Run ./new-function.sh {function_name} to create a skeleton directory for your AWS Lambda function
  6. Repeat 5. for each function you need

How does it work?

When you ./deploy.sh one or more functions, the following steps are performed inside a Python virtual environment:

  1. Top-level requirements.txt is installed
  2. If it doesn't exist yet, a Lambda Layer is built using Docker and uploaded to AWS
  • It's version is exported as an Environment Variable
  1. Environment Variables named {UPPER_REPO_NAME}_{UPPER_FUNCTION_NAME}_{VAR_NAME} are exported as {VAR_NAME} for use in the next step
  2. A Zappa template is generated using zappa_template.json inside the function directory, replacing any configured environment variables
  3. Zappa Deploy or Zappa Update is run, as appropriate

Function Structure

File Use
log.py Can be used to log various levels of output
from log import log
log.info("message") or log.error("message") etc.
main.py The entry point of the Lambda function
requirements.txt Used when executing a function locally or building a Lambda Layer during deploy
utils.py Utility functions that can be used
zappa_template.json Tells Zappa how to deploy a Lambda function
See zappa_template_options.json for available properties
Environment Variables can be used for deploy-time replacement of values

Executables

File Use
./init.sh Setup the main Python virtual environment, plus specific ones for each existing function
./new-function.sh {function_name} Create a skeleton directory for a new Lambda function
./deploy.sh [function_name] Deploy one or all Lambda functions
./status.sh [function_name] Check the deployment status of one or all Lambda functions
./undeploy.sh [function_name] Undeploy one or all Lambda functions
./scripts/exec_function.sh {function_name} Execute a function locally
Run inside the function-specific Python virtual environment
functions/{function}/requirements.txt is installed
Environment Variables named {UPPER_REPO_NAME}_{UPPER_FUNCTION_NAME}_{VAR_NAME} are exported as {VAR_NAME} for use by the function

Environment Variables

Required Optional
AWS_ACCESS_KEY_ID AWS_REGION
AWS_SECRET_ACCESS_KEY {UPPER_REPO_NAME}_{UPPER_FUNCTION_NAME}_{VAR_NAME}

Github Action

A deploy.yml Github Action is provided to enable deployment remotely (CD) after operations such as Push and Merge. Customise as required.

The Environment Variables mentioned above will need to be available in Github Secrets, include GH_TOKEN to enable the repo to be checked out.

Individual commits of Github Action libraries are specified instead of Versions to avoid malicious updates being "injected" into the Action executions.


Stuff to note

An example file zappa_template_options.json is included to demonstrate available properties in a Zappa template.

Some functions are ignored when deploying:

  • example
  • Where requirements.txt is missing from the function directory
  • Where main.py is missing from the function directory

Future Improvements

[ ] allow other AWS credential providers to be used [ ] provide some common function examples for even faster development [ ] provide an example RESTful API with easy Authentication and Authorisation integration

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages