Search code examples
amazon-web-servicesaws-lambdaaws-sdk-nodejs

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 )
  }
});

Solution

  • 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 ) {  
        ... 
    }