Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

simple node.js example in aws lambda

I am trying to send a simple request with aws lambda.

My module structure is as follows:

mylambda
|-- index.js
|-- node_modules
|   |-- request

I zip the file up and it is uploaded to lambda.

Then I invoke it, and it returns the following error. "errorMessage": "Cannot find module 'index'"

Here is the contents of the index.js file

var request = require('request');

exports.handler = function(event, context) {

    var headers = { 'User-Agent': 'Super Agent/0.0.1', 'Content-Type': 'application/x-www-form-urlencoded' }

    // Configure the request
    var options = {
        url: 'https://myendpoint',
        method: 'POST',
        headers: headers,
        form: {'payload': {"text":""} }
    }

    // Start the request
    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(body)
        }
    })

    console.log('value1 =', event.key1);
    context.succeed(event.key1);  // Echo back the first key value
};

Any help is appreciated, Thanks

like image 271
svnm Avatar asked Jun 04 '15 07:06

svnm


People also ask

Does AWS Lambda support node js?

AWS Lambda now supports Node. js 16 as both a managed runtime and a container base image. Developers creating serverless applications in Lambda with Node. js 16 can take advantage of new features such as support for Apple silicon for local development, the timers promises API, and enhanced performance.

Why node js is preferred for Lambda functions?

js is able to respond to many different requests at the same time. One thing that is interesting about Lambda Functions is that a particular Node. js instance will only ever handle a single request at a time! This means that if we do a lot of work outside of our Node.

What kind of packages can you use with node js for Lambda?

You use a deployment package to deploy your function code to Lambda. Lambda supports two types of deployment packages: container images and . zip file archives. To create the deployment package for a .


1 Answers

All working now, I had to increase the Timeout(s) seconds in advanced settings, as it was taking longer than 3 seconds.

Also I had to ensure my node modules were correctly installed. I had messed up the request module when trying to figure out what was wrong.

To reinstall the module, I deleted then re-installed request.

  • deleted node_modules
  • npm init
  • added the dependancies "request" : "*" in the package.json,
  • npm install. Compressed the zip and uploaded, all working now. :)
like image 156
svnm Avatar answered Nov 08 '22 20:11

svnm