This is part of a series on moving from desktop Linux back to Windows.
The first post is here. The previous post is here.
AWS SAM Local is a “CLI tool for local development and testing of Serverless applications.” It uses Docker to simulate a Lambda-like experience. The docs explain well how to get started, and the GitHub repo has lots of samples as well. As of this writing, it supports python, java, .net, and nodejs.
This is a quick post to show how to use it in Windows Subsystem for Linux (WSL) and Docker For Windows.
Installing on Windows
The instructions recommend installing with npm. That didn’t work for me, giving me errors about file not found. I’m not sure if this is a problem with npm inside of WSL, if it’s a problem with the current installer, or what.
I did get it installed by using the next option, which is go get github.com/awslabs/aws-sam-local
. I then aliased that to sam
: alias sam='aws-sam-local'
Running on Windows
Out of the box, using Docker for Windows as the Docker daemon, the sam
command itself worked fine, but actually invoking a function did not work for me when using the docker client on Ubuntu within WSL. With a simple python hello-world example, I’d get this:
marc@ulysses:sam-local-play$ sam local invoke "HelloWorldFunction" -e event.json 2018/01/20 08:59:18 Successfully parsed template.yml 2018/01/20 08:59:18 Connected to Docker 1.35 ... Unable to import module 'main': No module named 'main' END RequestId: c9edd13a-000e-49bd-a4a7-a8a23258a03b REPORT RequestId: c9edd13a-000e-49bd-a4a7-a8a23258a03b Duration: 1 ms Billed Duration: 0 ms Memory Size: 0 MB Max Memory Used: 19 MB {"errorMessage": "Unable to import module 'main'"}
In the instructions below, I’ll tie together several GitHub issues and a gist from three separate GH users.
First, Kivol, in this GH issue comment from GH user, shows a 2-fold solution:
1. bind-mount /c
to /mnt/c
and then within Ubuntu make sure you’re on /c
(from this comment on another GH issue from aseering)
2. pass --docker-volume-basedir
in the sam invoke
command
Here’s how to do all that (again, mostly copying from a few GH issues and adding some color commentary):
# bind-mount /c. this mount lasts as long as your current terminal session; instructions below for how to make this persistent if this works for you
$ sudo mkdir /c
$ sudo mount --bind /mnt/c /c
# now you can use /c instead of /mnt/c
$ cd /c/path/to/project
$ sam local invoke --docker-volume-basedir $(pwd -P) --event event.json "HelloWorldFunction"
And voila, it worked. I get this beautiful output:
2018/01/20 12:16:08 Successfully parsed template.yml 2018/01/20 12:16:08 Connected to Docker 1.35 2018/01/20 12:16:08 Fetching lambci/lambda:python3.6 image for python3.6 runtime... python3.6: Pulling from lambci/lambda Digest: sha256:0682e157b34e18cf182b2aaffb501971c7a0c08c785f337629122b7de34e3945 Status: Image is up to date for lambci/lambda:python3.6 2018/01/20 12:16:09 Invoking main.handler (python3.6) 2018/01/20 12:16:09 Mounting /c/dev/projects/lambda-projects/sam-local-play as /var/task:ro inside runtime container START RequestId: d7a9dcb0-751e-445d-9566-a025f4e804b0 Version: $LATEST Loading function value1 = value1 value2 = value2 value3 = value3 END RequestId: d7a9dcb0-751e-445d-9566-a025f4e804b0 REPORT RequestId: d7a9dcb0-751e-445d-9566-a025f4e804b0 Duration: 4 ms Billed Duration: 0 ms Memory Size: 0 MB Max Memory Used: 19 MB
Note: that cd /c/...
is really important! If you do all the above but stay in your shell at /mnt/c
— like I did 🙂 — it’s still not going to work.
Big thanks to GitHub users Kivol and aseering for putting this together!
Persistently mounting /c
If the above worked for you, then you’ll probably also want to persistently mount /c
so that you don’t have to redo it every time you want to use sam
from within Linux / WSL.
If you’ve got a Linux background, you’re thinking: just mount it in /etc/fstab
. It seems that as of now, anyway, WSL isn’t loading /etc/fstab
entries when you open a new shell (i.e. it seems as if you have to run mount -a
every time), at least according to comments in this MSDN post and this WSL issue.
Fortunately, linked in those comments, sgtoj has a gist that sets all this up nicely. I saved this locally, ran it once, and now /c
is mounted for all new Linux sessions.
Really?
Coming from doing all personal development on Linux for the past 3 or so years, these kinds of hacks are disappointing. So far in this experiment with going back to Windows, these hacks have been few, and so far for me have all been related to wanting to use a docker client inside of Linux / WSL. More on that in a future post.
Suffice to say: yeah, it’s hacky, but it’s not that bad. Annoying, sure, but certainly not enough to sully the overall experience so far in moving back to Windows. I can live with this one.
A note on aws-sam-local and Powershell
After initially failing to get aws-sam-local running successfully within WSL, I figured I’d try it out all on the Windows side of the house. I ran into problems there, too. First, using go-get to install it, I got path-too-long errors. WTF. That led me to choco install nodejs
and use the npm install
route. That did work, and sam invoke
worked fine.
So far, this is the first time that working in WSL was kind of a shit-show. Thankfully smart people figured out how to get it working correctly. I am always grateful when I find answers in GitHub comments along the lines of “maybe sharing this will help.”
I really did not want to have to use Powershell for this. Not because I don’t like Powershell, but mostly because it kind of pierces the vale of the single development experience I’m trying to achieve, I guess. It’s a bit of a context switch to be doing most of the work in WSL, and then for this one thing, need to pop over to Powershell, which also means maintaining duplicate installs of software in Windows land and WSL land. A small thing, for sure, but I’d like to avoid it if possible.
Next post: Launchy
Thanks for this article! I was going crazy trying to figure out what was not working why my AWS SAM NodeJS 8.10 sampe app was not working. Reading your article gave me a good insight into the issue. I have Docker for windows setup with ORACLE VirtualBox – so I simply added my “F” drive (where I have all my projects) as “f” on my docker machine and everything worked superbly!
This article was a life saver! Thanks for writing it.
I found another article that has a slightly easier workaround that I thought I’d share. This may be due to updates, I have not investigated the timing. Both seem to work.
https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly
In the section on “Ensure Volume Mounts Work” > “Create and modify new WSL configuration file:”
sudo nano /etc/wsl.conf
# Now make it look like this and save the file when you’re done:
[automount]
root = /
options = “metadata”
We need to set root = / because this will make your drives mounted at /c or /e instead of /mnt/c or /mnt/e.
The options = “metadata” line is not necessary but it will fix folder and file permissions on WSL mounts so everything isn’t 777 all the time within the WSL mounts. I highly recommend you do this!
Once you make those changes, sign out and sign back in to Windows to ensure the changes take effect. Win + L isn’t enough. You’ll need to do a full blown sign out / sign in.
I hope this adds a bit of help for someone.
I’ll have to give this a shot. Thanks!