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

Getting Error: Cannot find module 'aws-sdk' in Lambda since switching to node18 #13278

Closed
2 tasks done
PeteDuncanson opened this issue Sep 28, 2023 · 12 comments
Closed
2 tasks done
Labels
pending-response Issue is pending response from the issue author pending-triage Issue is pending triage question General question

Comments

@PeteDuncanson
Copy link

How did you install the Amplify CLI?

yarn

If applicable, what version of Node.js are you using?

node18 in my Lambda

Amplify CLI Version

12.5.1

What operating system are you using?

Windows

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

I've recently updated all my Lambdas to run on node18 instead of node16 following the manual steps in #10894

However to get it running again I've manually switched my Lambda back to node16.

Describe the bug

When running my Lambda in production I get the following error:

Error: Cannot find module 'aws-sdk'

From what I've read (https://aws.amazon.com/blogs/compute/node-js-18-x-runtime-now-available-in-aws-lambda/) this is to be expected when switching to node18 as aws-sdk isn't bundled up for node18 as it was for node16.

Trouble is I'm not importing aws-sdk in my Lambda so I'm not sure where this error is coming from?

For now I've manually reverted back to node16 to get the Lambda running.

Expected behavior

If I'm not importing aws-sdk I'm not sure why its complaining?

Its a Lambda created with Amplify CLI that I'm using in my schema via the @function directive. Its talking to GraphQL via axios but thats about it.

Reproduction steps

Create a lambda.
Edited the cloudformation templates ala #10894 (comment) but for node18
Push up
Run the Lambda

Now this Lambda was created on an older version of the CLI so I'm not sure if this is a fixed thing now or not.

Project Identifier

