An open API service indexing awesome lists of open source software.

https://github.com/dschep/sls-py-tmpl

A better Python template for serverless
https://github.com/dschep/sls-py-tmpl

aws-lambda python severless template

Last synced: 6 months ago
JSON representation

A better Python template for serverless

Awesome Lists containing this project

README

          

# Serverless Python Template

This repo is intended as a better alternative to the built-in AWS Python
templates in [serverless](https://github.com/serverless/serverless).

## What's included?
This template includes a few of my favorite things for working with Python &
Serverless:

* βš‘οΈπŸπŸ“¦ [serverless-python-requirements](https://github.com/UnitedIncome/serverless-python-requirements)
* 🐍λ✨ [lambda-decorators](http://lambda-decorators.rtfd.io)
* ✨🍰✨ [pipenv](https://docs.pipenv.org)

It also has a tweaked `serverless.yml` file featuring:
* custom variable syntax that allows using the default syntax AND `${AWS::AccountId}` CloudFormation-style variables
* A better IAM example - resources needed for using SSM Parameter store!
* Add custom section with useful workarounds sls var limitations
* A few other minor tweaks

## Getting started
```
$ # if you don't have them installed, ensure you have serverless & pipenv
$ npm i -g serverless ; pip install pipenv
$ # Clone the template using the
$ serverless create -u https://github.com/dschep/sls-py-tmpl -n project-name
Serverless: Downloading and installing "sls-py-tmpl"...
Serverless: Successfully installed "project-name"
$ cd project-name
$ # Create virtualenv and install dependencies
$ pipenv install
Creating a virtualenv for this project…
Using /usr/bin/python3.6m to create virtualenv…
β ‹Running virtualenv with interpreter /usr/bin/python3.6m
Using base prefix '/usr'
New python executable in /home/dschep/.local/share/virtualenvs/sls-py-tmpl-bdJKnR1O/bin/python3.6m
Also creating executable in /home/dschep/.local/share/virtualenvs/sls-py-tmpl-bdJKnR1O/bin/python
Installing setuptools, pip, wheel...done.]

Virtualenv location: /home/dschep/.local/share/virtualenvs/sls-py-tmpl-bdJKnR1O
Installing dependencies from Pipfile.lock (c44045)…
🐍 β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰β–‰ 1/1 β€” 00:00:00
To activate this project's virtualenv, run the following:
$ pipenv shell

$ # Test the sample lambda locally (sls is an included alias for serverless)
$ pipenv run sls invoke local -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
$ # Deploy to AWS!
$ sls deploy
Serverless: Generating requirements.txt from Pipfile...
Serverless: Installing required Python packages with python3.6...
Serverless: Linking required Python packages...
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Unlinking required Python packages...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (19.72 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.........
Serverless: Stack update finished...
Service Information
service: project-name
stage: dev
region: us-east-1
stack: project-name-dev
api keys:
None
endpoints:
None
functions:
hello: project-name-dev-hello
$ # Invoke!
$ sls invoke -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
```