Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building AWS Lambda layers with custom python functions using SAM

I am trying to build my lambda function using SAM. My lambda function depends on a custom python function which I plan to build as an AWS Lambda layer. My custom python function has a transitive dependency on a python package publicly available on PyPI, which I specified in the requirements.txt file for the layer.

Here's the folder structure for my lambda function:

my-lambda-func
|-- events
    |-- event.json
|-- my-func
    |-- my_function.py
    |-- __init__.py
    |-- requirements.txt
|-- my-layer
    |-- my_layer.py
    |-- requirements.txt
|-- template.yaml 
|-- buildspec.yml

Here's my template.yaml file:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  sam-test

  Sample SAM Template for sam-test

Globals:
  Function:
    Timeout: 300

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: my-func/
      Handler: app.lambda_handler
      Layers:
        - !Ref MyLayer
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

  MyLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
        LayerName: MyLayer
        Description: My custom Lambda Layer
        ContentUri: ./my-layer/
        CompatibleRuntimes:
            - python3.8
        LicenseInfo: MIT
        RetentionPolicy: Retain
    Metadata:
        BuildMethod: python3.8

My buildspec file:

version: 0.2

phases:
  build:
    commands:
      - sam package --template-file template.yaml --s3-bucket my-bucket --output-template-file packaged-template.yml

artifacts:
  files:
    - packaged-template.yml

when I run a sam build command locally, I see the 2 resources getting created correctly including pulling the transitive dependencies for the layer but when I setup CodePipeline with a build phase to build the command from builspec file and a deploy phase using CloudFormation, the layer isn't built as I expect it to. When I download the layer from the console, it doesn't show any transitive dependencies.

Has anyone done anything similar to this. Can somebody help with what I am doing wrong? Thanks!

like image 351
Ashok Avatar asked Oct 25 '25 02:10

Ashok


2 Answers

Never used Lambda Layer, though what captured my eye w.r.t. my buildspec.yml is that you're not running sam build before packaging. Here's my buildspec file, maybe it helps

phases:
  install:
    runtime-versions:
      python: 3.7

  build:
    commands:
      - pip install --upgrade aws-sam-cli
      - sam build
      - sam package --output-template-file packaged.yaml --s3-bucket ${FUNCTIONS_BUCKET}

artifacts:
  type: zip
  files:
    - packaged.yaml
like image 174
Stefano Messina Avatar answered Oct 26 '25 17:10

Stefano Messina


Stefano is correct, All you are packaging of the layer is the definition. You need to run sam build before you package.

like image 26
Eric Johnson Avatar answered Oct 26 '25 15:10

Eric Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!