I am trying to create a Lambda that when triggered...
# aws
n
I am trying to create a Lambda that when triggered by an S3 object being created, will read the contents of the file (which is JSON). When I tried that out with this code:
Copy code
... (other code omitted) ...

bucket.onObjectCreated('on_created', async e => {
  if (e.Records) {
    for (const rec of e.Records) {
      const file = aws.s3.getBucketObject({
        bucket: bucketName.get(),
        key: rec.s3.object.key
      });

      console.log('body', (await file).body);
    }
  }
});
I get this error message in CloudWatch:
Copy code
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module '@pulumi/aws/s3/index.js'\nRequire stack:\n- /var/task/__index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "stack": [
    "Runtime.ImportModuleError: Error: Cannot find module '@pulumi/aws/s3/index.js'",
    "Require stack:",
    "- /var/task/__index.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:955:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)",
    "    at Module.load (internal/modules/cjs/loader.js:811:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:723:14)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)",
    "    at internal/main/run_main_module.js:17:11"
  ]
}
This is the line that is causing the issue:
Copy code
const file = aws.s3.getBucketObject({
I have this import at the top of my TypeScript file:
Copy code
import * as aws from '@pulumi/aws';
So what am I doing wrong?
w
For code running at runtime (not deployment time) you need to use the AWS sdk instead of the Pulumi SDKs. Pulumi makes the AWS sdk absolve for you at
aws.sdk
. See for example: https://github.com/pulumi/examples/blob/11d7bb90bf5b7b2df7fb59c6fe48cca971faefc5/aws-ts-scheduled-function/index.ts#L14
n
thanks @white-balloon-205!