How to set layer on Lambda using AWS SAM

How to set up a layer on Lambda using AWS SAM

Introduction

When building a serverless application using Lambda or dynamoDB with AWS SAM, the code commonly used in various projects may come out. It would be convenient if things that could be shared could be shared quickly and called when needed, so I immediately put it into practice.

AWS Lambda has a layer function, and you can set the library you want to use in the Lambda code. I tried to find out how to set the code and libraries that depend on Layer as a project of AWS SAM.

I found some ways to zip up the libraries needed for Layer and save them in S3 and configure them from the AWS console, but I couldn't quite get the method to deploy them as an AWS SAM project, so I wrote it as a memorandum. I will leave it.

Prerequisites

We will assume that you have already installed the AWS SAM CLI. This explanation uses Python 3.6.

Directory structure

The directory for storing the code and libraries you want to use as a layer is determined for each language. Include AWS Lambda library dependencies in layers (https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/configuration-layers.html#configuration-layers-path)

For python, if you do not store it in python or python / lib / python3.6 / site-packages, the following reference error will be displayed.

Unable to import module 'app': cannot import name 'layer_code'

Add the directory to store the layer to the SAM project created by the sam init command.

sam-app
├── README.md
├── app
│   ├── __init__.py
│   ├── app.py
│   └── requirements.txt
├── events
│   └── event.json
├── layer                       //Add a layer storage directory here
│   └── python                  //Language name directory
│       └── sample
│           └── sample_layer.py   //Code you want to use as a layer
└── template.yaml

How to write template.yaml

template.yaml


  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: hello-world-sample-function
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.6
      Layers:
        - !Ref LayerSampleLayer     #Point 1
      Events:
        (abridgement)

  #Layer definition
  LayerSampleLayer:
    Type: AWS::Serverless::LayerVersion
    Properties:
      LayerName: layer-sample-layer
      Description: Hello World Sample Application Resource Layer
      ContentUri: layer              #Point 2

There are two points in writing template.yaml.

  1. Add the Layers item to the Lambda function definition and set it to refer to the Layer definition.

  2. In the layer definition, set the folder path that stores the layer code. → To be exact, set the parent directory of the language name directory

import in app.py

If you set the layer in template.yaml, you can use it as if it is in the same layer as app.py. The settings will not be reflected unless you do sam build once.

app.py


#Browse the file added as a layer
from sample import sample_layer

def lambda_handler(event, context):
    res = sample_layer.hello()
    return res

sample_layer.py


def hello():
    return "hello Layer!"

reference

What is the AWS Serverless Application Model (AWS SAM)](https://docs.aws.amazon.com/ja_jp/serverless-application-model/latest/developerguide/what-is-sam.html) AWS Lambda Layers

Recommended Posts

How to set layer on Lambda using AWS SAM
How to set up SVM using Optuna
Regularly post to Twitter using AWS lambda!
How to set xg boost using Optuna
Summary of how to write AWS Lambda
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
How to set up a jupyter notebook on ssh destination (AWS EC2)
[2020 version] How to install Python3 on AWS EC2
How to set up Random forest using Optuna
How to set up Random forest using Optuna
[AWS / Lambda] How to load Python external library
How to set up a Python environment using pyenv
Check types_map when using mimetypes on AWS Lambda (Python)
I tried to get an AMI using AWS Lambda
I want to AWS Lambda with Python on Mac!
How to play Cyberpunk 2077 on Linux/Ubuntu 20.04 using AMD GPU
[AWS EC2] How to install Maven on Amazon Linux 2
[AWS] How to expand disk space on Amazon linux
A little trick to know when writing a Twilio application using Python on AWS Lambda
How to register on pypi
Web scraping using AWS lambda
How to use Python lambda
Posted as an attachment to Slack on AWS Lambda (Python)
How to import Python library set up in EFS to Lambda
Addictive points when downloading files using boto on AWS Lambda
Create API with Python, lambda, API Gateway quickly using AWS SAM
[Python] Allow pip3 packages to be imported on AWS Lambda
How to set cron for regular Python scraping on Sakura server.
How to install python using anaconda
Summary if using AWS Lambda (Python)
How to install mysql-connector-python on mac
How to use Dataiku on Windows
Notes on how to use pywinauto
How to install graph-tool on macOS
Things to note when running Python on EC2 from AWS Lambda
How to install VMware-Tools on Linux
How to install pycrypto on Windows
How to deploy django-compressor on Windows
Run Python on Schedule on AWS Lambda
Notes on how to use featuretools
How to install OpenCV on Mac
How to run matplotlib on heroku
How to install PyPy on CentOS
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
How to use homebrew on Debian
Tweet WakaTime Summary using AWS Lambda
Misunderstanding on how to connect cnn
How to install TensorFlow on CentOS 7
Using Lambda with AWS Amplify with Go
How to add AWS EBS volume
Set up a node to do MNIST on ROS using Tensorflow
Notes on how to use doctest
How to install Maven on CentOS
Notes on how to write requirements.txt
How to install Go on Ubuntu
[For Python] Quickly create an upload file to AWS Lambda Layer
How to install music 21 on windows
How to install Theano on Mac OS X 10.10 (using pyenv, anaconda)
[AWS SAM] Introduction to Python version
How to create a serverless machine learning API with AWS Lambda
How to generate a new loggroup in CloudWatch using python within Lambda