AWS Lambda cannot find module aws-sdk in Build a Basic Web Application tutorial

0 votes

I'm trying to get a start with AWS by starting at the beginning, running the Build a Basic Web Application tutorial. I figured I'd be able to just follow it step by step and have a simple Hello World web page front end to a little database. But I'm running into a strange error: including the bare-bones, default aws-sdk module is failing!

Short version:

This code:

// Include the AWS SDK module
const AWS = require('aws-sdk');

generates this error: Error: Cannot find module 'aws-sdk'

Long Story:

I'm following the process step by step, creating all the pieces using the AWS online tools. The first hint of trouble happened when I created my Lambda function. I wasn't too worried that the Create Function tool shows a Node.js version of 18.x instead of the tutorial's Node.js 16.x, but I definitely noticed that the Lambda shell it generated was named index.mjs instead of index.js. No problem, I'll just use that file. I replaced the contents with Amazon's HelloWorld code.

// Define handler function, the entry point to our code for the Lambda service
// We receive the object that triggers the function as a parameter
exports.handler = async (event) => {
    // Extract values from event and format as strings
    let name = JSON.stringify(`Hello from Lambda, ${event.firstName} ${event.lastName}`);
    // Create a JSON object with our response and store it in a constant
    const response = {
        statusCode: 200,
        body: name
    };
    // Return the response constant
    return response;
};

I set up the Test Event functionality using their data:

{
  "firstName": "Ada",
  "lastName": "Lovelace"
}

But running the code generated my first error: exports is not defined in ES module scope

Doing some searching on that error sent me down a rabbit hole far outside the scope of a basic tutorial, so I renamed the file from index.mjs to index.js. Rebuilt and poof, it worked just fine:

{
  "statusCode": 200,
  "body": "\"Hello from Lambda, Ada Lovelace\""
}

I won't go through the steps of creating the database, because that's not relevant. Everything was fine until I replaced the code with the code that would access the database - it errored with the message above. To simplify the issue, I deleted the Lambda and rebuilt it with the working code above, and the require statement:

// Include the AWS SDK module
const AWS = require('aws-sdk');
// Define handler function, the entry point to our code for the Lambda service
// We receive the object that triggers the function as a parameter
exports.handler = async (event) => {
    // Extract values from event and format as strings
    let name = JSON.stringify(`Hello from Lambda, ${event.firstName} ${event.lastName}`);
    // Create a JSON object with our response and store it in a constant
    const response = {
        statusCode: 200,
        body: name
    };
    // Return the response constant
    return response;
};

Running that generates this output:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:1000:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1035:21)",
    "    at async start (file:///var/runtime/index.mjs:1200:23)",
    "    at async file:///var/runtime/index.mjs:1206:1"
  ]
}

The strangest thing I see in the dump: it still references index.mjs even though I deleted the file (or renamed it - I tried both ways). In the online IDE, I only have access to the file tree that shows only index.js. I assume that's /var/task so I don't know where /var/runtime is that still contains a reference to index.mjs.

I'm looking forward to the solution to this head-scratcher. It feels like it's simple enough that it should Just Work!
 

Dec 14, 2022 in AWS by Tejashwini
• 3,820 points
5,593 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In AWS

+3 votes
3 answers
+1 vote
2 answers

AWS WAF - Auto Save Web Application Firewall logs in S3.

You can do this through the console ...READ MORE

answered Jul 13, 2018 in AWS by Priyaj
• 58,020 points
1,746 views
0 votes
1 answer

Send a file in post method in aws lambda?

Looks like you're already using request module. in this case ...READ MORE

answered Oct 5, 2018 in AWS by Priyaj
• 58,020 points
4,121 views
0 votes
2 answers

How to kill a lambda function in AWS?

$ aws lambda put-function-concurrency --function-name my-function --reserved-concurrent-executions ...READ MORE

answered Mar 28, 2019 in AWS by Shashank
• 1,370 points
23,455 views
+1 vote
1 answer
0 votes
1 answer

Steps in creating a lambda function in AWS

To create a Lambda function, you first ...READ MORE

answered May 20, 2019 in AWS by Taragan
968 views
0 votes
1 answer

AWS Lamda with Proxy Integration not recieving request parameters

The lambda proxy integration should pass through ...READ MORE

answered Sep 19, 2018 in AWS by Priyaj
• 58,020 points
926 views
0 votes
1 answer
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP