You are currently viewing Lambda Function Layers

Lambda Function Layers

Lambda Function Layers

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed and configured environment variables for our lambda function.

https://cloudaffaire.com/lambda-function-environment-variables/

In this blog post, we are going to discuss layers in a lambda function. We will also create a layer for python package pymysql. Our function will have a trigger on S3 create-object and will write the bucket name and object name in AWS MySQL RDS.

Lambda Function Layers:

You can configure your Lambda function to pull in additional code and content in the form of layers. A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package.

Layers let you keep your deployment package small, which makes development easier. You can avoid errors that can occur when you install and package dependencies with your function code. For Node.js, Python, and Ruby functions, you can develop your function code in the Lambda console as long as you keep your deployment package under 3 MB.

Note: A function can use up to 5 layers at a time. The total unzipped size of the function and all layers can’t exceed the unzipped deployment package size limit of 250 MB.

Prerequisite for this demo:

S3 bucket, AWS MySQL RDS with table s3_upload.

We have already created an S3 bucket named cloudaffaire.

Lambda Function Layers

We have also created MySQL RDS instance with a database mysqldb and table s3_upload.

Lambda Function Layers

Next, we are going to modify our lambda function.

Step 1: Login to AWS console and navigate to ‘Lambda’.

Lambda Function Layers

Step 2: Click on the function name.

Lambda Function Layers

Step 3: Modify the code of the function and add an S3 trigger on create-object with cloudaffaire bucket.

Lambda Function Layers

Next, test the function by uploading an object to the S3 bucket.

Step 4: Upload an object in S3 bucket.

Lambda Function Layers

Check CloudWatch log

Lambda Function Layers

Observe: Our lambda function execution failed due to the missing pymysql module.

Next, we are going to create and add a layer for pymysql module.

Step 5: Login to EC2 instance and create the layer using AWS CLI.

Lambda Function Layers

Step 6: Add the layer to our existing lambda function and click ‘Save’.

Lambda Function Layers

Lambda Function Layers

Step 7: Test the function by uploading an object into the S3 bucket.

Lambda Function Layers

Check CloudWatch Logs.

Lambda Function Layers

Observe: Our lambda function with module pymysql successfully executed.

You can also check the MySQL database

Lambda Function Layers

Cleanup:

Remove the lambda layer pymysql from lambda function.

Hope you have enjoyed this article. In the next blog post, instead of layers, we will use the deployment package to include pymysql module to our lambda function.

To get more details on Lambda, please refer below AWS documentation

https://docs.aws.amazon.com/lambda/index.html

 

Leave a Reply