I am trying to access S3
via aws-sdk
in my lambda function.
import S3 from 'aws-sdk/clients/s3';
const s3 = new S3();
const { Contents: results } = await s3.listObjects({ Bucket: process.env.DOCUMENTS_BUCKET_NAME! }).promise()
I have also deployed it successfully using cdk deploy
command. But when I test, I get the following error
2022-11-23T15:53:40.891Z 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: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"
]
}
As per the documentation, aws-sdk
is always available in the runtime.
Does anyone know what am I doing wrong?
Thank you very much
listObjects
method rather thanlistObjectsV2
and is using the.promise()
feature, you appear to be trying to use JavaScript SDK v2, soconst AWS = require('aws-sdk'); const s3 = new AWS.S3();