This never works for me :(

Log output

# Put your logs below this line
2023-09-27T13:33:38.717Z	undefined	ERROR	Uncaught Exception 	
{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'aws-sdk'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/index.mjs",
    "stack": [
        "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:1061:17)",
        "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1093:21)",
        "    at async start (file:///var/runtime/index.mjs:1256:23)",
        "    at async file:///var/runtime/index.mjs:1262:1"
    ]
}



Additional information

No response

Before submitting, please confirm:

  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.
  • I have removed any sensitive information from my code snippets and submission.
@PeteDuncanson PeteDuncanson added the pending-triage Issue is pending triage label Sep 28, 2023
@josefaidt
Copy link
Contributor

Hey @PeteDuncanson 👋 thanks for raising this! With support for Node 18 in Lambda AWS SDK v2 was removed from the base image in favor of AWS SDK v3. As you have found downgrading to Node 16 will resolve this, however you can also install aws-sdk as a Function dependency ahead of migrating to use version 3 of the SDK

@josefaidt josefaidt added the pending-response Issue is pending response from the issue author label Sep 28, 2023
@josefaidt
Copy link
Contributor

I am also curious about the issue you're experiencing with diagnose 👀 do you get an error and/or stack trace if you run amplify diagnose --send-report?

@PeteDuncanson
Copy link
Author

Hey @josefaidt my friend.

I'll try that. So with v2 installed by default does that mean I don't have to import it? Im not importing anything from aws in my functions but maybe there's something global that im using somewhere and that's the issue. That would make sense. I'll check tomorrow.

For your last comment there's an issue for it. Would love to get it sorted to help with stuff like this so feel free to cast an eye over it #10980

@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Sep 28, 2023
@josefaidt
Copy link
Contributor

Hey @PeteDuncanson it will be installed by default but you would need to import/require it. Is this log coming from one of your project's functions or perhaps one of the generated functions in the auth template, etc.?

@josefaidt josefaidt added the pending-response Issue is pending response from the issue author label Oct 2, 2023
@ozbillwang
Copy link

ozbillwang commented Dec 4, 2023

Got the same issue when upgrade to nodejs18.x, since nodejs12.x is deprecated.

it related to the error #3173, but that ticket has been closed.

Not sure nodejs16.x, will take a try. But not sure what can be done if nodejs16.x is deprecated later.

updates

work with nodejs16.x

@krnrnee
Copy link

krnrnee commented Jan 29, 2024

I am having this problem with the AdminQueries created by amplify update auth. Works fine when I downgrade the runtime to "nodejs16.x" but when I upgrade to the recommended "nodejs20.x" or even "nodejs18.x" it doesn't. Not sure how to fix this since I am not hosting my front end (nextjs) on AWS.

@PeteDuncanson
Copy link
Author

PeteDuncanson commented Jan 30, 2024

I'm never ashamed to be that one person that puts their hand up and says they don't know something. If I'm feeling confused others must be to.

Could I ask for a hand holding walk through from someone on the AWS Team of how this issue should be found and fixed as currently I'm still unsure how to make it go away (other than leaving it running on Node 16).

What commands do we need to run?
Where do we run them?
What files need editing?
What steps do we need to take to clear this up?

Give me a walk through like I'm an idiot so I can understand more and others coming behind can follow along without having to be the one to put their hand up :)

@github-actions github-actions bot removed the pending-response Issue is pending response from the issue author label Jan 30, 2024
@josefaidt
Copy link
Contributor

Hey @PeteDuncanson 👋 I went through the motions of upgrading an "older" project and captured the following notes:

  1. install Amplify CLI 9.2.1 to simulate "older" project, run amplify init
  2. add a Function with the base "Hello World" template
  3. observe Function CloudFormation template is set to use the nodejs14.x runtime
  4. push with amplify push -y
  5. upgrade to latest Amplify CLI (12.10.1 at the time of writing)
  6. find and replace nodejs14.x with nodejs18.x
    image
  7. add aws-sdk to your Function's package.json#dependencies block
    {
      "name": "functionawssdkupgradce3aa97f",
      "version": "2.0.0",
      "description": "Lambda function generated by Amplify",
      "main": "index.js",
      "license": "Apache-2.0",
      "dependencies": {
    +   "aws-sdk": "^2.1548.0"
      },
      "devDependencies": {
        "@types/aws-lambda": "^8.10.92"
      }
    }
  8. push with amplify push -y
  9. 🎉

This is assuming the Function source code is not being migrated to use AWS SDK v3, which is now baked into the Node.js 18.x runtime for Lambda.

Let me know if this helps!

@josefaidt josefaidt added question General question pending-response Issue is pending response from the issue author pending-triage Issue is pending triage and removed pending-triage Issue is pending triage labels Feb 1, 2024
@krnrnee
Copy link

krnrnee commented Feb 2, 2024

I had already migrated the rest of my project to the v3 SDK so what I ended up doing was to copy the Admin Queries source code from a new project that was generated with the Node.js 18x runtime to my older project. Since I hadn't customized the actual generated Admin Queries code, this worked perfectly.

This code can be found in the project folders under your-project/amplify/backend/function/your-admin-queries-name/src/

@ykethan
Copy link
Member

ykethan commented Feb 16, 2024

Closing the issue due to inactivity. Do reach out to us if you are still experiencing this issue

@ykethan ykethan closed this as not planned Won't fix, can't repro, duplicate, stale Feb 16, 2024
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@gusmd01
Copy link

gusmd01 commented Feb 19, 2024

Hey @PeteDuncanson 👋 I went through the motions of upgrading an "older" project and captured the following notes:

  1. install Amplify CLI 9.2.1 to simulate "older" project, run amplify init
  2. add a Function with the base "Hello World" template
  3. observe Function CloudFormation template is set to use the nodejs14.x runtime
  4. push with amplify push -y
  5. upgrade to latest Amplify CLI (12.10.1 at the time of writing)
  6. find and replace nodejs14.x with nodejs18.x
    image
  7. add aws-sdk to your Function's package.json#dependencies block
    {
      "name": "functionawssdkupgradce3aa97f",
      "version": "2.0.0",
      "description": "Lambda function generated by Amplify",
      "main": "index.js",
      "license": "Apache-2.0",
      "dependencies": {
    +   "aws-sdk": "^2.1548.0"
      },
      "devDependencies": {
        "@types/aws-lambda": "^8.10.92"
      }
    }
  8. push with amplify push -y
  9. 🎉

This is assuming the Function source code is not being migrated to use AWS SDK v3, which is now baked into the Node.js 18.x runtime for Lambda.

Let me know if this helps!

How do you do this update for UserPoolClientLambda?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-response Issue is pending response from the issue author pending-triage Issue is pending triage question General question
Projects
None yet
Development

No branches or pull requests

6 participants