Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

153
Views
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!

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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
over 4 years ago · Santiago Trujillo Report

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!