straight-action-80318
09/16/2022, 10:03 PM{
"errorMessage": "the 'package' argument is required to perform a relative import for '.app.lambdex_handler'",
"errorType": "TypeError",
"requestId": "",
"stackTrace": [
" File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 122, in import_module\n raise TypeError(msg.format(name))\n"
]
}
I have re-created the repo based on the docs here: https://github.com/nharada1/lambda-aws-pants-demo
My only changes are I’m using Python 3.9 instead of 3.8enough-analyst-54434
09/16/2022, 10:47 PM$ ./pants package project:lambda
15:42:31.66 [INFO] Wrote dist/project/lambda.zip
Runtime: python3.9
Handler: lambdex_handler.handler
$ zipinfo dist/project/lambda.zip | tail
-rw-r--r-- 2.0 unx 156 b- defN 80-Jan-01 00:00 .bootstrap/pex/version.py
-rw-r--r-- 2.0 unx 533 b- defN 80-Jan-01 00:00 PEX-INFO
-rwxr-xr-x 2.0 unx 3524 b- defN 80-Jan-01 00:00 __main__.py
drwxr-xr-x 2.0 unx 0 b- defN 80-Jan-01 00:00 __pex__/
-rw-r--r-- 2.0 unx 3498 b- defN 80-Jan-01 00:00 __pex__/__init__.py
drwxr-xr-x 2.0 unx 0 b- defN 80-Jan-01 00:00 project/
-rw-r--r-- 2.0 unx 60 b- defN 80-Jan-01 00:00 project/lambda_example.py
?rwxr-xr-x 2.0 unx 57 b- stor 80-Jan-01 00:00 LAMBDEX-INFO
?rwxr-xr-x 2.0 unx 1920 b- stor 80-Jan-01 00:00 lambdex_handler.py
134 files, 1749851 bytes uncompressed, 469448 bytes compressed: 73.2%
$ ./pants roots
.
See that project/lambda_example.py
? Not good.
So teach Pants project/
is a project:
$ git diff
diff --git a/pants.toml b/pants.toml
index 14052ad..eece8d2 100644
--- a/pants.toml
+++ b/pants.toml
@@ -10,4 +10,10 @@ backend_packages.add = [
use_deprecated_directory_cli_args_semantics = false
[anonymous-telemetry]
-enabled = false
\ No newline at end of file
+enabled = false
+
+[source]
+root_patterns = [
+ '/project',
+]
+
$ ./pants roots
15:43:33.92 [INFO] Initializing scheduler...
15:43:34.06 [INFO] Scheduler initialized.
project
And now things look better:
$ ./pants package project:lambda
15:46:40.98 [INFO] Wrote dist/project/lambda.zip
Runtime: python3.9
Handler: lambdex_handler.handler
$ zipinfo dist/project/lambda.zip | tail
-rw-r--r-- 2.0 unx 106996 b- defN 80-Jan-01 00:00 .bootstrap/pex/venv/virtualenv_16.7.12_py
-rw-r--r-- 2.0 unx 156 b- defN 80-Jan-01 00:00 .bootstrap/pex/version.py
-rw-r--r-- 2.0 unx 533 b- defN 80-Jan-01 00:00 PEX-INFO
-rwxr-xr-x 2.0 unx 3524 b- defN 80-Jan-01 00:00 __main__.py
drwxr-xr-x 2.0 unx 0 b- defN 80-Jan-01 00:00 __pex__/
-rw-r--r-- 2.0 unx 3498 b- defN 80-Jan-01 00:00 __pex__/__init__.py
-rw-r--r-- 2.0 unx 60 b- defN 80-Jan-01 00:00 lambda_example.py
?rwxr-xr-x 2.0 unx 49 b- stor 80-Jan-01 00:00 LAMBDEX-INFO
?rwxr-xr-x 2.0 unx 1920 b- stor 80-Jan-01 00:00 lambdex_handler.py
133 files, 1749843 bytes uncompressed, 469435 bytes compressed: 73.2%
See here for more about source roots: https://www.pantsbuild.org/docs/source-roots#what-are-source-rootsstraight-action-80318
09/16/2022, 10:55 PM./pants package project:my_image
straight-action-80318
09/16/2022, 10:55 PMenough-analyst-54434
09/16/2022, 10:56 PMenough-analyst-54434
09/16/2022, 10:56 PMstraight-action-80318
09/16/2022, 10:59 PMenough-analyst-54434
09/16/2022, 11:00 PM.app.lambdex_handler
implies AWS is looking at /
but your image seems to think WORKDIR /app
means something and perhaps it doesn't to lambda. I've not used lambdas with images, just with uploaded zips.straight-action-80318
09/17/2022, 12:02 AMstraight-action-80318
09/17/2022, 12:23 AMstraight-action-80318
09/17/2022, 12:43 AMdocker run -p 9000:8080 myimage:latest
and then making the request with curl -XPOST "<http://localhost:9000/2015-03-31/functions/function/invocations>" -d '{}'
enough-analyst-54434
09/17/2022, 3:38 PMstraight-action-80318
09/17/2022, 4:23 PMenough-analyst-54434
09/17/2022, 5:22 PMjsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$ git diff
diff --git a/project/Dockerfile b/project/Dockerfile
index c658509..85a1639 100644
--- a/project/Dockerfile
+++ b/project/Dockerfile
@@ -3,6 +3,5 @@ FROM public.ecr.aws/lambda/python:3.9
WORKDIR /build
RUN yum install unzip -y
COPY project/lambda.zip /build
-RUN unzip /build/lambda.zip -d /app
-WORKDIR /app
-CMD ["/app/lambdex_handler.handler"]
\ No newline at end of file
+RUN unzip /build/lambda.zip -d "${LAMBDA_TASK_ROOT}"
+CMD ["lambdex_handler.handler"]
Then:
jsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$ ./pants package ::
10:23:49.38 [INFO] Completed: Building docker image myimage:latest
10:23:49.39 [INFO] Wrote dist/project/lambda.zip
Runtime: python3.9
Handler: lambdex_handler.handler
10:23:49.39 [INFO] Built docker image: myimage:latest
Docker image ID: e81026f23b35
$ docker run -p 9000:8080 myimage:latest
17 Sep 2022 17:20:46,509 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/build, handler=)
17 Sep 2022 17:20:54,838 [INFO] (rapid) extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory
17 Sep 2022 17:20:54,838 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
START RequestId: 5e2942b6-f782-487b-9f33-2bb503a1a19e Version: $LATEST
Hello AWS!
END RequestId: 5e2942b6-f782-487b-9f33-2bb503a1a19e
REPORT RequestId: 5e2942b6-f782-487b-9f33-2bb503a1a19e Init Duration: 0.18 ms Duration: 233.78 ms Billed Duration: 234 ms Memory Size: 3008 MB Max Memory Used: 3008 MB
START RequestId: 2875ad8d-ebb6-473e-996d-de12f066f566 Version: $LATEST
Hello AWS!
END RequestId: 2875ad8d-ebb6-473e-996d-de12f066f566
REPORT RequestId: 2875ad8d-ebb6-473e-996d-de12f066f566 Duration: 1.45 ms Billed Duration: 2 ms Memory Size: 3008 MB Max Memory Used: 3008 MB
^C17 Sep 2022 17:21:14,293 [INFO] (rapid) Received signal signal=interrupt
17 Sep 2022 17:21:14,293 [INFO] (rapid) Shutting down...
17 Sep 2022 17:21:14,293 [WARNING] (rapid) Reset initiated: SandboxTerminated
enough-analyst-54434
09/17/2022, 5:24 PMenough-analyst-54434
09/17/2022, 5:26 PMenough-analyst-54434
09/17/2022, 5:34 PM__pex__
package to have a PEX work like any other importable sys.path
entry. So this works too completely bypassing the lambdex handler:
$ git diff
diff --git a/project/Dockerfile b/project/Dockerfile
index c658509..8f756ff 100644
--- a/project/Dockerfile
+++ b/project/Dockerfile
@@ -3,6 +3,5 @@ FROM public.ecr.aws/lambda/python:3.9
WORKDIR /build
RUN yum install unzip -y
COPY project/lambda.zip /build
-RUN unzip /build/lambda.zip -d /app
-WORKDIR /app
-CMD ["/app/lambdex_handler.handler"]
\ No newline at end of file
+RUN unzip /build/lambda.zip -d "${LAMBDA_TASK_ROOT}"
+CMD ["__pex__.lambda_example.example_handler"]
enough-analyst-54434
09/17/2022, 5:38 PM$ cat project/Dockerfile
FROM public.ecr.aws/lambda/python:3.9
COPY project/lambda.zip ${LAMBDA_TASK_ROOT}
ENV PYTHONPATH ${LAMBDA_TASK_ROOT}/lambda.zip
CMD ["__pex__.lambda_example.example_handler"]
straight-action-80318
09/17/2022, 6:38 PMstraight-action-80318
09/17/2022, 6:41 PM__pex__
module? I’m now going to try and add another layer of folders, but simply doing CMD ["__pex__.inner_folder.lambda_example.example_handler"]
doesn’t work (No module named '__pex__.inner_folder'"
) and I’m not sure how to view which methods pex exposesenough-analyst-54434
09/17/2022, 6:47 PM__pex__
psuedo-package exposes everything in the PEX including 3rdparty deps. What is most likely is there is an issue with your folder add. What does zipinfo your/pex
say? Or is this all committed to the example repo where I can look?enough-analyst-54434
09/17/2022, 6:49 PMstraight-action-80318
09/17/2022, 8:11 PMstraight-action-80318
09/17/2022, 8:11 PMstraight-action-80318
09/17/2022, 8:12 PM-rw-r--r-- 2.0 unx 533 b- defN 80-Jan-01 00:00 PEX-INFO
-rwxr-xr-x 2.0 unx 3524 b- defN 80-Jan-01 00:00 __main__.py
drwxr-xr-x 2.0 unx 0 b- defN 80-Jan-01 00:00 __pex__/
-rw-r--r-- 2.0 unx 3498 b- defN 80-Jan-01 00:00 __pex__/__init__.py
drwxr-xr-x 2.0 unx 0 b- defN 80-Jan-01 00:00 inner/
-rw-r--r-- 2.0 unx 60 b- defN 80-Jan-01 00:00 inner/lambda_example.py
?rwxr-xr-x 2.0 unx 55 b- stor 80-Jan-01 00:00 LAMBDEX-INFO
?rwxr-xr-x 2.0 unx 1920 b- stor 80-Jan-01 00:00 lambdex_handler.py
This is what the zip looks like, excluding all the bootstrap stuffenough-analyst-54434
09/17/2022, 8:59 PM__pex__
import magic. You need to add a dunder-init to inner
to get it to work.
This works:
jsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$ git reset --hard origin/HEAD
HEAD is now at b7137c0 Update for folder structure
jsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$ touch project/inner/__init__.py
jsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$ ./pants package ::
13:58:47.32 [INFO] Canceled: Building docker image myimage:latest
13:58:47.56 [INFO] Completed: Building docker image myimage:latest
13:58:47.56 [INFO] Wrote dist/project.inner/lambda.zip
Runtime: python3.9
Handler: lambdex_handler.handler
13:58:47.56 [INFO] Built docker image: myimage:latest
Docker image ID: fd7014075b07
jsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$ docker run -p 9000:8080 myimage:latest
17 Sep 2022 20:58:50,052 [INFO] (rapid) exec '/var/runtime/bootstrap' (cwd=/build, handler=)
17 Sep 2022 20:58:53,476 [INFO] (rapid) extensionsDisabledByLayer(/opt/disable-extensions-jwigqn8j) -> stat /opt/disable-extensions-jwigqn8j: no such file or directory
17 Sep 2022 20:58:53,476 [WARNING] (rapid) Cannot list external agents error=open /opt/extensions: no such file or directory
START RequestId: 9624f52c-9dea-4136-a75b-003f3b5c2288 Version: $LATEST
Hello AWS!
END RequestId: 9624f52c-9dea-4136-a75b-003f3b5c2288
REPORT RequestId: 9624f52c-9dea-4136-a75b-003f3b5c2288 Init Duration: 0.22 ms Duration: 297.77 ms Billed Duration: 298 ms Memory Size: 3008 MB Max Memory Used: 3008 MB
^C17 Sep 2022 20:58:56,304 [INFO] (rapid) Received signal signal=interrupt
17 Sep 2022 20:58:56,304 [INFO] (rapid) Shutting down...
17 Sep 2022 20:58:56,304 [WARNING] (rapid) Reset initiated: SandboxTerminated
jsirois@Gill-Windows:~/dev/nharada1/lambda-aws-pants-demo$
straight-action-80318
09/17/2022, 9:13 PMenough-analyst-54434
09/17/2022, 9:13 PMenough-analyst-54434
09/17/2022, 9:14 PM