mhart/aws4fetch

How to use this in a lambda?

mangs opened this issue · 4 comments

AWS's SDKs keep getting bigger and bigger. I love the simplicity and lean design of this library. You did such a great job here!

I'm curious how I can use this on lambda directly. Seems like I need the accessKeyId and secretAccessKey values, but I'm not sure where to get those inside a running lambda. Any ideas?

FYI I did a bundle size comparison using bun build between using @aws-sdk/client-cloudfront and this library, and the difference is pretty big for my app:

  • @aws-sdk/client-cloudfront: ~574 KiB
  • This library: ~135 KiB

Difference: 439 KiB 🙂 (with sourcemaps the difference is over 1 MiB)

I may have found the answer to my own question: https://www.keithrozario.com/2020/06/access-keys-in-aws-lambda.html

I'm going to test this out and will report back if I have success.

Were you successful?

Yes, it works great! We got rid of 2 AWS SDK NPM packages and reduced our minified (before gzip) bundle size from ~600 KiB to ~140 KiB! This package rocks. 👌

Made using Bun on AWS lambda a breeze.

After all the learning and refactoring I did with this package, this is all you need:

const { AwsClient } = await import('aws4fetch');
const awsClient = new AwsClient({
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
  sessionToken: process.env.AWS_SESSION_TOKEN,
});

Every lambda has those 3 environment variables by default. Then call awsClient.fetch(url) and you're off to the races.

For each AWS service: