4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS Lambda PythonでULIDを使う

Posted at

AWS Lambda でULIDを使う(ライブラリをコピーして使う)

  • 以前は、dockerコンテナ使ってULIDを生成したけど、もう少し手軽さが欲しい
  • Lambda Layerまで使いたくない
  • ULIDは、https://github.com/mdomke/python-ulid を使ってみる
  • とりあえず動かすだけ

AWS Lambda Functionの作成

  • マネジメントコンソールで関数を作成します
  • 参考画像
    image.png

02.png

python-ulid の ulid ディレクトリと中のファイルをコピー

sugimoto-python-ulidディレクトリの下に ulid ディレクトリの作成、ファイルの作成し、GitHubのコードを貼り付ける
image.png

Lambda_fucntion.pyを変える

import time
import uuid
from datetime import datetime
from datetime import timedelta
from datetime import timezone

from ulid import base32
from ulid import constants
from ulid import ULID
import json

# see , https://github.com/mdomke/python-ulid

def lambda_handler(event, context):
    
    for i in range(10):
        print(str(ULID()))

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
  • コンソール画面の「Deploy」ボタンをを押して、Testを実行してみる

エラーになる
image.png

Response
{
  "errorMessage": "Unable to import module 'lambda_function': python-ulid",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "e1c22481-d695-4148-bd35-b129bedebc25",
  "stackTrace": []
}

Function Logs
START RequestId: e1c22481-d695-4148-bd35-b129bedebc25 Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': python-ulid

__init__.py

__version__ = version("python-ulid")

で、エラーになる

#__version__ = version("python-ulid")

コメントにする

「Deploy」ボタンを押して、Testを実行

ULIDがログ出力できた
image.png

Response
{
  "statusCode": 200,
  "body": "\"Hello from Lambda!\""
}

Function Logs
START RequestId: 51874246-fcd2-41d5-911a-829a3bcf73c0 Version: $LATEST
01G3NTE0HZXA245Y04H1ZYW5S1
01G3NTE0HZ93YM8STPE6NH2ZKJ
01G3NTE0HZ7MH15CWJP7ZJ8VK2
01G3NTE0HZAAFMZF55KRQQDXAX
01G3NTE0HZZHP0PDK588F92D5M
01G3NTE0HZMK73EJW2C9FKC3BA
01G3NTE0HZCYGKFQQ0W33DDPMM
01G3NTE0HZGTKFFXVTM3Z7C8B7
01G3NTE0HZF1QYGFW6KWAC8Y10
01G3NTE0HZ4YG0QFDH3ATX33G8
END RequestId: 51874246-fcd2-41d5-911a-829a3bcf73c0
REPORT RequestId: 51874246-fcd2-41d5-911a-829a3bcf73c0	Duration: 1.56 ms	Billed Duration: 2 ms	Memory Size: 128 MB	Max Memory Used: 39 MB	Init Duration: 144.88 ms

Request ID
51874246-fcd2-41d5-911a-829a3bcf73c0

他のエラー

__version__ = version("ulid")

この修正では、エラーになる
image.png

Response
{
  "errorMessage": "Unable to import module 'lambda_function': ulid",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "d241a569-bb2a-41c0-af1f-528469f4a55d",
  "stackTrace": []
}

Function Logs
START RequestId: d241a569-bb2a-41c0-af1f-528469f4a55d Version: $LATEST
[ERROR] Runtime.ImportModuleError: Unable to import module 'lambda_function': ulid
4
2
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
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?