Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python

Overview

Because I had to build a local development environment for AWS Lambda on Windows I will describe my own construction procedure as a memorandum. ** I would like to proceed with reference to the wonderful article ** that can be easily constructed just by doing so.

Environmental information

Prerequisite knowledge

Building WSL

Except for "bonus" and "uninstall" by referring to the wonderful article below

How to install / uninstall WSL

Install Docker Desktop on Windows

Except for "bonus" and "uninstall" by referring to the wonderful article below ** When the docker desktop for windows docker daemon is made available from WSL ** In ** Shared Drives **, check the drive that has your local working directory.

Docker desktop for windows + docker environment with WSL

Prepare the sauce

I have prepared a template for the source used in this article on GitHub. It is released under the MIT License from the link below. Feel free to use it.

Download source code template

Register an alias

Since the new Lambda function is created based on the template source, It is assumed that the commands when running Docker will always be constant. It is difficult to enter it every time, so register the alias in WSL in advance.

Open .bash_aliases in vim (create new if not)

~$ sudo vim ~/.bash_aliases && source ~/.bash_aliases

Add the following command to .bash_aliases

.bash_aliases


alias docrun='docker run -v $(wslpath -m $PWD):/var/task --env-file .env lambci/lambda:python3.6 lambda_function.lambda_handler $(printf "%s" $(cat event.json))'

It's easy, but I'll explain the contents of docrun.

docker run Create and run a container from a Docker image

-v $(wslpath -m $PWD):/var/task: ** Host OS directory: The volume is allocated in the container directory **.

/mnt/d/Program/Python/AWS/docker-lambda $ echo $PWD
/mnt/d/Program/Python/AWS/docker-lambda

So to convert this to the path of the local environment

/mnt/d/Program/Python/AWS/docker-lambda $ echo $(wslpath -m $PWD)
D:/Program/Python/AWS/docker-lambda

It is said.

--env-file .env Read the environment variable file.

lambci/lambda:python3.6 You are specifying a Docker image.

lambda_function.lambda_handler I'm trying to run lambda_handler in lambda_function.py.

$(printf "%s" $(cat event.json)) I'm passing the contents of event.json to the argument event of lambda_handler.

Try running a Lambda function

Windows

Copy the template source file to your working directory. (Here, it is D: \ Program \ Python \ AWS \ docker-lambda.)

WSL

Move to working directory

~$ cd /mnt/d/Program/Python/AWS/docker-lambda/

Execute Lambda function

/mnt/d/Program/Python/AWS/docker-lambda $ docrun
START RequestId: c02ba91d-7783-1719-3b5e-269dfb11c807 Version: $LATEST
event:{'Hello': 'World'}
ENV:LOCAL
END RequestId: c02ba91d-7783-1719-3b5e-269dfb11c807
REPORT RequestId: c02ba91d-7783-1719-3b5e-269dfb11c807  Init Duration: 439.63 ms        Duration: 4.32 ms       Billed Duration: 100 ms Memory Size: 1536 MB    Max Memory Used: 39 MB

"{\"message\": \"success\"}"

The first time you run it, it starts with Pull of the Docker image. The function is executed after the pull is completed, and if the above display is displayed, the environment construction is complete.

reference

How to install / uninstall WSL Docker desktop for windows + docker environment with WSL Easy to run AWS Lambda environment with docker-lambda I've just started Docker, so I've summarized it in an easy-to-understand manner

Finally

I will explain it so that even beginners can understand it as much as possible. If there is something that is difficult to understand or something is wrong I would appreciate it if you could comment.

Recommended Posts

Build a local development environment with WSL + Docker Desktop for Windows + docker-lambda + Python
[Memo] Build a development environment for Django + Nuxt.js with Docker
Create a simple Python development environment with VSCode & Docker Desktop
Create a Python execution environment for Windows with VScode + Remote WSL
Build a local development environment for Lambda + Python using Serverless Framework
[DynamoDB] [Docker] Build a development environment for DynamoDB and Django with docker-compose
Build a local development environment for Laravel6.X on Mac
Build a python environment for each directory with pyenv-virtualenv
Build a machine learning application development environment with Python
Build a development environment with Poetry Django Docker Pycharm
Build Mysql + Python environment with docker
Build a Django development environment with Docker! (Docker-compose / Django / postgreSQL / nginx)
[Django] Build a Django container (Docker) development environment quickly with PyCharm
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 Python environment with WSL + Pyenv + Jupyter + VS Code
Easily build a development environment with Laragon
Build Jupyter Lab (Python) environment with Docker
Build a 64-bit Python 2.7 environment with TDM-GCC and MinGW-w64 on Windows 7
Building a Python 3.6 environment with Windows + PowerShell
Get a local DynamoDB environment with Docker
[Linux] Build a jenkins environment with Docker
Create a development environment for Go + MySQL + nginx with Docker (docker-compose)
I made a development environment for Django 3.0 with Docker, Docker-compose, Poetry
Build a python virtual environment with pyenv
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
Build a modern Python environment with Neovim
[Linux] Build a Docker environment with Amazon Linux 2
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Easy construction]
Create a Python development environment on Windows (Visual Studio Code remote WSL).
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 LAMP environment on your local Docker
Build Django + NGINX + PostgreSQL development environment with Docker
Simply build a Python 3 execution environment on Windows
Build Python development environment with Visual Studio Code
Go (Echo) Go Modules × Build development environment with Docker
Create a python3 build environment with Sublime Text3
Build a Python development environment on your Mac
Build a Django development environment with Doker Toolbox
Build a Python environment with OSX El capitan
Quickly build a Python Django environment with IntelliJ
Build PyPy and Python execution environment with Docker
Build a mruby development environment for ESP32 (Linux)
Create a Python virtual development environment on Windows
Build a Python machine learning environment with a container
Build a Python development environment on Raspberry Pi
Build a python execution environment with VS Code
Get a quick Python development environment with Poetry
Build a development environment using Jupyter and Flask with Python in Docker (supports both VS Code / code-server)
Build a Python + bottle + MySQL environment with Docker on RaspberryPi3! [Trial and error]
Python local development environment construction template [Flask / Django / Jupyter with Docker + VS Code]
Build a python virtual environment with virtualenv and virtualenvwrapper
Build a Python extension for E-Cell 4 on Windows 7 (64bit)
Create a comfortable Python 3 (Anaconda) development environment on windows
Create a python development environment with vagrant + ansible + fabric
[Linux] WSL2 Build an environment for laravel7 with Ubuntu 20.04
How to build a Django (python) environment on docker
Build a Python development environment on Mac OS X
Procedure for building a CDK environment on Windows (Python)