Serverless Top Tips
- 3 minutes read - 463 wordsTip 1: Packaging of aws-sdk
The aws-sdk
comes bundled with the Lambda execution environment, but you should not rely on this. Instead you should pack the aws-sdk
in your deployment package because:
- the one in the Lambda execution environment is always a few months old, so probably missing patches, security updates, etc.
- it’ll introduce difference to what I had tested
- AWS can update it without notice, so far I haven’t heard of anyone running into issues in Node.js. But a while back a lot of people’s Python functions started failing without any user action because AWS updated the version of boto3 in the environment, and that version had a bug.
- It made no difference to the cold start performance where I load the AWS SDK from, it’s the act of requiring it that incurs latency.
- The AWS SDK is not that big and makes a trivial difference to the time it takes to deploy.
It’s also the recommendation from AWS - to pack your own AWS SDK.
Tip 2: Don’t require the full aws-sdk
if not needed
The AWS developer docs use the following as an example:
// Load the AWS SDK for Node.js
const AWS = require('aws-sdk');
// Create DynamoDB document client
const docClient = new AWS.DynamoDB.DocumentClient();
This has the result of initialising the whole of the SDK as opposed to just the service which can be done as follows:
const DynamoDB = require('aws-sdk/clients/dynamodb')
const documentClient = new DynamoDB.DocumentClient()
The example above saves 141ms in initialisation time, or a whopping 33% reduction.
See Yan Cui - how expensive is the full aws sdk
Tip 3: Use webpack for functions
Webpack
is described as a static module bundler for Javascript. When webpack processes your application, it internally builds a dependency graph which maps every module your project needs and generates one or more bundles. At a high level, it means you can significantly reduce function size, as it only pulls in the relevant code that is needed. I decided to use Serverless Webpack as a plugin to the Serverless Framework.
To start off with, install the following:
$ npm install webpack --save-dev
$ npm install webpack-node-externals --save-dev
$ npm install serverless-webpack --save-dev
The next step is to include the plugin and the configuration of the plugin in the serverless.yml
file:
plugins:
- serverless-webpack
...
custom:
webpack:
webpackConfig: 'webpack.config.js' # Name of webpack configuration file
includeModules: false # Node modules configuration for packaging
packager: 'npm' # Packager that will be used to package your external modules
By default the plugin looks for a webpack.config.js
in the service directory. I used the following basic configuration:
module.exports = {
entry: {
'functions/index': './functions/index.js',
'functions/data/getNextQuestion': './functions/data/get-question.js',
'functions/constants/constants': './functions/constants/constants.js',
},
mode: 'production',
target: 'node'
}
The resulted in the package size reducing from 8MB to less than 1MB.