o1lab/xmysql

AWS lambda - sharing knowledge

Bnaya opened this issue · 0 comments

Bnaya commented

I want to share my progress with integrating it with lambda, and also to ask related questions.

My use case is that i have existing serverless project, and i want to add xapi under specific route.

First you need to import colors = require("colors"); (or require if not typescript or whatever)

What works for me:
I'm using https://github.com/awslabs/aws-serverless-express
Which is the official lambda -> express compat layer (and not serverless-http)

  • Im using the serverless framework. this is how my lambda declaration looks like
  ExpressApp:
    handler: dist/expressEntrypoint.start
    role: arn:aws:iam...
    environment:
      SQL_USER: ${self:custom.envParams.....}
      SQL_PASSWORD: ${self:custom.envParams......}
      SQL_URL: ${self:custom.envParams......}
      SQL_RO_USER: ${self:custom.envParams......}
      SQL_RO_PASSWORD: ${self:custom.envParams......}
      SQL_RO_URL: ${self:custom.envParams......}
    events:
      - http:
          path: expressApp/{p1+}
          parameters:
            paths:
              p1: false
          method: any
          private: true

To make xapi work properly under subpath:
apiPrefix: "/expressApp/xapi/",

And im mounting the express app that im passing to xapi under subroute

    const app = express();
    const topLevelApp = express();
    const r = await initializeXapi(app);
    topLevelApp.use("/expressApp", app);

in that way, xapi will have the correct path to itself

What am i missing:
Due to lambda nature, it is expected to close any db connection after any request, or the lambda won't exit until timeout (30 secs for api gateway lambda) and we will get an error in the log.
So we need a way to: or close destroy xpi, and create new one for each request,
Or a way to "suspend"/"resume" it, that will close to mysql connection and restore it on demand.

Both ways have advantages and disadvantages (i can elaborate)

When i will finish to set up all of my stuff i will be happy to create an example project