0

Reactjs

const result = await invokeLambda(points);

invokeLambda.js

const AWS = require("aws-sdk");

AWS.config.update({
      accessKeyId: "id",
      secretAccessKey: "AccessKey",
      region: "region"
})

const lambda = new AWS.Lambda();

export const invokeLambda = async (points: any) => {

    console.log("manual lambda call being made");

    return lambda.invoke({
        FunctionName: 'my-lambda-fn',
        InvocationType: 'RequestResponse',
        Payload: JSON.stringify({
          path: '/api/v5.1/points',
          body: JSON.stringify(points)
        }),
      }).promise();
}

Node/express (AWS-lambda)

const express = require("express");
const awsServerlessExpress = require("aws-serverless-express");
const app = express();
const server = awsServerlessExpress.createServer(app)
const cors = require("cors");
...
...

const pointsRoute = require("./src/routes/points.route");

...

app.use(`/api/v5.1/points`, pointsRoute);


module.exports.handler = (event, context) => {
    console.log("Event", event);                              // I can see this log in cloudwatch
    console.log("Context", context);                          // I can see this log in cloudwatch
    return awsServerlessExpress.proxy(server, event, context);
}

points.controller.js

exports.postPoints = (req, res, next) => {                    // not hitting controller
  console.log("hitting postPoints controller method", req);   // not logging this line
  console.log("req object", req);

  ...
  ...
}

Error

    {
    "statusCode": 404,
    "body": "{\"message\":\"Requested Endpoint doesn't not exist\"}",
    "headers": {
        "x-powered-by": "Express",
        "access-control-allow-origin": "*",
        "content-type": "application/json; charset=utf-8",
        "content-length": "50",
        "etag": "W/\"32-/IMs3l+8ZFAcgBIVWVRYcOV6Gbk\"",
        "date": "Tue, 25 Apr 2023 04:07:12 GMT",
        "connection": "close"
    },
    "isBase64Encoded": false
 }

call from broswer

Request URL: https://lambda.whatever-region.amazonaws.com/2015-03-31/functions/my-lambda-fn/invocations
Request Method: POST
Status Code: 200 

is above network call right?

I'm not sure what I'm doing wrong but controller method is not getting hit.

micronyks
  • 54,797
  • 15
  • 112
  • 146

0 Answers0