AWS Cloud9: Referencing Lambda Layer Locally
Asked Answered
I

4

10

I have set-up a Cloud9 environment to develop and test lambda functions. To make the environment "cleaner" I have opted to use lambda layers to specify the function's dependencies. By doing this, I have removed the dependency folders from the environment, but I am now unable to test locally.

For example, I have a lambda layer for Stripe's python library. I am able to write a lambda function in Cloud9 referencing stripe, deploy the function, and successfully test the function remotely. But i am unable to run that function locally, as I get "unable to import stripe"

Is there a way to test a lambda function, that depends on a lambda layer, locally by specifying the layer ARN in Cloud9?

Izettaizhevsk answered 4/3, 2019 at 3:38 Comment(1)
Did you check in AWS forum or talk to aws support directly?Bullivant
D
6

Unfortunately, AWS Cloud9 does not support Lambda layers at the moment.

Devest answered 4/3, 2019 at 12:35 Comment(1)
Is there an alternative to placing the dependency so that lambda can run locally while avoiding cluttering the environment? For example -- can i use the automatically created venv directory to hold the stripe dependency and allow the lambda to locally reference it there?Izettaizhevsk
B
3

AWS Cloud9 documentation says that is not supported to execute functions with layers from the AWS Resources window. But you can use sam local invoke or aws lambda on the Cloud9 terminal window. For instance

sam local invoke --event input.json --template ../template.yml <function_name>

will create a new docker image with the layer(s) dependencies that will be used to execute the lambda function. The TAG name is explained here

master:~/environment/ahdv (master) $ docker images
REPOSITORY          TAG                                    IMAGE ID            CREATED             SIZE
samcli/lambda       nodejs8.10-03eb754e9966a1a2f789d500d   6b52bcffdc2e        About an hour ago   968MB
lambci/lambda       python3.6                              420212d009b3        3 weeks ago         1.03GB
lambci/lambda       python2.7                              7a436931435e        3 weeks ago         901MB
lambci/lambda       nodejs4.3                              c0914066d9a8        3 weeks ago         931MB
lambci/lambda       nodejs6.10                             74b405a65ed4        3 weeks ago         946MB
lambci/lambda       nodejs8.10                             edf1f613772c        3 weeks ago         960MB
Balbur answered 7/5, 2019 at 18:6 Comment(0)
P
1

There's an easy workaround to the problem of layers working from Lambda, but local testing failing:

Add a folder to your python path before you import the modules contained in your layer, and simply have your Layer packages (unzipped) in that path.

Here's an example where the "nltk" package is coming from a Layer. All layers in this project are stored in a "Layers" folder in the parent directory of the Lambda folders themselves, but you could do this from any level:

import sys
import os
sys.path.append(os.path.abspath("../Layers/custom_NLTK/python"))
import nltk

Hope this helps!

Paleoecology answered 5/4, 2020 at 20:42 Comment(2)
With "local testing" are you referring to "local on Cloud9" or "local on your personal computer". Maybe I don't understand your explanation.Decane
This should work on any "local" terminal, on cloud 9 or your own machine. It is simply a way to reference packages that will come from layers once deployed but wouldn't otherwise be accessible before deployment.Paleoecology
S
1

I've also run into the same problem and ended up going with a workaround. Might be useful for others.

My use case was fairly simple, I needed to share some common code between several functions of an application. Lambda layers is the perfect solution, but I could not get the integration to work in the way I needed in cloud-9.

I ended up not using lambda layers in the end. I created a new folder (not a function) called common-code at the application level.

I then created a hard link (not symbolic) for each of the files I needed in the common area from each of the functions

ln ../common-code/some-helper-functions.js some-helper-functions.js

Then the files get correctly packaged up by cloud-9 when deploying.

Schexnayder answered 17/8, 2020 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.