skip to Main Content

I’m learning AWS CDK. I’m trying to deploy a program that uses aws-sdk but my program cannot find the aws-sdk. It IS installed.

$ ls -ld ./node_modules/*aws*
drwxrwxr-x   5 jimbo jimbo 87 Jul 11 13:04 ./node_modules/@aws-cdk
drwxrwxr-x   5 jimbo jimbo 180 Jul 11 13:04 ./node_modules/aws-cdk
drwxrwxr-x 264 jimbo jimbo 12288 Jul 11 13:04 ./node_modules/aws-cdk-lib
drwxrwxr-x   9 jimbo jimbo 4096 Jul 11 14:23 ./node_modules/aws-sdk

$ npm install aws-sdk
npm WARN deprecated [email protected]: The querystring API is considered Legacy. new code should use the URLSearchParams API instead.

added 28 packages, and audited 372 packages in 3s

46 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

It’s listed in my packages.json

  "name": "cdk-quotes-api",
  "version": "0.1.0",
  "bin": {
    "cdk-quotes-api": "bin/cdk-quotes-api.js"
  },
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "test": "jest",
    "cdk": "cdk"
  },
  "devDependencies": {
    "@types/jest": "^29.5.1",
    "@types/node": "20.1.7",
    "aws-cdk": "2.87.0",
    "jest": "^29.5.0",
    "ts-jest": "^29.1.0",
    "ts-node": "^10.9.1",
    "typescript": "~5.1.3"
  },
  "dependencies": {
    "aws-cdk-lib": "2.87.0",
    "aws-sdk": "^2.1414.0",
    "constructs": "^10.0.0",
    "source-map-support": "^0.5.21"
  }
}

What I get:

Response
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'aws-sdk'nRequire stack:n- /var/task/app.jsn- /var/runtime/index.mjs",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'",
    "Require stack:",
    "- /var/task/app.js",
    "- /var/runtime/index.mjs",
    "    at _loadUserApp (file:///var/runtime/index.mjs:997:17)",
    "    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)",
    "    at async start (file:///var/runtime/index.mjs:1195:23)",
    "    at async file:///var/runtime/index.mjs:1201:1"
  ]
}

Function Logs
2023-07-11T18:17:03.923Z    undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'aws-sdk'nRequire stack:n- /var/task/app.jsn- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'","Require stack:","- /var/task/app.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:997:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)","    at async start (file:///var/runtime/index.mjs:1195:23)","    at async file:///var/runtime/index.mjs:1201:1"]}
2023-07-11T18:17:05.511Z    undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'aws-sdk'nRequire stack:n- /var/task/app.jsn- /var/runtime/index.mjs","stack":["Runtime.ImportModuleError: Error: Cannot find module 'aws-sdk'","Require stack:","- /var/task/app.js","- /var/runtime/index.mjs","    at _loadUserApp (file:///var/runtime/index.mjs:997:17)","    at async UserFunction.js.module.exports.load (file:///var/runtime/index.mjs:1032:21)","    at async start (file:///var/runtime/index.mjs:1195:23)","    at async file:///var/runtime/index.mjs:1201:1"]}
START RequestId:  Version: $LATEST
Unknown application error occurred
Runtime.ImportModuleError
END RequestId: 
REPORT RequestId: 

Duration: 1815.30 ms Billed Duration: 1816 ms Memory Size: 128 MB Max Memory Used: 17 MB

Do you have any ideas?

Please and thank you!

2

Answers


  1. Chosen as BEST ANSWER

    The answer is that I was using node 18 and aws-sdk 2. I had to drop back to node 16 to make my code work. I think going to aws-sdk 3 might have worked as well, but that required change to the code. I've also found that Python 3.6 is problematic. Python 3.8 seems to work better.


  2. From my understanding, you seem to be constructing the most recent Lambda Function, Node.js 18.x runtime. Node.js 18.x runtime includes AWS SDK for JavaScript v3, that imports quite diferent modules like @aws-sdk/client-s3, @aws-sdk/client-sqs, etc.

    ex:

    import { SendMessageCommand, SQSClient } from '@aws-sdk/client-sqs'
    import {
      Context,
      S3Event,
      SQSEvent,
    } from 'aws-lambda'
    
    
    const sqsClient = new SQSClient({})
    
    export const handler = async (event: SQSEvent, context: Context) => {
    
      const sqsMessages = event.Records
      const batchItemFailures: { itemIdentifier: string }[] = []
    
      try {
        for (const message of sqsMessages) {
          const s3Event: S3Event = JSON.parse(message.body)
          const messageId = message.messageId
    
          // logic
        }
        return ({
          statusCode: 200,
          batchItemFailures: batchItemFailures,
        })
      } catch (err) {
        return ({
          body: err,
          statusCode: 500,
          batchItemFailures: event.Records.map(e => ({ itemIdentifier: e.messageId })),
        })
      }
    }
    

    Alternatively, you might consider downgrading to Node.js 16.x runtime, then the aws-sdk module will be present there.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search