# AWS Lambda Layer for Python Not Working

**URL:** <https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713>\
**Category:** AI/ML\
**Tags:** lambda, docker\
**Created:** [December 24, 2023, 6:53pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713 "2023-12-24T18:53:09Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 24, 2023, 6:53pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/1 "2023-12-24T18:53:09Z")

</div>

Hi,

can anyone help me figure this out?

My goal is to create a lambda function that uses Langchain with VertexAI. For this, I’ve created a zip file with the Python packages. The method used was by creating a temp Docker image from amazonlinux in which I’m installing Python3.8, PIP and a virtualenv.

Langchain works, but when I try to use VertexAI I get an error.

Here’s the code:

from langchain.llms import VertexAI  
llm = VertexAI(model\_name=“gemini-pro”,credentials=credentials)

and the error:

“errorMessage”: “Please, install or upgrade the google-cloud-aiplatform library: pip install google-cloud-aiplatform\>=1.38.0”,

```
  "errorType": "ImportError",

  "stackTrace": \[

" File \\"/var/task/lambda\_function.py\\", line 12, in lambda\_handler\\n llm = VertexAI(model\_name=\\"gemini-pro\\",credentials=credentials)\\n",

" File \\"/opt/python/langchain\_core/load/serializable.py\\", line 107, in \_\_init\_\_\\n super().\_\_init\_\_(\*\*kwargs)\\n",

" File \\"/opt/python/pydantic/v1/main.py\\", line 339, in \_\_init\_\_\\n values, fields\_set, validation\_error = validate\_model(\_\_pydantic\_self\_\_.\_\_class\_\_, data)\\n",

" File \\"/opt/python/pydantic/v1/main.py\\", line 1102, in validate\_model\\n values = validator(cls\_, values)\\n",

" File \\"/opt/python/langchain\_community/llms/vertexai.py\\", line 226, in validate\_environment\\n cls.\_try\_init\_vertexai(values)\\n",

" File \\"/opt/python/langchain\_community/llms/vertexai.py\\", line 185, in \_try\_init\_vertexai\\n init\_vertexai(\*\*params)\\n",

" File \\"/opt/python/langchain\_community/utilities/vertexai.py\\", line 75, in init\_vertexai\\n raise\_vertex\_import\_error()\\n",

" File \\"/opt/python/langchain\_community/utilities/vertexai.py\\", line 49, in raise\_vertex\_import\_error\\n raise ImportError(\\n"

  \]

```

I’ve tried to cut dig into this by cutting down the code and it seems only using this:

from google.cloud import aiplatform

generates an error like

{  
“errorMessage”: “Unable to import module ‘lambda\_function’: No module named ‘google.protobuf’”,  
“errorType”: “Runtime.ImportModuleError”,  
“stackTrace”:   
}

Needless to say that the zip file uploaded contains:  
langchain  
google  
protobuf  
google-cloud-aiplatform  
google-cloud-api-keys

What am I missing here?

thanks in advance,  
John

---

<div class="post-metadata">

**Author:** ![wosiu420](https://awshq.com/letter_avatar_proxy/v4/letter/w/f475e1/32.png) [@wosiu420](https://awshq.com/u/wosiu420)\
**Post date:** [December 24, 2023, 7:33pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/2 "2023-12-24T19:33:44Z")

</div>

What’s your directory structure? Python library file needs to be put into “python” directory in the zip file

---

<div class="post-metadata">

**Author:** ![FloRulGames](https://awshq.com/letter_avatar_proxy/v4/letter/f/8e7dd6/32.png) [@FloRulGames](https://awshq.com/u/FloRulGames)\
**Post date:** [December 24, 2023, 7:34pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/3 "2023-12-24T19:34:11Z")

</div>

Sometimes your requirements.txt needs to also include the modules dependencies as well.

---

<div class="post-metadata">

**Author:** ![MrPinga0](https://awshq.com/letter_avatar_proxy/v4/letter/m/4491bb/32.png) [@MrPinga0](https://awshq.com/u/MrPinga0)\
**Post date:** [December 24, 2023, 8:09pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/4 "2023-12-24T20:09:36Z")

</div>

this works for me all the time when creating layers:

[https://stackoverflow.com/questions/63931402/runtime-importmoduleerror-unable-to-import-module-lambda-function-no-module](https://stackoverflow.com/questions/63931402/runtime-importmoduleerror-unable-to-import-module-lambda-function-no-module)

---

<div class="post-metadata">

**Author:** ![blademaster2005](https://awshq.com/letter_avatar_proxy/v4/letter/b/57b2e6/32.png) [@blademaster2005](https://awshq.com/u/blademaster2005)\
**Post date:** [December 24, 2023, 8:58pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/5 "2023-12-24T20:58:41Z")

</div>

Order of layers matters too so if you have multiple layers check that.

Check what your zip contains. Make sure it has what you expect it to and like others said there should be a python dir in there so it gets found correctly.

Set up a simple cdk app and leverage the constructs to help put together your functions and layers.

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 8:17am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/6 "2023-12-25T08:17:28Z")

</div>

> [@wosiu420](#):
>
> What’s your directory structure? Python library file needs to be put into “python” directory in the zip file

the directory structure is correct

[my-zip.zip](https://my-zip.zip)

|-python/google\_cloud\_aiplatform-1.38.1.dist-info/

|-python/langchain/

|-…

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 8:19am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/7 "2023-12-25T08:19:43Z")

</div>

> [@FloRulGames](#):
>
> Sometimes your requirements.txt needs to also include the modules dependencies as well.

this is what I have in my requirements.txt:  
langchain  
google  
protobuf  
google-cloud-aiplatform  
google-cloud-api-keys

in the Docker container, this is enough to run my code, but in the lambda function I get errors

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 8:22am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/8 "2023-12-25T08:22:41Z")

</div>

> [@MrPinga0](#):
>
> this works for me all the time when creating layers: [https://stackoverflow.com/questions/63931402/runtime-importmoduleerror-unable-to-import-module-lambda-function-no-module](https://stackoverflow.com/questions/63931402/runtime-importmoduleerror-unable-to-import-module-lambda-function-no-module)

this is exactly what I did. This works fine for langchain alone, so the method is good, but I run into problems when trying to use the google-cloud-aiplatform module

---

<div class="post-metadata">

**Author:** ![nucc4h](https://awshq.com/letter_avatar_proxy/v4/letter/n/34f0e0/32.png) [@nucc4h](https://awshq.com/u/nucc4h)\
**Post date:** [December 25, 2023, 8:46am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/9 "2023-12-25T08:46:59Z")

</div>

> [@psd-dude](#):
>
> the directory structure is correct [my-zip.zip](https://my-zip.zip) |-python/google\_cloud\_aiplatform-1.38.1.dist-info/ |-python/langchain/ |-…

Not downloading a random zip but that doesn’t look correct. Afaik layers require a much more explicit path. You mentioned having followed the stack overflow post below, I suspect you skipped over the directory structure.

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 9:02am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/10 "2023-12-25T09:02:39Z")

</div>

> [@blademaster2005](#):
>
> Order of layers matters too so if you have multiple layers check that. Check what your zip contains. Make sure it has what you expect it to and like others said there should be a python dir in there so it gets found …

I’m only using one layer in which I tried to use both langchain and the google-cloud-aiplatform

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 9:10am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/11 "2023-12-25T09:10:22Z")

</div>

> [@nucc4h](#):
>
> Not downloading a random zip but that doesn’t look correct. Afaik layers require a much more explicit path. You mentioned having followed the stack overflow post below, I suspect you skipped over the directory structure.

I didn’t follow the stack overflow post, but did something similar

as for the structure, I think it’s the correct structure, otherwise langchain would not work at all. The structure is according to the Python structure documented here: [https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html](https://docs.aws.amazon.com/lambda/latest/dg/packaging-layers.html)

---

<div class="post-metadata">

**Author:** ![blademaster2005](https://awshq.com/letter_avatar_proxy/v4/letter/b/57b2e6/32.png) [@blademaster2005](https://awshq.com/u/blademaster2005)\
**Post date:** [December 25, 2023, 10:52am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/12 "2023-12-25T10:52:01Z")

</div>

> [@psd-dude](#):
>
> I’m only using one layer in which I tried to use both langchain and the google-cloud-aiplatform

You can generate a sample cdk app via

mkdir project-name  
cd project-name  
npx cdk init --language=typescript --type=app  
npm install @aws-cdk/aws-lambda-python-alpha

It will produce a few different files one of them being something like `project-name-stack.ts` in it you should put the following

import \* as cdk from ‘aws-cdk-lib’;  
import { Construct } from ‘constructs’;  
import \* as python from “@aws-cdk/aws-lambda-python-alpha”;  
import path = require(‘path’);  
// import \* as sqs from ‘aws-cdk-lib/aws-sqs’;  
const libDir = path.join(\_\_dirname, ‘..’, ‘lib’);

export class GcaipRedditSampleStack extends cdk.Stack {  
constructor(scope: Construct, id: string, props?: cdk.StackProps) {  
super(scope, id, props);  
new python.PythonFunction(this, “function”, {  
entry: path.join(libDir, ‘functions’, ‘sample’),  
handler: “index.lambda\_handler”,  
runtime: cdk.aws\_lambda.Runtime.PYTHON\_3\_11,  
layers: [  
new python.PythonLayerVersion(this, “shared”, {  
compatibleRuntimes: [cdk.aws\_lambda.Runtime.PYTHON\_3\_11],  
entry: path.join(libDir, ‘layers’, ‘shared’)})  
],  
})  
}  
}

Because I like poetry and it handles the dependency resolution for me including generating a requirements.txt if needed then i can do things like.

mkdir -p lib/layers/shared  
pushd lib/layers/shared  
poetry init -q --name shared --dependency langchain --dependency google-cloud-aiplatform  
poetry lock  
popd

The above will create a poetry project with those dependencies, create the lock and return back to the original dir.

And in a similiar fashion do the same for convenience in your python directory except as dev dependencies. That was your IDE knows about the libraries but it gets packaged separately.

mkdir -p lib/functions/sample  
pushd lib/functions/sample  
poetry init -q --name function --dev-dependency langchain --dev-dependency google-cloud-aiplatform  
poetry lock  
popd

Now before deploying you’ll need to bootstrap cdk.

npx cdk bootstrap

Now you can deploy your cdk app

npx cdk deploy --all

You’ll have to provide region either from profile or `AWS_DEFAULT_REGION`.

Is this overall very over-engineered possibly for what you’re doing? almost certainly, will this also make your life easier to deploy this code iteratively, definitely.

Now this does introduce a few different things like [Poetry](https://python-poetry.org/) and [CDK](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html) but I also think it sets you up well.

If you really want a zip of what i’ve done DM me and i’ll figure out how to get it to you.

---

<div class="post-metadata">

**Author:** ![pint](https://awshq.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pint](https://awshq.com/u/pint)\
**Post date:** [December 25, 2023, 11:02am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/13 "2023-12-25T11:02:51Z")

</div>

> [@nucc4h](#):
>
> Not downloading a random zip but that doesn’t look correct. Afaik layers require a much more explicit path. You mentioned having followed the stack overflow post below, I suspect you skipped over the directory structure.

layers are placed in /opt  
/opt/python is one of the locations configured for modules. the above look fine.

---

<div class="post-metadata">

**Author:** ![pint](https://awshq.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@pint](https://awshq.com/u/pint)\
**Post date:** [December 25, 2023, 11:15am UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/14 "2023-12-25T11:15:11Z")

</div>

i have no idea, but a few things:

1. you use Amazon Linux 2 for installation, right? python3.8 environment uses that.
2. you can use a lambda to install the exact right set of stuff, if you are lucky. for some reason, pip is included in lambda environments, so you can just invoke os.system with the pip command, and specify /tmp as installation dir.
3. you can also look around in the lambda env to see if everything is all right. make a tiny lambda with the same layer and do an e.g. `os.system("find /opt -type d -ls")`
4. make sure that the layered modules are not in the lambda package itself

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 3:24pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/15 "2023-12-25T15:24:55Z")

</div>

> [@pint](#):
>
> layers are placed in /opt /opt/python is one of the locations configured for modules. the above look fine.

thanks for the replies, I fixed the problem!

I did not realize that when you do pip install into a virtualenv you get both /lib and /lib64 folders. When I built the zip, I only copied the content from /lib. Adding the content of /lib64 too fixed the problem

---

<div class="post-metadata">

**Author:** ![psd-dude](https://awshq.com/letter_avatar_proxy/v4/letter/p/ad7895/32.png) [@psd-dude](https://awshq.com/u/psd-dude)\
**Post date:** [December 25, 2023, 7:06pm UTC](https://awshq.com/t/aws-lambda-layer-for-python-not-working/17713/16 "2023-12-25T19:06:37Z")

</div>

> [@blademaster2005](#):
>
> Order of layers matters too so if you have multiple layers check that. Check what your zip contains. Make sure it has what you expect it to and like others said there should be a python dir in there so it gets found …

figured it out. I posted an update in the original post
