<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Bhagyesh Rangole</title>
    <description>The latest articles on DEV Community by Bhagyesh Rangole (@bhagyesh_rangole).</description>
    <link>https://dev.to/bhagyesh_rangole</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3281930%2Ff55733de-59a1-47fb-810b-38204a7f47ce.png</url>
      <title>DEV Community: Bhagyesh Rangole</title>
      <link>https://dev.to/bhagyesh_rangole</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bhagyesh_rangole"/>
    <language>en</language>
    <item>
      <title>Complete Guide: Creating and Managing AWS Lambda Layers</title>
      <dc:creator>Bhagyesh Rangole</dc:creator>
      <pubDate>Sat, 21 Jun 2025 18:13:23 +0000</pubDate>
      <link>https://dev.to/bhagyesh_rangole/complete-guide-creating-and-managing-aws-lambda-layers-1jgp</link>
      <guid>https://dev.to/bhagyesh_rangole/complete-guide-creating-and-managing-aws-lambda-layers-1jgp</guid>
      <description>&lt;h2&gt;
  
  
  What Are AWS Lambda Layers?
&lt;/h2&gt;

&lt;p&gt;AWS Lambda layers are .zip archives that allow you to share libraries, custom runtimes, or other dependencies across multiple Lambda functions. Think of them as reusable code packages that can be attached to your Lambda functions without being included in your main function code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63pl1eil1hy6z0hin1y4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F63pl1eil1hy6z0hin1y4.png" alt="How Lambda layers works" width="800" height="520"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Key Benefits:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Code Reusability: Share common dependencies across multiple functions&lt;/li&gt;
&lt;li&gt;Easier Updates: Update shared libraries in one place instead of updating each function&lt;/li&gt;
&lt;li&gt;Better Organization: Separate business logic from dependencies&lt;/li&gt;
&lt;li&gt;Faster Deployments: Reuse existing layers instead of packaging dependencies with each function&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Common Challenges: Why Do My Imports Fail?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn398pg1r94xj86lnxegc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fn398pg1r94xj86lnxegc.png" alt="Common import errors in lambda" width="800" height="364"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is the most common problem when working with layers. It happens because the dependencies were installed on your local machine (like Windows or macOS), but Lambda runs on a different operating system (Linux).&lt;/li&gt;
&lt;li&gt;Many Python packages, like OpenCV (cv2), pandas, or NumPy, contain compiled code that is specific to the operating system they are installed on. When you try to use a macOS or Windows version of a package in Lambda's Linux environment, it fails to load, resulting in an ImportError.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Step-by-Step Process
&lt;/h2&gt;
&lt;h2&gt;
  
  
  Step 1: Create the Directory Structure
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mkdir -p openai-layer/python

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This creates a parent folder (openai-layer) with a python subdirectory. The python folder is crucial because Lambda expects dependencies to be in a python directory at the root of the layer.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 2: Install Dependencies with Platform-Specific Targeting
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install \
    --platform manylinux2014_x86_64 \
    --target=openai-layer/python \
    --implementation cp \
    --python-version 3.10 \
    --only-binary=:all: --upgrade \
    openai exceptiongroup

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;Key Parameters Explained:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- platform manylinux2014_x86_64&lt;/strong&gt;&lt;br&gt;
This is the most important parameter for Lambda layers&lt;br&gt;
Lambda runs on Amazon Linux 2, which uses the manylinux2014 platform. When you install packages on macOS or Windows, they contain platform-specific binaries that won't work on Lambda's Linux environment. This flag ensures all dependencies are compiled for the Linux environment that Lambda uses&lt;br&gt;
Without this, your layer will fail with import errors when deployed&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- target=openai-layer/python&lt;/strong&gt;&lt;br&gt;
Specifies where to install the packages&lt;br&gt;
Must point to the python subdirectory we created&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- implementation cp&lt;/strong&gt; &lt;br&gt;
Ensures we're using CPython (standard Python implementation)&lt;br&gt;
Lambda uses CPython, so this ensures compatibility&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- python-version 3.10&lt;/strong&gt;&lt;br&gt;
Matches your Lambda runtime version&lt;br&gt;
Must match the runtime specified in your serverless.yaml&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- only-binary=:all:&lt;/strong&gt;&lt;br&gt;
Forces pip to use pre-compiled wheels when available&lt;br&gt;
Faster installation and ensures compatibility&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;- upgrade&lt;/strong&gt;&lt;br&gt;
Updates packages to their latest compatible versions&lt;/p&gt;
&lt;h2&gt;
  
  
  Step 3: Create the Layer Package
&lt;/h2&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd openai-layer
zip -r ../openai-layer.zip .

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This creates a ZIP file containing all the dependencies. The structure should be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;openai-layer.zip
└── python/
    ├── openai/
    ├── exceptiongroup/
    └── [other dependencies]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Upload to S3
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;aws s3 cp openai-layer.zip s3://your-bucket-name/openai-layer.zip

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Replace your-bucket-name with your actual S3 bucket name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 5: Create/Update Lambda Layer via AWS Console
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Go to AWS Lambda Console&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to AWS Lambda service&lt;/li&gt;
&lt;li&gt;Click on "Layers" in the left sidebar&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;2. Create New Layer&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Click "Create layer"&lt;/li&gt;
&lt;li&gt;Enter layer name (e.g., openai-layer)&lt;/li&gt;
&lt;li&gt;Upload the ZIP file or provide S3 link&lt;/li&gt;
&lt;li&gt;Select compatible runtimes (Python 3.10)&lt;/li&gt;
&lt;li&gt;Click "Create"&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52ug8rbg5tk3lrke017f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F52ug8rbg5tk3lrke017f.png" alt="Creating new layer" width="800" height="384"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Update Existing Layer&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Find your existing layer&lt;/li&gt;
&lt;li&gt;Click "Create new version"&lt;/li&gt;
&lt;li&gt;Upload the new ZIP file or upload from s3&lt;/li&gt;
&lt;li&gt;The new version will be automatically created&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1606u19b0nvsrjrah1m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1606u19b0nvsrjrah1m.png" alt="updating existing lambda layer" width="800" height="351"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Attach Layer to AWS Lambda&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Open your Lambda function in the AWS Console.&lt;/li&gt;
&lt;li&gt;Under the Layers section, click Add a layer.&lt;/li&gt;
&lt;li&gt;Select your custom layer, specify the version, and attach it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ffu87uvqocg3orlgfv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ffu87uvqocg3orlgfv8.png" alt="Attaching layer to lambda" width="800" height="341"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
    </item>
  </channel>
</rss>
