Hi all, if I'm correct using Lambda Layers isn't s...
# sst
j
Hi all, if I'm correct using Lambda Layers isn't support yet by sst?
f
Hi @Jesse Wynants, we are actually adding support for it.
Are you building and packaging a layer? Or are you using an external layer?
j
Just using an external one
f
Got it. And you are seeing that the layer isn’t there when you run
sst start
right?
j
To be honest, I haven't tried it, since I saw this in the ideas on GH
Using serverless-framework, I used this before:
Copy code
layers:
    # Google Chrome for AWS Lambda as a layer
    # Make sure you use the latest version depending on the region
    # <https://github.com/shelfio/chrome-aws-lambda-layer>
    # puppeteer with serverless and layers: <https://codissimo.wordpress.com/2019/12/27/serverless-puppeteer-with-aws-lambda-layers-and-node-js/>
    - arn:aws:lambda:eu-west-1:764866452798:layer:chrome-aws-lambda:20
which worked.
t
I'm using layers with sst
f
By “worked” you mean you could
sls deploy
and the layer worked inside the Lambda rigth?
j
yes.
t
I'm using a normal AWS CDK layer and passing it into the sst.Function's options in the field
layers
?
t
Yes that's right but this is only for production - I don't need layers locally so it's ok that my local instance cannot access stuff in the layer
j
ok, well, I need to use a layer in a sqs consumer function, you have any idea how to load it for the consumer function?
Copy code
// Create Queue
    const createLabelQueue = new sst.Queue(this, "Queue", {
      consumer: "src/create-label.handler",
    });
I probably create a sst.Function load the layer there, and use the function name as consumer?
m
something like this?
Copy code
const brefArn = 'arn:aws:lambda:us-east-1:209497400698:layer:php-80-fpm:7';
        const brefLayer = LayerVersion.fromLayerVersionArn(this, 'adminBrefLayer', brefArn);

        new AwsFunction(this, 'handler', {
            layers: [
                brefLayer,
            ]
        }
(that’s for a normal lambda but it’s the same diff)
j
Ok, I'll give that a try, would be a good use case for the examples 🙂
f
Thank you guys for chiming in! So combining @thdxr and @Matthew Purdon’s answers:
Copy code
// Import a layer
const layer = LayerVersion.fromLayerVersionArn(this, "MyLayer", layerArn);

// Create queue and attach layer to the consumer
const createLabelQueue = new sst.Queue(this, "Queue", {
  consumer: {
    handler: "src/create-label.handler",
    layers: [ layer ]
  },
});
j
that's awesome.
🙂
f
Hey guys @thdxr @Matthew Purdon, layers don’t work inside
sst start
, how are you guys using it?
t
I'm using a layer to bundle up some node_modules stuff I don't want bundled. The new
nodeModules
option will help with that but I have common things across a dozen functions so I figured I'd toss them in a layer
It doesn't bother me locally because it finds it on my local directory
f
Ah i see, you are building the layer and publishing it urself.
j
I can confirm it doesn't work through
sst start
😅
but @Frank if I'm correct this is what you guys are planning to work on?
f
Yeah, we are pretty request driven. Let me take a look at it today and see if we can put something in.
Btw do you know if these Lambda layers can be downloaded and unzipped?
j
Yeah, I suppose so, I use an existing one that is already published, but you can find the zip in their repo: https://github.com/shelfio/chrome-aws-lambda-layer
but unsure if there an automatic way of getting the source based on an arn
f
Yeah that’d make things much easier..
f
OMG!!! Aight lemme give that a try!
m
nice
that would be good to mess with for me because I am doing a PHP lambda and in order to meet the file size requirements I have to clear out deps and install without dev but then I am stuck without unit tests and stuff
j
so this works for me
aws lambda get-layer-version --layer-name arn:aws:lambda:eu-west-1:764866452798:layer:chrome-aws-lambda --version-number 20
f
@Matthew Purdon and with this u can import the unit tests layer, right?
Working on it..
m
correct
f
got it
t
where would you extract it to locally?
f
LOL i’m literally thinking about that right now
t
haha
f
the code is inside
.build
in ur app
t
In AWS it goes to
/opt
and they play with each runtime's env vars for things like
node_modules
to make it search there
f
should I extract it to tmp and symlink it?
Oh i see
t
If it's not for
node_modules
seems it it would be hard to recreate what's happening in aws locally
maybe sst asks you where you want to put it? idk
f
yeah.. lemme see if i can read up on what aws does with the layers
t
Avoided layers until I found this
f
Ah I see.. so for your layer, are you packing them with folder structure
nodejs/node_modules
?
t
yeah exactly
f
So if i understand it correctly, for nodejs ppl access layer data either through: • import/require or; • directly reading off
/opt
Is that fair?
t
That's my understanding
f
the latter might be tricky.. unless we unzip layers to the
/opt
directory as well?
t
Probably not super viable - I'm on linux and that's not usually a writable directory
I haven't yet used layers in this way where I need to hardcode something to look in the
opt
path
f
yeah also if 2 lambdas using 2 diff layers with the same file/path, one would overwrite another
t
Feel like maybe you'd need to provide a specialized function that resolves a path. So locally it can point to some function specific local dir and in production it'll resolve normally to
/opt
SSTLayer.resolve("my-resource.png")
f
U mean this going into user’s Lambda code right?
t
Yeah not ideal because it takes away from the magic of "it's just normal code and sst does its magic"
Unless you want to start running stuff in containers locally 😬
f
… yeah probably have to down the road.. but it’d be good if we can avoid that now
Hey guys, I had a chance to learn how @thdxr and @Jesse Wynants use layers, and I added some guidelines in this discussion https://github.com/serverless-stack/serverless-stack/discussions/179 @thdxr falls under the use case #1 @Jesse Wynants falls under the use case #2 I also created a repo showing how to use the
chrome-aws-lambda
layer (use case #2) - https://github.com/serverless-stack/examples/tree/main/layer-chrome-aws-lambda
It seems to be working for you guys.. so I’m hold back any further work for layers until it gets brought up again.
a
late to the party here but the layers thing in use case #1 has literally made my week. not the easiest thing to find but super grateful you guys had the discussion here. Thanks
f
Yeah, we need to document the layer usage better!