Is there an elegant way to reference the AWS SDK i...
# general
p
Is there an elegant way to reference the AWS SDK in a Lambda callback function? Such as
Copy code
const ingestFunction = new aws.lambda.CallbackFunction("processor", {
  role: lambdaRole,
  callback: async event => {
    const s3 = new aws.sdk.S3({ apiVersion: "2006-03-01" });
    const foo = s3.listBuckets((err, data) => {
      return data;
    });
    console.log(foo);
  }
});
That function, when run just dumps a huge AWS response object
t
This approach should work but you need to use promises in an
async
function, smth like:
Copy code
callback: async event => {
        const s3 = new aws.sdk.S3({ apiVersion: "2006-03-01" });     
        const foo = await s3.listBuckets().promise();     
        console.log(foo.Buckets);     
      }
w
+1 to @tall-librarian-49374's point. One additional note - if you do not use an
async
function, then you can use the callback form of the AWS API calls. For example:
Copy code
const ingestFunction = new aws.lambda.CallbackFunction("processor", {
  role: lambdaRole,
  callback: (event, context, callback) => {
    const s3 = new aws.sdk.S3({ apiVersion: "2006-03-01" });
    const foo = s3.listBuckets((err, data) => {
       callback(err, data);
    });
    console.log(foo);
  }
});
But in general, using the promise/async version is likely going to be simpler. AWS Lambda decides whether to wait for the
callback
to be called based on whether the function returns a promise or not - so as soon as you make it
async
(meaning it will return a Promise), it will not wait for the
callback
and will instead return as soon as the promise resolves, which will happen immediately without waiting for your
listBuckets
call to complete.
p
Got it - this is most helpful! Thanks @tall-librarian-49374 and @white-balloon-205