LoginSignup
0
0

【データ基盤構築/AWS】Lambda Layerを使ってLambdaでpandasライブラリを使用する方法

Last updated at Posted at 2023-08-11

今回の課題

AWS Lambdaでpandasライブラリを使用したかった。
それにあたり、pandasが入った自作のLambda Layerを使用してLambdaを実行したのだが、
エラーが発生してしまったので解決する手順を記事にした。

躓いた箇所

以下の手順でLayerを作成して、Lambdaを実行してみた。
すると、依存関係系のエラーが発生してしまった。

Layerを作成した手順

  1. ローカルにpythonフォルダを作成
  2. pythonフォルダ内で、$ pip install -t . pandasコマンドを実行してpandasをインストール
  3. $ zip -r9 layers.zipコマンドを実行してpythonファイルをzipファイルに圧縮
  4. zipファイルをLayer作成画面でアップロードimage.png

発生したエラー

上記で作成したLayerをLambdaに設定してLambdaを実行してみたところ、
「Numpyのインストール方法に問題がある。」といった内容のエラーが発生してしまった。

Test Event Name
test

Response
{
  "errorMessage": "Unable to import module 'lambda_function': Unable to import required dependencies:\nnumpy: \n\nIMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!\n\nImporting the numpy C-extensions failed. This error can happen for\nmany reasons, often due to issues with your setup or how NumPy was\ninstalled.\n\nWe have compiled some common reasons and troubleshooting tips at:\n\n    https://numpy.org/devdocs/user/troubleshooting-importerror.html\n\nPlease note and check the following:\n\n  * The Python version is: Python3.8 from \"/var/lang/bin/python3.8\"\n  * The NumPy version is: \"1.25.1\"\n\nand make sure that they are the versions you expect.\nPlease carefully study the documentation linked above for further help.\n\nOriginal error was: No module named 'numpy.core._multiarray_umath'\n",
  "errorType": "Runtime.ImportModuleError",
  "stackTrace": []
}

Function Logs
START RequestId: ************** Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': Unable to import required dependencies:
numpy: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.8 from "/var/lang/bin/python3.8"
  * The NumPy version is: "1.25.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: No module named 'numpy.core._multiarray_umath'

Traceback (most recent call last):END RequestId: **************
REPORT RequestId: **************	Duration: 2.21 ms	Billed Duration: 3 ms	Memory Size: 3008 MB	Max Memory Used: 41 MB	Init Duration: 180.67 ms

Request ID
**************

原因と解決策

調査した結果、ローカル環境でpandasをインストールしてしまったことに問題があることがわかった。
AWS Lambdaではランタイム毎に実行環境(OS)が異なり、
対応している実行環境上でライブラリをインストールしてLayerを作成する必要がある
ようだった。

Lambdaで動かすpythonはAmazon Linuxまたは、Amazon Linux2上で動いているため、
pandasをそれらの環境下でインストールしてzipファイルを作成し、そちらを使用してLayerを作成する必要があった。

以下のどちらかの方法で、
Amazon Linux or Amazon Linux2の環境下でpandasをインストールすることができる。

  • 方法1
    EC2でAmazon Linuxまたは、Amazon Linux2でインスタンスを立てて、その中で$ pip install -t . pandasをする。

  • 方法2
    LambdaのDockerイメージを使用して、その中で$ pip install -t . pandasをする。

解決した手順

今回は、方法2で解決することにした。

1)Lambdaの実行環境のDockerイメージを取得する

$ docker pull amazon/aws-sam-cli-build-image-python3.8

2)コンテナを作成する

$ sudo docker run -it -v $(pwd):/var/task amazon/aws-sam-cli-build-image-python3.8:latest

-v $(pwd):/var/taskの部分で、ホストマシンのカレントディレクトリ($(pwd))をDockerコンテナ内の/var/taskディレクトリにマウントするように設定している。
 /var/taskディレクトリはLambdaのコードが配置されるデフォルトのディレクトリのこと。

3)コンテナ内でpandasをインストールする

pythonディレクトリの中にpandasをインストールして、zipファイル化する。

bash-4.2# pip install pandas -t ./python
bash-4.2# zip -r python.zip ./python

これでDocker環境で作ったファイルがローカルに作られるので、
生成されたファイルをアップロードして、Layerを作成した手順の通りLayerを作成することで、Lambdaでpandasライブラリを使えるようになった。

まとめ

自作のLambda Layerを作成する時は、
使用するランタイムの実行環境(OS)と同じOS上でライブラリをインストールしてzipファイル化して、そちらをアップロードしてLayerを作成する必要があるということを学んだ。

参考にした記事

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0