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

How to invoke lambda functions? #9

Open
stephan-nordnes-eriksen opened this issue Nov 30, 2018 · 7 comments
Open

How to invoke lambda functions? #9

stephan-nordnes-eriksen opened this issue Nov 30, 2018 · 7 comments

Comments

@stephan-nordnes-eriksen

I am struggling to actually invoke a function with AWS.lambda.invoke()

Can you perhaps provide a full example?

Using the example code, I have tried all permutations of the following:

Lambda setup

let lambda = new aws.Lambda({region: 'us-east-1', endpoint: 'http://localhost:3000/proxy' })
let lambda = new aws.Lambda({region: 'us-east-1', endpoint: 'http://localhost:3000' })

Invocation type

InvocationType:  undefined
InvocationType:  "Event"
InvocationType:  "RequestResponse"

Payload

Payload: '{}'
Payload: '{ "name" : "Alex" }'

Lambda invoke

lambda.invoke({
				FunctionName: 'hello',
				InvocationType: invocationType,
				Payload: payload
			}, (err, data) => {/**/})

lambda.invoke({
				FunctionName: 'serverless-offline-direct-lambda-example-dev-hello_proxy',
				InvocationType: invocationType,
				Payload: payload
			}, (err, data) => {/**/})
lambda.invoke({
				FunctionName: 'serverless-offline-direct-lambda-example-dev-hello',
				InvocationType: invocationType,
				Payload: payload
			}, (err, data) => {/**/})
lambda.invoke({
				FunctionName: '/proxy/serverless-offline-direct-lambda-example-dev-hello',
				InvocationType: invocationType,
				Payload: payload
			}, (err, data) => {/**/})
lambda.invoke({
				FunctionName: 'proxy/serverless-offline-direct-lambda-example-dev-hello',
				Payload: '{}'
			}, (err, data) => {/**/})

They all say Unsupported Media Type

What am I doing wrong?

@ned-kelly
Copy link

ned-kelly commented Jan 10, 2019

You need to use the fork here that's had it's routes updated to support the lambda SDK...

Then you can invoke against it with the lambda sdk i.e.

let lambda = new AWS.Lambda({region: 'ap-southeast-2', endpoint: 'http://localhost:3000' })

var lambda_args = {
    "data": "Hello World"
}

var params = {
    FunctionName: 'servicename-prd-functionname', // name of the serverless function you are going to execute as if it was deployed with the default serverless stage 
    Payload: JSON.stringify(lambda_args)
};

lambda.invoke(params, function(err, data) {
    if (err) {
        console.error(err);
    } else {
        console.dir(data);
    }
})

@suwilliam
Copy link

suwilliam commented Feb 21, 2019

Hey @ned-kelly, currently using your fork and receiving:

'TypeError: Cannot read property \'split\' of undefined'

in serverless-offline-direct-lambda/proxy.js:6:74.

whenever I set up my serverless offline server and then using aws cli to invoke the function hosted by the server. I'm invoking based on these params:

const lambda = new AWS.Lambda({region: 'us-west-2', endpoint: 'http://localhost:4000' });
const params = {
  FunctionName: 'bosch-lambda-dev-renderPng',
  InvocationType: 'Event',
  Payload: JSON.stringify(data),
};
lambda.invoke(params,...)

where data is a key value json.
How my lambda functions are set up is that they are not using API Gateway and are to be invoked directly through the aws cli.

Can you provide me some assistance?

@suwilliam
Copy link

I also receive this error when I try to do a serverless deploy with serverless-offline-direct-lambda

  ENAMETOOLONG: name too long, open '/Users/william.su/dev/lightning/packages/bosch/node_modules/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.serverless_plugins/serverless-offline-direct-lambda/example/.gitignore'

@ndelvalle
Copy link

@suwilliam @ned-kelly how can I install the fork from npm?

@suwilliam
Copy link

@ndelvalle You can just do npm install <ghusername>/<repoName>

@alexloi
Copy link

alexloi commented Jun 21, 2019

@suwilliam have you found a workaround on this one? I'm currently getting the same, trying to figure out if it's some weird configuration issue / not setting headers correctly perhaps?

Hey @ned-kelly, currently using your fork and receiving:

'TypeError: Cannot read property \'split\' of undefined'

in serverless-offline-direct-lambda/proxy.js:6:74.

whenever I set up my serverless offline server and then using aws cli to invoke the function hosted by the server. I'm invoking based on these params:

const lambda = new AWS.Lambda({region: 'us-west-2', endpoint: 'http://localhost:4000' });
const params = {
  FunctionName: 'bosch-lambda-dev-renderPng',
  InvocationType: 'Event',
  Payload: JSON.stringify(data),
};
lambda.invoke(params,...)

where data is a key value json.
How my lambda functions are set up is that they are not using API Gateway and are to be invoked directly through the aws cli.

Can you provide me some assistance?

@ned-kelly
Copy link

ned-kelly commented Aug 3, 2019

Hi guys, sorry for the delay - I'm not actively getting github notification emails hence the slow delay...

So you should be able to invoke the function without API gateway (I'm using it this way)....

You need to ensure that your lambda.invoke actually includes the callback handler also.. like this:

// Simple script to test we can invoke the lambda function locally - from another lambda function...

var AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-2';

let lambda = new AWS.Lambda({
    region: 'ap-southeast-2',
    endpoint: 'http://localhost:4000'
})

var lambda_args = {
    "some_key": "value",
    "magic_answer": 42
}

var params = {
    FunctionName: 'name-of-function', // the lambda function we are going to invoke
    Payload: JSON.stringify(lambda_args)
};

lambda.invoke(params, function(err, data) {
    if (err) {
        console.error(err);
    } else {
        console.dir(data);
    }
})

And your lambda function would be written like any other normal lambda function...

// Example for API Gateway Response
exports.handler = (event, context, callback) => {
  callback(null, {
    statusCode: 200,
    body: JSON.stringify({
      message: 'it worked',
      input: event,
    }),
  });
};

// Callback without API Gateway headers can be just like any other regular JS callback
exports.handler = (event, context, callback) => {
  callback(null, {
    key: "value",
    other_key: 42,
  });
};

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

No branches or pull requests

5 participants