Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

152
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda