前提条件
・「test-function」という名前のLambda functionを作成している。
1. 外部ライブラリの作成
利用する外部ライブラリを「python」フォルダにインストールして、ZIP形式で圧縮してください。
※フォルダの名前は必ず「python」にしてください。
$ mkdir python $ pip install -t python requests $ zip -r test-layer.zip python
2. Lambda layerの作成
$ aws lambda publish-layer-version --layer-name test-layer --zip-file fileb://test-layer.zip --compatible-runtimes python3.12
3. Lambda layerをLambda functionに追加
$ aws lambda update-function-configuration --function-name test-function --layers "arn:aws:lambda:ap-northeast-1:{AWSアカウントID}:layer:test-layer:1"
4. 外部ライブラリの利用
Lambda functionの中で外部ライブラリをインポートして利用することができます。
lambda_function.py
import json import requests def lambda_handler(event, context): response = requests.get('https://api.example.com/v1/items') return { 'statusCode': response.status_code, 'body': response.json() }