※当サイトはPRを含みます

【AWS】Lambdaでpillow, pandasなどのPython外部ライブラリを使用するためのLayer作成 (ARN早見表付き)

2023年8月30日

はじめに

AWS Lambdaで Pythonを使う際、外部ライブラリを利用するにはLayerを用意する必要があります。

外部ライブラリをまとめたzipファイルを使って毎回Layerを作成するのは効率が悪いので先人の知恵を借りて効率よく開発したいと思います。

1. 使いたいライブラリのARNを調べる

必要な情報は以下の二つになります。

• Lambda実行環境のPythonのバージョン
• Lambda関数が所属しているリージョン

ここでは、Lambda実行環境のPythonのバージョンが Python3.10、 所属しているのはap-northeast-1 (アジア パシフィック (東京リージョン)として、画像処理ライブラリのPillow を実行する Layerを作成してみます。

下記リンクのgitにアクセスしてほしいライブラリのARNを取得します。

https://github.com/keithrozario/Klayers#list-of-arns

[deployments] > [python3.10] と辿ると各リージョンのARNが用意されているページが現れます。

目的のリージョンであるap-northeast-1のARNのcsvファイルを開くと、様々なライブラリのARNが載っているのでpillowのARNをコピーします。

arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-Pillow:2

2. Lambda関数の作成

・Lambda コンソールの [Functions] (関数) ページを開きます。

・設定する関数を選択します。

・作成した関数のスクリプトの先頭にimport pillow を追加します。

import pillow
import json
def lambda_handler(event, context):
# TODO implement return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!'
}

この時点でテストを実行するとpillowがインポートできないのでエラーが返されます。

3. Layerの追加

[Layers] (レイヤー) で [Add a layer] (レイヤーの追加) をクリックします。

・テキストボックスに 先ほどの1でコピーしたARN を入力し、[Verify (検証)] を選択します。

[Layers] (レイヤー) で [Add a layer] (レイヤーの追加) をクリックします。[Add] (追加) を選択します。

4. 実行

これでレイヤーの追加ができたので、先ほど設定したテストを実行してpillow が使えるか確認します。
先ほど設定したテストを実行します。

pillowがインポートされている状態で、 関数が正常終了したことが分かります。

ARN早見表(一部抜粋)

Python3.8 アジアパシフィック(東京リージョン)

t.md
Package Package Version arn
Pillow 9.5.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-Pillow:7
pandas 2.0.1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-pandas:15
numpy 1.24.3 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-numpy:12
matplotlib 3.7.1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-matplotlib:17
geopy 2.3.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-geopy:1
scipy 1.3.3 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-python38-scipy:1
opencv-python-headless 4.2.0.34 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-python38-opencv-python-headless:11
PyYAML 6 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-python38-PyYAML:5
boto3 1.26.129 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p38-boto3:17

Python3.9 アジアパシフィック(東京リージョン)

t.md
Package Package Version arn
pandas 2.0.1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-pandas:14
sklearn 0.0.post1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-sklearn:1
PyYAML 6 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-PyYAML:1
pillow 9.2.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-pillow:1
numpy 1.24.3 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-numpy:12
boto3 1.26.129 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-boto3:15
matplotlib 3.5.2 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-matplotlib:1
dynamodb-encryption-sdk 3.2.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-dynamodb-encryption-sdk:15
sympy 1.11.1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p39-sympy:4

Python3.10 アジアパシフィック(東京リージョン)

t.md
Package Package Version arn
aws-requests-auth 0.4.3 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-aws-requests-auth:1
pandas 2.0.1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-pandas:1
mysql-connector-python 8.0.33 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-mysql-connector-python:1
pyqldb 3.2.2 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-pyqldb:1
openpyxl 3.1.2 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-openpyxl:1
numpy 1.24.3 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-numpy:1
requests 2.30.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-requests:1
Pillow 9.5.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-Pillow:2
idna 3.4 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-idna:1
dynamodb-encryption-sdk 3.2.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-dynamodb-encryption-sdk:1
aws-xray-sdk 2.12.0 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-aws-xray-sdk:1
redshift-connector 2.0.910 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-redshift-connector:1
jinja2 3.1.2 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-jinja2:1
boto3 1.26.129 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-boto3:1
beautifulsoup4 4.12.2 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-beautifulsoup4:1
cryptography 40.0.2 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-cryptography:1
bcrypt 4.0.1 arn:aws:lambda:ap-northeast-1:770693421928:layer:Klayers-p310-bcrypt:1

AWSAWS,python