dehio3’s diary

仕事、生活、趣味のメモ

lambdaで「No module named 'numpy.core._multiarray_umath'」の対応したメモ

事象

ローカル環境で動作確認したPythonをLambdaで動かしたら以下のエラーが発生

START RequestId: 8fadcafe-2b18-419b-a131-ac8bebee82a3 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda/main': 

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.7 from "/var/lang/bin/python3.7"
  * The NumPy version is: "1.19.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'

END RequestId: 8fadcafe-2b18-419b-a131-ac8bebee82a3
REPORT RequestId: 8fadcafe-2b18-419b-a131-ac8bebee82a3  Duration: 1.63 ms   Billed Duration: 100 ms Memory Size: 256 MB Max Memory Used: 62 MB  

前提条件

調査

実行環境のPythonバージョンを確認

ローカル

.venv ❯ python --version 
Python 3.7.4

.venv ❯ pip list        
Package         Version
--------------- ---------
boto3           1.14.37
botocore        1.17.37
certifi         2020.6.20
chardet         3.0.4
cycler          0.10.0
docutils        0.15.2
idna            2.10
jmespath        0.10.0
kiwisolver      1.2.0
matplotlib      3.2.2
numpy           1.19.1
Pillow          7.2.0
pip             20.1.1
pyparsing       2.4.7
python-dateutil 2.8.1
python-dotenv   0.14.0
requests        2.24.0
s3transfer      0.3.3
setuptools      46.4.0
six             1.15.0
urllib3         1.25.10
wheel           0.34.2

Lambda

Lambdaのバージョンを確認

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys


def main(event, context):
    print(sys.version)
3.7.8 (default, Jul 30 2020, 14:26:00) 
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]

docs.aws.amazon.com

対応

ローカルとバージョンが合ってなかったので、合わせて再度検証

ローカルのPythonバージョンを変更

pyenvでローカル環境を管理してるけど、3.7.8がない

❯ pyenv install 3.7.8 
python-build: definition not found: 3.7.8

See all available versions with `pyenv install --list'.

If the version you need is missing, try upgrading pyenv:

  brew update && brew upgrade pyenv

そもそもpyenvが最新じゃないからupgrade

❯ pyenv -v           
pyenv 1.2.13
❯ brew upgrade pyenv
❯ pyenv -v           
pyenv 1.2.20

3.7.8をインストール

❯ pyenv install 3.7.8

ローカル環境を再度作成

❯ pipenv --rm 
❯ grep python_version Pipfile                               
python_version = "3.7.8"
❯ pipenv install
❯ pipenv shell 
.venv ❯ python --version     
Python 3.7.8

ローカル環境で動作確認後、Lambdaデプロイして動かしたけど事象変わらず。。

serverless frameworkでアップロードしてるzipには対象のモジュール含まれてる

.venv ❯ tar tvf .serverless/aws-cost-report.zip | grep _multiarray_umath
-rwxr-xr-x  0 0      0     4641720  1  1  2098 numpy/core/_multiarray_umath.cpython-37m-darwin.so

パッケージを取得してる環境の影響の問題

MacOS上ではなく、AWS Linux OS上でパッケージを取得する必要がある

github.com

ちなみに上記を見て初めてAWS提供するLayerがあることを知った

aws.amazon.com

色々見てたらクラメソさんの記事を発見!!

dev.classmethod.jp

まさにserverless framework使ってリリースしてたので、これを参考にしたら行けそう!

serverless-python-requirements設定を変更

dockerizePipの設定を追加

www.serverless.com

  pythonRequirements:
    dockerizePip: true

再度デプロイしたらlambdaでも動いた!!

ちなみに一度作ってるとキャッシュを使ってしまうので、deplyログの以下で表示されるキャッシュのディレクトリを削除して再実行する

Serverless: Using static cache of requirements found at .....

slimオプションを試す

slimを有効にして、サイズがどのくらい変わるか確認してみる

  pythonRequirements:
    dockerizePip: true
    slim: true

Serverless: Uploading service aws-cost-report.zip file to S3 (40.43 MB)...

Serverless: Uploading service aws-cost-report.zip file to S3 (33.93 MB)...

まとめ

Pure Pythonなライブラリかどうか意識しなくていいように常にdockerizePipを有効にしておこう!