A memo when building a local development environment of Lambda + Python using Serverless Framework on Windows. Build an environment where you can even unit test locally without actually deploying to AWS.
The latest version from Original Windows download site (at the time of writing 3.8.1 Download /3.8.1/python-3.8.1-amd64.exe)) and execute it.
Check "Add Python 3.8 to PATH" on the first screen and install as standard with "Install Now". Click "Disable path length limit" on the last screen to remove the path string length limit.
>python --version
Python 3.8.1
OK if the version is displayed correctly.
Install using Python's package management system pip.
>pip install pipenv
(Abbreviation)
Successfully installed appdirs-1.4.3 certifi-2019.11.28 distlib-0.3.0 filelock-3.0.12 pipenv-2018.11.26 six-1.14.0 virtualenv-20.0.1 virtualenv-clone-0.5.3
>pipenv --version
pipenv, version 2018.11.26
Since Serverless Framework is made with Node.js, install Node.js.
The latest version of the Windows installer from Home Download Site (at the time of writing 12.15.0 Download /node-v12.15.0-x64.msi)) and run it. Install with the default settings without changing any options.
>node --version
v12.15.0
Install using Node.js's package management system npm.
>npm install -g serverless
(Abbreviation)
+ [email protected]
added 527 packages from 335 contributors in 21.542s
>sls --version
Framework Core: 1.63.0
Plugin: 3.3.0
SDK: 2.3.0
Components Core: 1.1.2
Components CLI: 1.4.0
Create a Serverless Framework project. The template is for AWS Python3.
>sls create -t aws-python3 -p helloworld
(Abbreviation)
Serverless: Successfully generated boilerplate for template: "aws-python3"
-t
specifies the template to use. Since it is developed using Python3 with Lambda on AWS, specify ʻaws-python3.
-p` is the path of the project directory (if you don't specify -n, it will be the project name as it is)
A project directory called helloworld is created under the current path, and the Serverless Framework configuration file serverless.yml and handler.py and .gitignore containing the implementation of the Lambda function are created.
To manage the Python library for each project, go inside the project directory and create a virtual environment for this project.
>cd helloworld
>pipenv install
Creating a virtualenv for this project…
(Abbreviation)
Successfully created virtual environment!
(Abbreviation)
To activate this project's virtualenv, run pipenv shell.
Alternatively, run a command inside the virtualenv with pipenv run.
Try running the Lambda function named hello that is defined in the template from the beginning locally.
>sls invoke local -f hello
{
"statusCode": 200,
"body": "{\"message\": \"Go Serverless v1.0! Your function executed successfully!\", \"input\": {}}"
}
The hello function described in handler.py was executed and a response was returned. Since it is executed with ʻinvoke local`, it will be executed locally.
This completes the environment for local development with Lambda + Python.