Invoking Lambda Functions Locally with Serverless Offline

I recently started building a Slack Application linked to a booking API. The API is deployed in AWS Lambda, and it's developed locally using Serverless Offline.

When handling user interactions, Slack requires an acknowledgment within 3 seconds, but with the booking API, it's not possible to complete the request within 3 seconds, especially when creating a booking.

Therefore, I used one function for the acknowledgment, and another function to handle the request and return data. In AWS docs, this was quite simple. Using aws-sdk, we can trigger another function.

const {Lambda} = require("aws-sdk");

const lambda = new Lambda();

const payload = {...}

const params = {
  FunctionName: `my-api-${process.env.STAGE}-handleProcessA`,
  InvocationType: "Event",
  LogType: "None",
  Payload: JSON.stringify(payload),
};

await lambda.invoke(params).promise();

This code works perfectly fine when deployed to AWS, but not locally with serverless offline. To use this with serverless offline, we need to pass the endpoint variable, which is also mentioned in the docs from Serverless Offline.

const {Lambda} = require("aws-sdk");

const lambda = new Lambda({
  endpoint: process.env.IS_OFFLINE
    ? 'http://localhost:3002'
    : 'https://lambda.us-east-1.amazonaws.com',
});

const payload = {...}

const params = {
  FunctionName: `my-api-${process.env.STAGE}-handleProcessA`,
  InvocationType: "Event",
  LogType: "None",
  Payload: JSON.stringify(payload),
};

await lambda.invoke(params).promise();

For the API's local development, I was using the port 8002, and when using the above code, I changed the port 3002 to be 8002. And running this returned an error: "Unsupported Media Type"

I searched in SO and found these two questions, but still, it didn't solve the issue.

The answers to those two questions didn't work for me, so I posted [a new question in SO.] (https://stackoverflow.com/questions/73718753/how-to-invoke-a-lambda-function-from-another-lambda-in-serverless-offline)

After all that, I got another direction and started looking at the [CLI options](https://www. serverless.com/plugins/serverless-offline#usage-with-invoke) of Serverless Offline again. That's when I realised that there were two ports: --httpPort and --lambdaPort.

When invoking lambda functions, the --lambdaPort should be used. I was using the --httpPort and that's what gave me the error.

They have mentioned to setting serverless-offline endpoint. But I think it would be better understood in this way:

"To use Lambda.invoke you need to set the lambda endpoint to the serverless-offline endpoint with the --lambdaPort."