Build a local development environment for Lambda + Python using Serverless Framework

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.

Python installation

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.

Installation of pipenv

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

Install Node.js

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 Serverless Framework

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

Creating a project

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.

Creating a Python virtual environment

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.

Execution of sample function

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.

Recommended Posts

Build a local development environment for Lambda + Python using Serverless Framework
Build a local development environment for Laravel6.X on Mac
Build a Python development environment using pyenv on MacOS
Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
Building a Python development environment for AI development
Try using virtualenv, which can build a virtual environment for Python
[Python] Build a Django development environment with Docker
Build a Python development environment on your Mac
Build a Kubernetes environment for development on Ubuntu
Build a mruby development environment for ESP32 (Linux)
Build a Python development environment on Raspberry Pi
Build a GVim-based Python development environment on Windows 10 (3) GVim8.0 & Python3.6
Build a Django development environment using pyenv-virtualenv on Mac
Build a python environment for each directory with pyenv-virtualenv
Build a GVim-based Python development environment on Windows 10 (1) Installation
Build a Python development environment on Mac OS X
Build a Python virtual environment using venv (Django + MySQL ①)
Build a Python environment on your Mac using pyenv
[ev3dev × Python] Build ev3dev development environment
Build a machine learning application development environment with Python
How to build a development environment for TensorFlow (1.0.0) (Mac)
[For organizing] Python development environment
Build a Python environment offline
Memo for building a machine learning environment using Python
Build a lightweight Fast API development environment using Docker
Build an environment for machine learning using Python on MacOSX
[Memo] Build a development environment for Django + Nuxt.js with Docker
Build a Python development environment in Eclipse (add HTML editor)
Building a development environment for Android apps-creating Android apps in Python
Build a GVim-based Python development environment on Windows 10 (2) Basic settings
How to build a python2.7 series development environment with Vagrant
Build a Flask development environment at low cost using Docker
Until building a Python development environment using pyenv on Ubuntu 20.04
Build a python environment on CentOS 7.7 for your home server
Build a go environment using Docker
Python development environment options for May 2020
Emacs settings for Python development environment
Build a python3 environment on CentOS7
Build and test a CI environment for multiple versions of Python
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
Install pip in Serverless Framework and AWS Lambda with Python environment
Build a Python execution environment using GPU with GCP Compute engine
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build an environment for Blender built-in Python
Easily build a development environment with Laragon
Build a python environment on MacOS (Catallina)
Let's create a virtual environment for Python
[Python] Create a Batch environment using AWS-CDK
I want to build a Python environment
[Mac] Building a virtual environment for Python
Build a python virtual environment with pyenv
Build Python development environment (pythonz, virtualenv, direnv)
Build a Python + OpenCV environment on Cloud9
Build a modern Python environment with Neovim
Creating a development environment for machine learning
Create a Python development environment locally at the fastest speed (for beginners)
Build a version control environment for Python, Ruby, Perl, Node.js on UNIX
[Mac] Build a Python 3.x environment at the fastest speed using Docker
Quickly build a python environment for deep learning and data science (Windows)
I tried to build a Mac Python development environment with pythonz + direnv
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)