0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

python 3.13のlambdaレイヤー構築

Last updated at Posted at 2025-06-08

以下ドキュメントを参照してレイヤーを作成する

 Python Lambda 関数にレイヤーを使用する
 https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/python-layers.html

なお、以下ドキュメントの通り、Python 3.12 以降のベースイメージは、
Amazon Linux 2023 の最小コンテナイメージが利用される

 コンテナイメージで Python Lambda 関数をデプロイする
 https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/python-image.html

そのため、レイヤーも Amazon Linux 2023 で作成すべくレイヤー作成用のインスタンスを用意

スクリーンショット 2025-06-03 033346.png

インスタンス作成後、デフォルトで導入されているpythonを確認すると 3.9 だった

 [ec2-user@xxxxxxxxx0 ~]$ python3 --version
 Python 3.9.22

今回は 3.13 が目的なのでまず python 3.13 を導入する
以下の記事を参考にさせていただいて導入

 Amazon Linux2023にpyenvを利用してpython3.12をインストール
 https://qiita.com/YujiHamada3/items/e1124b78f7d89b53c498

その後、pipとAWS CLIも導入

 AWS CLI の最新バージョンのインストールまたは更新
 https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/getting-started-install.html

次に numpyだが、公式ドキュメントの「1-install.sh」は python3.11 のコマンドになっており
今回は 3.13 なのでそのままやるとエラーになる。

なので「1-install.sh」部分は手動で numpy をインストールし「2-package.sh」だけ利用した

[ec2-user@xxxxxxxxx layer-numpy]$ python -m venv create_layer
[ec2-user@xxxxxxxxx layer-numpy]$ ls
1-install.sh  2-package.sh  create_layer  requirements.txt
[ec2-user@xxxxxxxxx layer-numpy]$ source create_layer/bin/activate
(create_layer) [ec2-user@xxxxxxxxx layer-numpy]$ pip install numpy
Collecting numpy
  Downloading numpy-2.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (62 kB)
Downloading numpy-2.2.6-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (16.5 MB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 16.5/16.5 MB 83.1 MB/s eta 0:00:00
Installing collected packages: numpy
Successfully installed numpy-2.2.6

[notice] A new release of pip is available: 25.0.1 -> 25.1.1
[notice] To update, run: pip install --upgrade pip
(create_layer) [ec2-user@xxxxxxxxx layer-numpy]$ pip list
Package Version
\------- -------
numpy  2.2.6
pip   25.0.1

(create_layer) [ec2-user@xxxxxxxxx layer-numpy]$ ./2-package.sh
  adding: python/ (stored 0%)
  adding: python/lib/ (stored 0%)
  adding: python/lib/python3.13/ (stored 0%)
~~~~~~~中略~~~~~~~~~~~~~~~~~~~

(create_layer) [ec2-user@xxxxxxxxx layer-numpy]$ ls
1-install.sh  2-package.sh  create_layer  layer_content.zip  python  requirements.txt

(create_layer) [ec2-user@xxxxxxxxx layer-numpy]$ aws configure
AWS Access Key ID [None]: xxxx
AWS Secret Access Key [None]: xxxx
Default region name [None]: ap-northeast-1
Default output format [None]: json

(create_layer) [ec2-user@xxxxxxxxx layer-numpy]$ aws lambda publish-layer-version --layer-name python-numpy-layer --zip-file fileb://layer_content.zip --compatible-runtimes python3.13 --compatible-architectures "x86_64"
{
    "Content": {
        "Location": "https://xxxxxxxxxxxxx",
        "CodeSha256": "xxxxxxxxx",
        "CodeSize": 25093140
    },
    "LayerArn": "xxxxxx",
    "LayerVersionArn": "xxxxxx",
    "Description": "",
    "CreatedDate": "2025-06-02T20:06:04.745+0000",
    "Version": 1,
    "CompatibleRuntimes": [
        "python3.13"
    ],
    "CompatibleArchitectures": [
        "x86_64"
    ]
}

これでレイヤー python-numpy-layer が作成されるため、以下の「レイヤーの追加」から追加

スクリーンショット 2025-06-03 053424.png

レイヤー追加前は以下の通りテストを実施してもモジュール不足のエラーが出るが

スクリーンショット 2025-06-03 053534.png

レイヤーを追加すればエラーは解消することを確認

スクリーンショット 2025-06-03 053631.png

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?