10
votes

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?

4
Did you check in AWS forum or talk to aws support directly?BMW

4 Answers

5
votes

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

3
votes

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
1
votes

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!

1
votes

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.