dougmoscrop / serverless-http

Use your existing middleware framework (e.g. Express, Koa) in AWS Lambda 🎉
Other
1.72k stars 166 forks source link

Can I invoke Lambda that use serverless-http from another Lambda directly? #152

Closed biigpongsatorn closed 4 years ago

biigpongsatorn commented 4 years ago

I have a Lambda function that works as a REST API server by using serverless-http and I want to invoke this Lambda function from another Lambda function directly via AWS SDK (https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Lambda.html#invoke-property)

How can I do this?

REST API Lambda function:

require('dotenv').config()
const serverless = require('serverless-http')
const Koa = require('koa')
const koaBody = require('koa-body')
const router = require('./controllers')
const env = process.env.NODE_ENV
const app = new Koa()

app.use(koaBody())
app.use(router.routes())

module.exports.handler = serverless(app)

Let's say my API has a route /user/profile and I would like to call the Lambda to route /user/profile via AWS SDK invoke.

 var params = {
  FunctionName: "my-rest-api-function", 
  Payload: '???',  // What's the payload should be?
  Qualifier: "1"
 };
 lambda.invoke(params, function(err, data) {
   if (err) console.log(err, err.stack); // an error occurred
   else     console.log(data);           // successful response
 });

What's the payload should be?

dougmoscrop commented 4 years ago

yes you can do this, you're just bypassing the API Gateway HTTP->JSON formatter, and it's wise to do so since API Gateway is pretty expensive.

Your payload needs to be like { path: '/..', headers: {}, method: 'GET' }

See: https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html

biigpongsatorn commented 4 years ago

@demacdonald Thanks. It's work!