Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to specify client context for local invoke #1177

Open
nzoschke opened this issue May 14, 2019 · 9 comments
Open

No way to specify client context for local invoke #1177

nzoschke opened this issue May 14, 2019 · 9 comments

Comments

@nzoschke
Copy link

nzoschke commented May 14, 2019

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
@rodmaz
Copy link

rodmaz commented Jun 17, 2019

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

@L-F-Escobar
Copy link

I am dealing with the exact same problem and ive spent hours on it already. Using Python3.7

  ctx = {
"custom": {
	'work': custom.work
      }
  }

stringify_ctx = json.dumps(ctx)

stringify_ctx_base64 = base64.b64encode(stringify_ctx.encode())

response = lambda_client.invoke(FunctionName="LambdaStackFunction",
                              LogType='None',
                              Payload=json.dumps(event),
                              InvocationType='RequestResponse',
                              ClientContext=stringify_ctx_base64.decode('utf-8'))

In my handler function I have

print('\nvars(context):', vars(context))

Which prints 'client_context': None

Ive tried to pass this custom ctx in so many different ways and simplt cant get it to work. I am starting to wonder if its even possible with sam local start-lambda

@cbertozzi
Copy link

I think that the same issue is for the sam local invoke commands. Documentation claims that this SAM command:

...corresponds to the AWS CLI command aws lambda invoke. You can use either version of this command to invoke a Lambda function that you've uploaded to the AWS Cloud.

but there is no way to pass the corresponding --client-context option

@2matzzz
Copy link

2matzzz commented Jun 1, 2021

+1

@michaeltangfong
Copy link

+1

@soundstep
Copy link

soundstep commented May 18, 2022

This is still open and needed, any way to run the lambda with a specific context?

The functionName passed by SAM is not the same kind of value when executed in AWS.

Consider the template:

Resources:
  NotAFunctionNameButALogicalID:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: function-name-visible-in-aws
      Handler: index.handler
      Runtime: nodejs14.x
      Timeout: 10
      Environment:
        Variables:
          DEBUG: cassandra-redirect:*

In a lambda, context.functionName will be:

  • on AWS: function-name-visible-in-aws
  • with SAM local invoke: NotAFunctionNameButALogicalID

Any solution?

@danikenan
Copy link

+1

@jfuss
Copy link
Contributor

jfuss commented Feb 10, 2023

In order for SAM CLI to support this we need RIE to support this first. Right now there isn't a way for SAM CLI to pass the clientContext down into the local Lambda Function.

Linking to the RIE issue: aws/aws-lambda-runtime-interface-emulator#74

I hacked something together locally for python that at least proves we could do this. Mainly we need to:

  1. Add the capability to RIE
  2. Update SAM CLI sam local start-lambda to accept and pass the clientcontext into the running local container
  3. Handle the error cases and have tests.

@mcieno
Copy link

mcieno commented Feb 12, 2024

In order for SAM CLI to support this we need RIE to support this first.

I've opened aws/aws-lambda-runtime-interface-emulator#110

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests