How to import Python library set up in EFS to Lambda

Introduction

You can now mount ** EFS ** on ** Lambda **. This allows you to import Python libraries set up in EFS into Lambda.

Until now, if you wanted to import a library other than the Python standard to Lambda, the only way was to upload it to the Lambda layer. However, this method has a size limit of compressed to 50MB or less and decompressed to 250MB or less. For this reason, it was not possible to use multiple large libraries. Therefore, if the system is a little large, it is necessary to take measures such as going out some processing to docker, and Lambda alone was not complete.

However, now that you can mount EFS on Lambda, this issue goes away. ʻYou can now import libraries installed on EFS into Lambda. ``

how to

Overview

  1. Create EFS
  2. Create an EFS access point
  3. Mount EFS on EC2 for work
  4. Install Python and libraries on EFS
  5. Import to Lambda

procedure

  1. Create EFS --Creation procedure abridgement
  1. Create an EFS access point (client access) Owner User ID: 1001 Owner group ID: 1001 Permissions: 777 The path (directory) is set here to / lambda

  2. Mount EFS on working EC2

    sudo yum install -y amazon-efs-utils
    cd /mnt
    sudo mkdir efs
    sudo mount -t efs fs-xxxxx:/ efs
    
  3. Install Python and libraries on EFS Install Python3.8.1 and pandas here

    sudo yum -y install gcc openssl-devel bzip2-devel libffi-devel
    wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
    tar xzf Python-3.8.1.tgz
    
    cd Python-3.8.1
    sudo ./configure --enable-optimizations
    sudo make altinstall
    
    # check
    python3.8 --version
    pip3.8 --version
    
    cd /mnt/efs
    pip3.8 install --upgrade --target lambda/ pandas
    
  4. Set up Lambda function Need to belong to VPC Added policy ʻAmazonElasticFileSystemClientReadWriteAcces` to Lambda execution role Add EFS

  5. Import with Lambda You can import it like this.

    import sys
    sys.path.append("/mnt/efs")
    import pandas as pd
    

Recommended Posts

How to import Python library set up in EFS to Lambda
[Python] How to import the library
How to use the C library in Python
Summary of how to import files in Python 3
How to use Python Image Library in python3 series
[AWS / Lambda] How to load Python external library
How to develop in Python
How to use Python lambda
How to set up public key authentication in ssh
How to set up a Python environment using pyenv
How to debug the Python standard library in Visual Studio
How to collect images in Python
How to use Requests (Python Library)
How to use SQLite in Python
How to import a file anywhere you like in Python
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
How to speed up Python calculations
How to handle Japanese in Python
How to set up a simple SMTP server that can be tested locally in Python
[For beginners] How to register a library created in Python in PyPI
[Beginner memo] How to specify the library reading path in Python
[Introduction to Python] How to use class in Python?
How to dynamically define variables in Python
How to do R chartr () in Python
How to set up SVM using Optuna
[Itertools.permutations] How to put permutations in Python
To set default encoding to utf-8 in python
How to work with BigQuery in Python
[Python] How to use import sys sys.argv
How to get a stacktrace in python
How to display multiplication table in python
How to extract polygon area in Python
How to check opencv version in python
How to access RDS from Lambda (python)
How to switch python versions in cloud9
How to adjust image contrast in Python
How to use __slots__ in Python class
How to dynamically zero pad in Python
How to use regular expressions in Python
How to display Hello world in python
How to use is and == in Python
How to write Ruby to_s in Python
How to generate a new loggroup in CloudWatch using python within Lambda
How to get a value from a parameter store in lambda (using python)
Summary of how to set up major Python Lint (pep8, pylint, flake8)
Set up a simple HTTPS server in Python 3
How to receive command line arguments in Python
[REAPER] How to play with Reascript in Python
How to clear tuples in a list (Python)
How to generate permutations in Python and C ++
How to embed a variable in a python string
How to set up Random forest using Optuna
How to set browser location in Headless Chrome
I was able to recurse in Python: lambda
How to simplify restricted polynomial fit in python
How to implement shared memory in Python (mmap.mmap)
How to create a JSON file in Python
Summary of how to use MNIST in Python