3
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?

More than 1 year has passed since last update.

AWS Lambdaから別Lambdaを呼び出す際にPickle形式でデータをやりとりする

Posted at

Lambdaから別Lambdaを実行するのはアンチパターンにも載っている方法ですが、やってみたので書いておきます。

呼び出し元

FooのdataclassをいくつかリストにしてPickle形式にシリアライズして送信先のLambdaに送信しています。
返り値もPickleで返ってくるので読み込んでプリントします。

import json
import pickle
from typing import List
from dataclasses import dataclass
from datetime import datetime
from base64 import b85decode, b85encode

import boto3


@dataclass
class Foo:
    i: int
    d: datetime


def lambda_handler(event, context):

    foo_list: List[Foo] = [Foo(i=i, d=datetime.utcnow()) for i in range(10)]
    send_data_pickle = pickle.dumps(foo_list)
    send_data = b85encode(send_data_pickle).decode("utf-8")

    lambda_cli = boto3.client("lambda")

    response = lambda_cli.invoke(
        FunctionName="lambda_t",
        InvocationType="RequestResponse",
        Payload=json.dumps({"data": send_data}),
    )

    response_payload = response["Payload"].read()
    response_data_base85 = json.loads(response_payload)["data"]
    response_data = b85decode(response_data_base85)
    foo_list_with_index = pickle.loads(response_data)
    [print(f) for f in foo_list_with_index]

呼び出し先

Fooのdataclassで飛んできたリストにインデックスを付けて返すだけです。

import pickle
from dataclasses import dataclass
from datetime import datetime
from base64 import b85decode, b85encode


@dataclass
class Foo:
    i: int
    d: datetime


def lambda_handler(event, context):
    receive_data_base85 = event["data"]
    receive_data = b85decode(receive_data_base85)
    foo_list = pickle.loads(receive_data)
    foo_list_with_index = [(i, foo) for i, foo in enumerate(foo_list)]
    response_data = pickle.dumps(foo_list_with_index)
    return {"data": b85encode(response_data).decode("utf-8")}

実行結果

送信元Lambdaを実行すると、送信元Lambdaで作成したFooクラスのリストに、送信先でインデックスが付けられたものが表示されます。

(0, Foo(i=0, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734783)))
(1, Foo(i=1, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734787)))
(2, Foo(i=2, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734789)))
(3, Foo(i=3, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734789)))
(4, Foo(i=4, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734790)))
(5, Foo(i=5, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734792)))
(6, Foo(i=6, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734792)))
(7, Foo(i=7, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734793)))
(8, Foo(i=8, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734794)))
(9, Foo(i=9, d=datetime.datetime(2022, 1, 29, 7, 41, 52, 734795)))
3
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
3
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?