Lambda Layer is a true gem in the Lambda function realm. It provides a convenient way to package your libraries and other dependencies that can be used with the Lambda functions. Our open-source project, which has 50k+ downloads and 5.5k stars on GitHub uses Lambda layers. It helps us reduce our Lambda function size by 12 MB and enable us to share the image processing library across all the Webiny deployments. Webiny sharp By the time you finish reading this article, you will know how to create and use Lambda layers. As an example, we will create a sharp library Layer and use it in a Lambda function to create thumbnail images. In this tutorial, we will create a Lambda function that will create a thumbnail image using the library. This library will be packaged into the Lambda function using a . sharp Lambda Layer The workflow will be like this, for each image file uploaded to an S3 bucket (source bucket), the lambda function will be invoked. It will read the image object from the source S3 bucket, create a thumbnail image, and save it to the target S3 bucket. Prerequisites AWS account - To use various AWS services, you will need an AWS account. Please follow to create an AWS account. this link AWS command line - We will use the AWS command line for creating Lambda Function and Lambda Layer. The AWS CLI installation guide is available . here Step 1 - Create two S3 buckets Let’s start by creating two buckets, the source and the target. As mentioned earlier, for each image file uploaded to a source bucket, the Lambda function will create a corresponding thumbnail image and save it in the target S3 bucket. Please follow these steps to create S3 buckets. 1.) Open the . Amazon S3 console 2.) - source and target. Create two S3 buckets The target bucket name should be plus suffixed by . For example, if the source bucket is named then the target bucket should be named as . Please follow this naming convention because we will be using this naming logic in the Lambda function. source-bucket-name -resized mybucket mybucket-resized Step 2 - Create the IAM policy Now let’s create an IAM policy that specifies the permissions for the Lambda function. The Lambda function must have permission for the following operations: Get the object from the source S3 bucket. Put the resized object into the target S3 bucket. Write logs to Amazon CloudWatch Logs. To create an IAM policy 1.) Open the in the IAM console. Policies page 2.) Choose . Create policy 3.) Choose the tab, and then paste the following policy. Be sure to replace with the name of the source bucket that you created previously. JSON mybucket { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "logs:PutLogEvents", "logs:CreateLogGroup", "logs:CreateLogStream" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "s3:GetObject" ], "Resource": "arn:aws:s3:::mybucket/*" }, { "Effect": "Allow", "Action": [ "s3:PutObject" ], "Resource": "arn:aws:s3:::mybucket-resized/*" } ] } 4.) Choose . Next: Tags 5.) Choose . Next: Review 6.) Under , for , enter . Review policy Name AWSLambdaS3Policy 7.) Choose . Create policy Step 3 - Create the execution role Create the that gives your Lambda function permissions to access AWS resources. execution role To create an execution role 1.) Open the in the IAM console. Roles page 2.) Choose . Create role 3.) Create a role with the following properties: – AWS Service (Allow AWS services like EC2, Lambda, or others to perform actions in this account.) Trusted entity type - Lambda (Allows Lambda functions to call AWS services on your behalf.) Use case – Permissions policy AWSLambdaS3Policy – Role name lambda-s3-role Step 4 - Create a sharp Lambda Layer Here comes the special part of this tutorial. We will create the Lambda Layer in this section. sharp You will perform this part of the tutorial on your machine. Please ensure you have installed (we will need the to install the package). npm npm sharp 1.) Create a directory with the name . nodejs 2.) Install the dependency in the directory by running the following commands: sharp nodejs npm install SHARP_IGNORE_GLOBAL_LIBVIPS=1 npm install --arch=x64 --platform=linux sharp After this step, you will get the following directory structure: nodejs |- node_modules |- package-lock.json 3.) Create a file archive of the directory. .zip nodejs zip -r nodejs.zip nodejs 4.) Now let’s publish the Layer. Run the following command to create and publish the Layer: aws lambda publish-layer-version --layer-name sharp --description "Sharp dependency for image transformation" --zip-file fileb://nodejs.zip --compatible-runtimes "nodejs14.x" --region "us-east-1" --output "json" After successful execution of the command above, you will see an output similar to this one. , we will use it while creating the Lambda function. Please note the LayerVersionArn { "Content": { "Location": "https://prod-04-2014-layers.s3.us-east-1.amazonaws.com/snapshots/400803493251/sharp-539dd937-e29c-4418-bb52-2089f3945afc?versionId=4AMhQSZjY3uw3WI85Jcbf7j7Wj1x1X9i&X-Amz-Security-Token=IQoJb3JpZ2luX2VjEJz%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaCXVzLWVhc3QtMSJHMEUCIQDu5lMXWxGc7ydyD6HbmtJk2tnqLLxfRbCYqP7tAwi8wAIgAt3KpyhdECs9I7a2mSGNPjk1n4fq1BKjA7iOmBxOquUq%2BgMIRBAAGgw3N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2Faws4_request&X-Amz-Signature=d84c5f4e44cf6473e0a924f3a5fd57aea32c00b17480af1551a869e41693b030", "CodeSha256": "f9IchJrdt01ftKIlcSZ25z6ZRbf780hO8xVYCd6Uxhs=", "CodeSize": 8748397 }, "LayerArn": "arn:aws:lambda:us-east-1:400803493251:layer:sharp", "LayerVersionArn": "arn:aws:lambda:us-east-1:400803493251:layer:sharp:1", "Description": "Sharp dependency for image transformation", "CreatedDate": "2022-04-12T12:13:27.990+0000", "Version": 4, "CompatibleRuntimes": [ "nodejs14.x" ] } Step 5 - Create Lambda function Now let’s create a Lambda function that will use the Lambda Layer. As discussed earlier, this Lambda function will be invoked when an object is created in the source bucket. Then it will create the respective thumbnail image and store it in the target bucket. The code example below assumes that you are following the S3 bucket name convention that is mentioned in Step-1. sharp Step 5.1 Create the function code Copy the following code example into a file named . index.js // dependencies const AWS = require('aws-sdk'); const util = require('util'); const sharp = require('sharp'); // get reference to S3 client const s3 = new AWS.S3(); exports.handler = async (event, context, callback) => { // Read options from the event parameter. console.log("Reading options from event:\n", util.inspect(event, {depth: 5})); const srcBucket = event.Records[0].s3.bucket.name; // Object key may have spaces or unicode non-ASCII characters. const srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " ")); const dstBucket = srcBucket + "-resized"; const dstKey = "resized-" + srcKey; // Infer the image type from the file suffix. const typeMatch = srcKey.match(/\.([^.]*)$/); if (!typeMatch) { console.log("Could not determine the image type."); return; } // Check that the image type is supported const imageType = typeMatch[1].toLowerCase(); if (imageType != "jpg" && imageType != "png") { console.log(`Unsupported image type: ${imageType}`); return; } // Download the image from the S3 source bucket. try { const params = { Bucket: srcBucket, Key: srcKey }; var origimage = await s3.getObject(params).promise(); } catch (error) { console.log(error); return; } // set thumbnail width. Resize will set the height automatically to maintain aspect ratio. const width = 200; // Use the sharp module to resize the image and save in a buffer. try { var buffer = await sharp(origimage.Body).resize(width).toBuffer(); } catch (error) { console.log(error); return; } // Upload the thumbnail image to the destination bucket try { const destparams = { Bucket: dstBucket, Key: dstKey, Body: buffer, ContentType: "image" }; const putResult = await s3.putObject(destparams).promise(); } catch (error) { console.log(error); return; } console.log('Successfully resized ' + srcBucket + '/' + srcKey + ' and uploaded to ' + dstBucket + '/' + dstKey); }; Step 5.2 Create the deployment package The deployment package is a containing your Lambda function code and its dependencies. .zip file archive 1.) Open a command-line terminal or shell in a Linux environment. 2.) Save the function code as in a directory named . After this step, you have the following directory structure: index.js lambda-s3 lambda-s3 |- index.js 3.) Create a deployment package with the function code and its dependencies. Set the -r (recursive) option for the zip command to compress the subfolders. Run this command from the directory. lambda-s3 zip -r function.zip . Step 5.3 Create a Lambda function We will use the AWS CLI command ( ) to create the Lambda function. aws lambda create-function Please replace the role parameter with your and layers ARN with the one we got in Step 4 ( ). 123456789012 AWS account ID LayerVersionArn aws lambda create-function --function-name CreateThumbnail \ --zip-file fileb://function.zip --handler index.handler --runtime nodejs14.x \ --timeout 30 --memory-size 1024 \ --role arn:aws:iam::123456789012:role/lambda-s3-role \ --region "us-east-1" \ --layers "arn:aws:lambda:us-east-1:400803493251:layer:sharp:1" With the successful execution of the above command, you will see an output similar to this: { "FunctionName": "CreateThumbnail", "FunctionArn": "arn:aws:lambda:us-east-1:400803493251:function:CreateThumbnail", "Runtime": "nodejs14.x", "Role": "arn:aws:iam::400803493251:role/lambda-s3-role", "Handler": "index.handler", "CodeSize": 1061, "Description": "", "Timeout": 30, "MemorySize": 1024, "LastModified": "2022-04-12T12:22:02.432+0000", "CodeSha256": "y0QVpF6+WN3wH1Zp+sbpbYlQEeAdkBuSf7p5aJV52Sc=", "Version": "$LATEST", "TracingConfig": { "Mode": "PassThrough" }, "RevisionId": "64dc7cab-9adf-4472-b20b-e15237209939", "Layers": [ { "Arn": "arn:aws:lambda:us-east-1:400803493251:layer:sharp:4", "CodeSize": 8748397 } ], "State": "Pending", "StateReason": "The function is being created.", "StateReasonCode": "Creating", "PackageType": "Zip", "Architectures": [ "x86_64" ], "EphemeralStorage": { "Size": 512 } } Step 6 - Configure Amazon S3 Trigger So far we have created all the building blocks. Now let's add the final piece where we will configure the Amazon S3 Trigger to invoke the Lambda function. When a file is uploaded to S3, it should trigger the Lambda function that will resize, create a thumbnail, and upload it to the target S3 bucket. To create an Amazon S3 Trigger 1.) Open the in the AWS console. Lambda page 2.) Open the function. CreateThumbnail 3.) Choose under the Function overview section Add trigger 4.) Create a trigger with the following properties: – S3 Select a trigger - mybucket (your source bucket name) Bucket – Event type PUT Step 7 - All set, let’s test it! Nice, we are all set with our Lambda function, which uses the sharp Layer and creates thumbnail images. Please upload an image ( or ) to the source S3 bucket. Once the upload is complete, you should be able to see the thumbnail version of the image in the target S3 bucket. jpg png Lambda Layer is a very efficient and convenient way to package libraries and other dependencies that you can use with your Lambda functions. It offers great benefits in reducing the deployment archives size and faster deployment. layers enable code sharing and separation of responsibilities to create maintainable Lambda functions. In case you want to explore and learn more about best coding practices and code examples related to Lambda, you can check out the . Webiny is an open-source enterprise-grade serverless CMS. Webiny code repository If you have any questions related to this tutorial or have any feedback, please feel free to ping us on the ! Community Slack Credits As this tutorial aims to explain and demonstrate the use of Lambda Layer, we didn't reinvent the wheel and write a new Lambda function to create thumbnail images. The Lambda function code used in this article to create thumbnail images is taken from this . Huge thanks to the authors of this great tutorial on the Lambda function. 🙏🏻 official AWS Lambda tutorial