Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Specifying a temporary directory/environment variable for any unzipping/mounting in SAM local #773

Closed
er1c opened this issue Nov 18, 2018 · 7 comments

Comments

@er1c
Copy link

er1c commented Nov 18, 2018

Description:

I'm developing on Windows, using Windows Subsystem Linux (WSL) for running anything aws-related (as well as Scala commands/etc). I have a windows native-docker server, and run docker inside WSL using export DOCKER_HOST=tcp://localhost:2375 (similar to: https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly)

I'm developing a java application, the SAM client extracts the jar to the WSL /tmp directory, which isn't mountable by the Windows Docker process.

Example SAM CLI

eric@EricPeters-PC:/c/Users/Eric Peters/IdeaProjects/ta-html-to-pdf-lambda$ echo '<!DOCTYPE html><html><head><title>HTML doc</title></head><body>Content<body></html>' | sam local invoke --docker-volume-basedir /c/Users/Eric\ Peters/IdeaProjects/ta-html-to-pdf-lambda "HtmlToPdfFunction"
2018-11-18 11:43:27 Reading invoke payload from stdin (you can also pass it from file with --event)
2018-11-18 11:43:27 Invoking ta.aws.lambda.htmltopdf.HtmlToPdfLambda::handleRequest (java8)
2018-11-18 11:43:27 Found credentials in shared credentials file: ~/.aws/credentials
2018-11-18 11:43:27 Decompressing /c/Users/Eric Peters/IdeaProjects/ta-html-to-pdf-lambda/target/scala-2.12/html-to-pdf.jar

Fetching lambci/lambda:java8 Docker container image......
2018-11-18 11:43:43 Mounting /tmp/tmpJw834g as /var/task:ro inside runtime container
START RequestId: 8a75ec13-5a1a-45d7-89b4-15923c11986d Version: $LATEST
java.lang.ClassNotFoundException: ta.aws.lambda.htmltopdf.HtmlToPdfLambda
        at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:348)

END RequestId: 8a75ec13-5a1a-45d7-89b4-15923c11986d
REPORT RequestId: 8a75ec13-5a1a-45d7-89b4-15923c11986d  Duration: 2.73 ms       B
                        Memory Size: 128 MB     Max Memory Used: 3 MB

Proving to myself the mounting is the issue

eric@EricPeters-PC:/c/Users/Eric Peters/IdeaProjects/ta-html-to-pdf-lambda$ mkdir /tmp/tmpKBm6IQ
eric@EricPeters-PC:/c/Users/Eric Peters/IdeaProjects/ta-html-to-pdf-lambda$ touch /tmp/tmpKBm6IQ/test.txt
ls eric@EricPeters-PC:/c/Users/Eric Peters/IdeaProjects/ta-html-to-pdf-lambda$ docker run -v /tmp/tmpKBm6IQ:/var/task -t -it --entrypoint  /bin/sh lambci/lambda:java8
sh-4.2$ ls -al /var/task
total 4
drwxr-xr-x 2 root root   40 Nov 18 19:25 .
drwxr-xr-x 1 root root 4096 Nov  1 21:03 ..
sh-4.2$

Proving to myself that I can mount any directory in /c/, from WSL

eric@EricPeters-PC:/c/Users/Eric Peters/IdeaProjects/ta-html-to-pdf-lambda$ docker run -v /c/tmp:/var/task -t -it  --entrypoint /bin/sh lambci/lambda:java8          
sh-4.2$ ls -al /var/task                                                                                                                                             
total 4                                                                                                                                                              /var/task:ro
drwxrwxrwx 2 root root    0 Nov 18 19:35 .                                                                                                                           
drwxr-xr-x 1 root root 4096 Nov  1 21:03 ..                                                                                                                          
-rwxr-xr-x 1 root root    0 Nov 18 19:35 foo.txt                                                                                                                     

So I need something like --docker-volume-basedir /c/Users/Eric\ Peters/IdeaProjects/ta-html-to-pdf-lambda but that will help specify a working directory for the unzip to occur.

Looks like this is happening somewhere around: https://github.com/awslabs/aws-sam-cli/blob/0e0f8159fab66cc4d4b66a0f01aa39579bc02fa2/samcli/local/lambdafn/runtime.py#L177

@er1c
Copy link
Author

er1c commented Nov 18, 2018

I figured out a work around already, adding for any other google-searchers. I simply create a /c/tmp and set TMPDIR=/c/tmp environment variable.

echo '<!DOCTYPE html><html><head><title>HTML doc</title></head><body>Content<body></html>' | TMPDIR=/c/tmp  sam local invoke "HtmlToPdfFunction"

@er1c er1c closed this as completed Nov 18, 2018
@caylorme
Copy link

Hey, I am attempting to use WSL and aws-sam-cli together. I've also got a working docker setup over WSL, but have not yet gotten my sam local start-api to properly functioning (also assuming its because of the filesystem mounting). Does your setup allow you to run the sam local start-api if you use the output of sam init --runtime python3.7 ?

Steps to try:

Initialize the sam-app
sam init --runtime python3.7
Run the local instance
cd sam-app && sam local start-api
curl the endpoint
curl http://localhost:3000/hello

@amlcodes
Copy link

amlcodes commented Jan 9, 2021

this isnt still a thing is it? TMPDIR=/tmp is not working

@er1c
Copy link
Author

er1c commented Jan 10, 2021

@amlcodes the "/c/tmp" is the fun thing so its on the windows host

@amlcodes
Copy link

amlcodes commented Jan 10, 2021

Ahh I understand. Since I'm on mac, I would use /tmp correct?

@er1c
Copy link
Author

er1c commented Jan 11, 2021

@amlcodes Yeah I think so.

@jigneshkhatri
Copy link

jigneshkhatri commented Oct 7, 2022

@er1c TMPDIR is not working for me on Windows. I have mentioned it in template.yaml to invoke lambda locally using SAM CLI.

Resources:
    MyFunction:
        Properties:
              Environment:
                  Variables:
                      MICRONAUT_ENVIRONMENTS: dev
                      TMPDIR: /d/my-dir

MICRONAUT_ENVIRONMENTS variable just works fine, but TMPDIR is not working. Any suggestion?

Basically I want to access the file on the host machine inside the lambda function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants