python Pillow(AWS Lambda Custom Layer)
pillow qrcode のレイヤー作成を確認する
import os
import sys
from PIL import Image
import qrcode
def lambda_handler(event, context):
try:
# 環境情報の出力
print("Python version:", sys.version)
print("PYTHONPATH:", os.environ.get('PYTHONPATH'))
print("sys.path:", sys.path)
# qrcode モジュールのパスを確認
print("qrcode module location:", qrcode.__file__)
# Pillow のテスト
img = Image.new('RGB', (60, 30), color = (73, 109, 137))
print("Pillow is working.")
# qrcode のテスト
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('https://example.com')
qr.make(fit=True)
img_qr = qr.make_image(fill='black', back_color='white')
print("qrcode is working.")
return {
'statusCode': 200,
'body': 'Both Pillow and qrcode import successful.'
}
except Exception as e:
print("Error:", str(e))
return {
'statusCode': 500,
'body': 'Import failed. Error: ' + str(e)
}
test 結果
Test Event Name
(unsaved) test event
Response
{
"statusCode": 200,
"body": "Both Pillow and qrcode import successful."
}
Function Logs
START RequestId: 5fa70ea0-a0bf-4348-ba6b-7fc656759d33 Version: $LATEST
Python version: 3.10.14 (main, Apr 9 2024, 08:38:02) [GCC 7.3.1 20180712 (Red Hat 7.3.1-17)]
PYTHONPATH: /var/runtime
sys.path: ['/var/task', '/opt/python/lib/python3.10/site-packages', '/opt/python', '/var/runtime', '/var/lang/lib/python310.zip', '/var/lang/lib/python3.10', '/var/lang/lib/python3.10/lib-dynload', '/var/lang/lib/python3.10/site-packages', '/opt/python/lib/python3.10/site-packages']
qrcode module location: /opt/python/qrcode/init.py
Pillow is working.
qrcode is working.
END RequestId: 5fa70ea0-a0bf-4348-ba6b-7fc656759d33
REPORT RequestId: 5fa70ea0-a0bf-4348-ba6b-7fc656759d33 Duration: 103.12 ms Billed Duration: 104 ms Memory Size: 128 MB Max Memory Used: 38 MB