Hey all, I am trying to install dependencies in t...
# help
f
Hey all, I am trying to install dependencies in the
requirements.txt
file for an example lambda function. However, I keep getting a module not found error for the dependencies I am trying to install (
requests
) My
MyStack.js
file is as follows:
Copy code
import * as sst from "@serverless-stack/resources";

export default class MyStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props);

    // Create a HTTP API
    const api = new sst.Api(this, "Api", {
      defaultFunctionProps: {
        srcPath: "src/example",
      },
      routes: {
        "GET /": "lambda.handler",
      }
    });

    // Show the endpoint in the output
    this.addOutputs({
      "ApiEndpoint": api.url,
    });
  }
}
My
lambda.py
file:
Copy code
import requests


def handler(event, context):
    return {
        "statusCode": 200,
        "body": "Hello, World! Your request was received at {}.".format(
            event["requestContext"]["time"]
        ),
    }
My directory structure:
t
Does this help
s
Hey, could you show the contents of index.ts aswell? Wondering if default runtime is set to node in there?
f
Thanks, my
index.ts
file:
Copy code
import MyStack from "./MyStack";
import * as sst from "@serverless-stack/resources";

export default function main(app: <http://sst.App|sst.App>): void {
  // Set default runtime for all functions
  app.setDefaultFunctionProps({
    runtime: "python3.7"
  });

  new MyStack(app, "my-stack");

  // Add more stacks
}
t
Can you run the python file directly without errors?
f
If I remove the
import requests
it all works nicely. The error I get if I try and include the import:
I think for some reason the
requirements.txt
file is not installed
t
If you manually run pip install in the folder does it work?
I'm not sure if the sst.Function is supposed to install deps for you
s
Hmm, that's strange. This is how I set up my projects locally, maybe it is some help. I only needed requirements.txt for deploy. When running locally, the venv needs to be active I think. https://blog.simonireilly.com/posts/fastapi-with-aws-cdk
What I mean is what @thdxr has said, I think you need to install requirements locally
f
Ah i see, I will create a virtualenv and then install the requirements in there. I'll then activate it and see if that works. I assumed that the requirements.txt file would be automatically installed via sst bundling the function up. I'll give it a go
Ok seems to be working if I install requirements into a venv. Thanks @thdxr, @Simon Reilly. For deployment, would I need to install the requirements in the folder where my
lambda.py
file is housed that contains the handler function? I guess I am unsure how SST will ensure that the lambda function has access to all the dependencies
t
SST only bundles when deploying to production. Here's the default behavior
f
Ah got it - thanks I was a bit confused reading the docs but now it makes sense