question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No way to specify client context for local invoke

See original GitHub issue

Description

I am running a local service with start-lambda and interacting with it from the Node SDK. It appears that the function ClientContext is undefined despite sending it correctly with the SDK.

There is also no way to specify ClientContext with the local invoke command.

Steps to reproduce

  1. Write a simple lambda function
exports.processEvents = function (event, context, callback) {
    console.log("event", event)
    console.log("context", context)
})
  1. Start local lambda service
$ sam local start-lambda
2019-05-13 17:48:27 Found credentials in shared credentials file: ~/.aws/credentials
2019-05-13 17:48:27 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint.
2019-05-13 17:48:27  * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)
  1. Invoke function with AWS SDK
#!/usr/bin/env node
var fs = require('fs');

var AWS = require('aws-sdk');
var Lambda = new AWS.Lambda({
    endpoint: 'http://127.0.0.1:3001/',
});

Lambda.invoke({
    ClientContext: new Buffer('{"apiKey": "abcd1234"}').toString('base64'),
    FunctionName: "ProcessEventsFunction",
    Payload: fs.readFileSync("event.json"),
}, function(err, data) {
    console.log("err", err)
    console.log("data", data)
})

Observed result

See that context.clientContext is undefined

2019-05-13 17:52:30 Invoking index.processEvents (nodejs10.x)
Fetching lambci/lambda:nodejs10.x Docker container image......
2019-05-13 17:52:31 Mounting /Users/noah/dev/partnerapp/lambda as /var/task:ro,delegated inside runtime container
START RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72 Version: $LATEST
  event: 'Registered',3Z        52fdfc07-2182-154f-163f-5f0f9a621d72 

INFO    event { type: 'track',
  userId: 'test-user-23js8',
  timestamp: '2019-04-08T01:19:38.931Z',
  email: 'test@example.com',
  properties: { plan: 'Pro Annual', accountType: 'Facebook' } }
  succeed: [Function],4Z        52fdfc07-2182-154f-163f-5f0f9a621d72

INFO    context { callbackWaitsForEmptyEventLoop: [Getter/Setter],
  fail: [Function],
  done: [Function],
  functionVersion: '$LATEST',
  functionName: 'test',
  memoryLimitInMB: '128',
  logGroupName: '/aws/lambda/test',
  logStreamName: '2019/05/14/[$LATEST]ea74a58b6601ed93e4664022f15bf42d',
  clientContext: undefined,
  identity: undefined,
  invokedFunctionArn: 'arn:aws:lambda:us-east-1:1178758407:function:test',
  awsRequestId: '52fdfc07-2182-154f-163f-5f0f9a621d72',
  getRemainingTimeInMillis: [Function: getRemainingTimeInMillis] }
END RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72
REPORT RequestId: 52fdfc07-2182-154f-163f-5f0f9a621d72      Duration: 19.25 ms      Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 46 MB  

Expected result

Expect the ClientContext value sent with the Invoke API call would be available in my function.

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: OS X
  2. sam --version: SAM CLI, version 0.16.0

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:6
  • Comments:6

github_iconTop GitHub Comments

2reactions
2matzzzcommented, Jun 1, 2021

+1

2reactions
rodmazcommented, Jun 17, 2019

+1 This is definitely an important missing capability in sam.

Read more comments on GitHub >

github_iconTop Results From Across the Web

No way to specify client context for local invoke #1177 - GitHub
I am running a local service with start-lambda and interacting with it from the Node SDK. It appears that the function ClientContext is ......
Read more >
Invoking Lambda functions locally - AWS Documentation
You can invoke your AWS Lambda function locally by using the sam local invoke AWS SAM CLI command and providing the function's logical...
Read more >
Invoke Local - AWS Lambda - Serverless Framework
The invoke local command sets reasonable environment variables for the invoked function. All AWS specific variables are set to values that are quite...
Read more >
lambda-local - npm
lambdaLocal.execute(options) ; callback, optional, lambda third parameter callback. When left out a Promise is returned ; clientContext, optional, ...
Read more >
class Lambda. Client - Boto3 Docs 1.26.34 documentation
For details about how to configure different event sources, ... base64-encoded data about the invoking client to pass to the function in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found