Hey everyone, I want to create a route on my Pytho...
# help
m
Hey everyone, I want to create a route on my Python API that uses a large package (Pandas) that none of my other routes need. Is there a standard way to do this in SST so that my other packages don't all end up with Pandas as a dependency. If I add it to the requirements.txt for my api that gets loaded it will end up everywhere which would be a cosmic waste of resources. Any suggestions would be greatly apprciated
a
create a lambda layer and import it in the lambda for that specific route. These instructions should work - https://docs.aws.amazon.com/lambda/latest/dg/python-package.html
m
Yeah, I thought a layer might be the way to go, the tooling for layers just isn't very good though. 😞 In the past I've used LambdaZipper to make layers, if anyone knows a better way, I'd love to hear it
t
don’t need to make your own layers, I normally just look around for premade ones https://github.com/keithrozario/Klayers#list-of-arns
this link looks good for python 🙂 It has pandas
a
@Thomas Ankcorn good find! 😎👏
f
@Michael Robellard you can override the
bundle.installCommands
of a specific route to run a different pip command
For example:
Copy code
new Api(this, "Api", {
  defaultFunctionProps: {
    srcPath: "...",
  },
  route: {
    "GET /route1": "...",
    "GET /route2": "...",
    "GET /route3": {
      handler: "...",
      bundle: {
        installCommands: [
          'pip install ...',
        ],
      },
    },
});
m
Thanks Frank, I will give that a try and I always have the Layer to fall back on. BTW would be great if SST had a layer thing that built those bundles into layers that you could reference. Just an idea. If you could build your dependencies (requirements.txt in Python) into a layer separate from the application code that may speed up deployment time since the dependencies don't change as often as app code. Chalice has something like that, and it helped there quite a bit.
f
Would that work somewhat similar to CDK’s LayerVersion? For example, if you created a separate folder in your app, call ie.
myLayer
, put a requirements.txt in it and does a pip install. Then in ur SST code:
Copy code
const layer = new lambda.LayerVersion(this, 'MyLayer', {
  code: lambda.Code.fromAsset(path.join(__dirname, 'myLayer')),
});

new Api(this, "Api", {
  defaultFunctionProps: {
    layers: [layer],
  },
  routes: ...
});