Lypheus
Lypheus

Reputation: 474

Call AWS Lambda from a Lambda using NodeJS with Headers and Path

I'd like a lambda called GoalsFeed to invoke another lambda called Goals using AWS.Lambda. This seems to work, except for two things:

  1. I'm not sure how to pass a header through to the target service.

  2. The FunctionName is "myapp-goals-get" (aka Goals) but I want to go to a specific path on that service - the three paths shown below are all valid, but i'm wanting to specify the "../owner/123" path.

  1. http://aws.com/myapp/goals
  2. http://aws.com/myapp/goals/1
  3. http://aws.com/myapp/goals/owner/123

Below is my first crack at this, can someone help me modify this to pass "tenantid" as a header and to ensure that when "myapp-goals-get" is invoked, it sees itself as being invoked from the path with "../owner/123" ?

// fetch back all goals by userid
var lambda = new AWS.Lambda({
    region: 'us-east-1' 
});

var payload = {};
payload[ "userId" ] = "123";
payload[ "tenantid" ] = "1";

const params = {
  FunctionName: 'myapp-goals-get',
  InvocationType: "RequestResponse",
  Payload: JSON.stringify(payload)
};

lambda.invoke( params, function(error, data) {
  console.log( "data: %s", JSON.stringify( data ) );

  if( error ) {
    context.done( 'error', error );
  } 
  else if( data.Payload )
  {
    context.succeed( data.Payload )
  }
});

Upvotes: 2

Views: 2759

Answers (2)

Lypheus
Lypheus

Reputation: 474

My solution was to simply reset the pathParameters and pass the Event through to the child lambda.

event.pathParameters = { };
event.pathParameters.id = 123;
const tdParms = { 
    FunctionName : 'mylambda', 
    Payload: JSON.stringify( event ) 
};

lambda.invoke( tdParms, function( error, data ) {  
    ... 
}

Upvotes: 1

John
John

Reputation: 975

The proper way to handle this use-case is to setup a REST API with API-Gateway + Lambda using proxy integration ( to forward all HTTP Headers/body/etc to your lambda function )

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-as-simple-proxy-for-lambda.html#api-gateway-proxy-integration-lambda-function-nodejs

You can then chain inside the same request or make another call to your API.

Upvotes: 1

Related Questions