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.
Windows10 Pro(1909)
WSL(Ubuntu 18.* LTS)
bash
Docker
Python3.6
By the way, the content of the article is written assuming bash. Since my environment is fish, the display may be incorrect. If there is something wrong, Sorry to trouble you, but I would appreciate it if you could let me know in the comments.
Except for "bonus" and "uninstall" by referring to the wonderful article below
How to install / uninstall WSL
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
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.
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 **.
/ var / task
on the container, you can see the directory of the host OS specified on the left.
However, when you are in a local directory on WSL/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.
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.
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
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.