3
votes

I am using serverless framework alongside lambda layers and few lambdas functions. I had to include aws-sdk in order to support some of the features.

The lambda layer size with aws-sdk is 80MB where aws-sdk takes 40MB alone.

what I know is Lambda function include aws-sdk by default. but does that imply on lambda layer as well?

Notes

  • I tried to remove aws-sdk and deploy and the lambda function shows an error 'can't find module aws-sdk'
  • NODE_PATH: "./:/opt/node_modules" is included in serverless.yml (will this cause aws-sdk to not being included by default)?
  • Runtime is nodejs10.x
1

1 Answers

5
votes

Lambda layers give you the ability to pull additional code and content into the execution environment of your AWS Lambda function.

By default, the Node.js Lambda runtime environment provides you with a version of the AWS JavaScript SDK. But as a best practice and, indeed, because you may wish to use newer features of recent versions of the SDK that are not part of the version that is automatically included in the Lambda execution environment, you can package up your desired AWS JavaScript SDK version into a Lambda later so that it will override the version of the SDK present in the execution environment.

Here's how:

mkdir lambda-layer
cd lambda-layer/
npm install aws-sdk
mkdir nodejs
mv node_modules/ nodejs/
zip -r aws-sdk.zip nodejs/

aws lambda publish-layer-version --layer-name aws-sdk --zip-file fileb://aws-sdk.zip --compatible-runtimes nodejs10.x --description "My AWS SDK"

This Lambda layer's size is 6.1 megabytes as reported by the output from aws lambda publish-layer-version above.