Ill be showing you a method of importing any package you need for your Lambda function. I’ll be using Pandas as an example, but the same method can be used for other packages.
Step 1:
Search for Cloud9 in the AWS Services
Click ‘Create Environment’
Give your environment a name and click next step
Keep the environment default settings and click next step
Click ‘Create Environment’ and you’re ready to go
Step 2:
We'll be setting up the panda lambda layer.
- Type the following code line by line into the terminal at the bottom. The pip install pandas command can be replaced with a package of your choosing. You can also install more than 1 package\.*
mkdir folder
cd folder
virtualenv v-env
source ./v-env/bin/activate
pip install pandas
deactivate
if the virtualenv v-env fails for you, then you need to If you're encountering a "command not found" error when trying to run
virtualenv v-env
, it's likely because thevirtualenv
package is not installed in your system globally or not accessible from the current environment.You can try installing
virtualenv
using pip:pip install virtualenv
After installing
virtualenv
, you can retry the command:virtualenv v-env
Once you have installed panda and ran the deactivate command, Then type the following code line by line to create your layer.
mkdir python
cd python
cp -r /home/ec2-user/environment/folder/v-env/lib/python3.9/site-packages/* .
cd ..
zip -r panda_layer.zip python
aws lambda publish-layer-version --layer-name pandas --zip-file fileb://panda_layer.zip --compatible-runtimes python3.12
Step 3:
Now we add the pandas layer to our desired function
Go to the AWS Lambda service and click ‘Create Function’
Name your function, set the runtime to ‘Python 3.12’, and click ‘Create Function’
Click on ‘Layers’ in the function designer, then click ‘add a layer’
On the name dropdown, you should see your Pandas layer. Click ‘Add’.
There you go!