Best practices in Node.js and AWS

Best practices in Node.js and AWS

2019, Apr 22    

Best practices with Amazon

If using it with AWS, use discrete SDK clients!

The official aws-sdk-js officially supports browserify. That allows us to further reduce the size of our bundles (and Lambda memory usage and speed) by loading only what is strictly needed.

// instead of ...
const AWS = require('aws-sdk')
const s3 = new AWS.S3()
// ... you should use ...
const S3 = require('aws-sdk/clients/s3')
const s3 = new S3()

Ignore AWS SDK!

Although you can use discrete clients (see item above), AWS Lambda service always bundles up the latest SDK version in its Lambda container image. That means that, even if you don’t add AWS SDK to your bundle, it will still be available in runtime.

Therefore, if you don’t need any specific AWS SDK version, you can add the following to your plugin config:

custom:
  browserify:
    exclude:
      - aws-sdk
      - aws-sdk/clients/s3
      - ./**/*/aws-sdk/**
      - ./**/*/.cache/**

References

https://github.com/digitalmaas/serverless-plugin-browserifier