Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for How to create a Python Lambda Layer?
AWS Community Builders  profile imageGilbert Young Jr
Gilbert Young Jr forAWS Community Builders

Posted on • Edited on

     

How to create a Python Lambda Layer?

In this article I intend to explain what a Lambda Layer is, the benefits of it, and lastly, how to create a Layer for a Python Lambda function.

What is a Lambda Layer?

A Lambda Layer is an isolated zip file that contains libraries, packages and/or application code that is shareable between your Lambda functions. A common use case for a Lambda Layer is, for example your company has two separate Python Web Scrapers. Both functions use pandas to do some sort of data processing. Since they both use the same Python packages, the smart solution is to create a Lambda Layer, so both functions can access the Layer. Below is a visual from AWS that shows a solution with Lambda Layers vs without Lambda Layers.

Layers

Benefits of Lambda Layers

  • Reduces the size of uploaded deployment archives.
  • Increases the speed of deployment.

Creating a Python Lambda Layer

We will create the layer using an EC2 Instance that will be provisioned by IaC (CloudFormation). I know what you are thinking. Why do we need an EC2 Instance to simply create a Lambda Layer? Why not just do it locally? The architecture for a Python Lambda Layer requires a Linux OS. Doing it on a Linux EC2 Instance helps us meet the following requirements:

  1. AWS Lambda runs on a Linux machine
  2. Any Python Packages added to the Layer need to be compiled with the correct architecture (Linux x86_64).

You can find the complete CloudFormation template on myGitHub.

Create stack parameters

---Parameters:InstanceName:Type:StringDescription:Enter the name of your instance.Default:LinuxInstance
Enter fullscreen modeExit fullscreen mode

The only parameter we will be using is for our instance name.

Create IAM Role & Instance Profile

# Instance ProfileInstanceProfile:Type:AWS::IAM::InstanceProfileProperties:InstanceProfileName:!Join["_",[!RefInstanceName,"Profile"]]Path:/Roles:-!RefInstanceRole# Instance RoleInstanceRole:Type:AWS::IAM::RoleProperties:RoleName:!Join["_",[!RefInstanceName,"EC2PublishLambdaLayer"]]AssumeRolePolicyDocument:Version:2012-10-17Statement:-Effect:AllowPrincipal:Service:-ec2.amazonaws.comAction:-sts:AssumeRolePath:/Policies:-PolicyName:!Join["_",[!RefInstanceName,"Policy"]]PolicyDocument:Version:"2012-10-17"Statement:-Effect:AllowAction:"lambda:PublishLayerVersion"Resource:"*"
Enter fullscreen modeExit fullscreen mode

NOTE: The Policy Document contains the action lambda:PublishLayerVersion, without this policy we will not be able to publish the Lambda Layer from within the EC2 Instance.

Create EC2 Instance

# EC2 InstanceInstance:Type:AWS::EC2::InstanceProperties:ImageId:ami-09d3b3274b6c5d4aa# Static AMI ID chosen from List of AMIs in the consoleInstanceType:t2.microIamInstanceProfile:!RefInstanceProfileSecurityGroups:-!RefSSHSecurityGroup# Install Python, Pandas and Publish Lambda LayerUserData:Fn::Base64:|#!/bin/bashsudo amazon-linux-extras install python3.8curl -O https://bootstrap.pypa.io/get-pip.pypython3.8 get-pip.py --usermkdir -p home/ec2-user/pythoncd home/ec2-userpython3.8 -m pip install pandas -t python/zip -r layer.zip pythonaws lambda publish-layer-version --layer-name pandas-layer --zip-file fileb://layer.zip --compatible-runtimes python3.8 --region us-east-1Tags:-Key:NameValue:!RefInstanceName
Enter fullscreen modeExit fullscreen mode

If you look at the UserData property, we are passing bash scripts. This is where the magic happens. We defined scripts to install Python and pandas. We then zip the directory that has the Python Package and lastly publish the Lambda Layer to AWS.

Upload CF Stack

To upload your CF stack, use the command below.

aws cloudformation deploy--template-file <path_to_file>--stack-name <stack_name>--region us-east-1--capabilities CAPABILITY_NAMED_IAM
Enter fullscreen modeExit fullscreen mode

If the above command is successful, you will get an output in your terminal saying "Successfully created/updated stack - ". The stack should take a few minutes to provision our EC2 Instance and execute our scripts. Lastly, navigate to Lambda Layers in the AWS console and you should see the Layer that was created.

Lamda Layer

Clean Up

To clean up all the resources that were created simply delete the CloudFormation stack.

aws cloudformation delete-stack--stack-name <stack_name>
Enter fullscreen modeExit fullscreen mode

I hope you found this article useful. Stay curious, keep learning, and keep building. Thank you.

Top comments(3)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
avinashdalvi_ profile image
Avinash Dalvi
AWS Community Builder | Full Stack Developer | PHP + Angular + Python + AWS | Speaker | Blogger | LeadershipI write blog at https://www.internetkatta.com
  • Location
    Bangalore
  • Pronouns
    Avi
  • Work
    Senior Software Engineer at EagleView
  • Joined

Good article. Small correction in title required. Lamda should Lambda.

CollapseExpand
 
giyoungjr profile image
Gilbert Young Jr
Serverless Developer | AWS Community Builder | AWS User Group Leader
  • Email
  • Location
    San Ignacio, Belize
  • Education
    University of Belize
  • Pronouns
    him
  • Work
    Senior Serverless Developer at Serverless Guru
  • Joined

Appreciate it bro@avinashdalvi_ . Good looking out on the typo.

CollapseExpand
 
giyoungjr profile image
Gilbert Young Jr
Serverless Developer | AWS Community Builder | AWS User Group Leader
  • Email
  • Location
    San Ignacio, Belize
  • Education
    University of Belize
  • Pronouns
    him
  • Work
    Senior Serverless Developer at Serverless Guru
  • Joined

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Build On!

Would you like to become an AWS Community Builder? Learn more about the program and apply to join when applications are open next.

More fromAWS Community Builders

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp