【问题标题】:Mocking AWS lambda with Moto用 Moto 模拟 AWS lambda
【发布时间】:2021-09-09 11:22:35
【问题描述】:

我正在尝试模拟一个 AWS lambda 函数,下面是我的示例代码

def get_lambda_resp(arn: str, input: str) -> str:
    lambda_client = boto3.client("lambda")
    response = lambda_client.invoke(
        FunctionName=arn, LogType="None",
        Payload=json.dumps({"param": input}).encode("utf-8")
    )
    output = json.loads(response["Payload"].read().decode("utf-8"))
    return output["value"]

下面是我的测试用例

import io
import zipfile

import boto3
from moto import mock_lambda

@mock_lambda
def test():
    conn = boto3.client('lambda', 'us-east-1')

    def get_test_zip_file():
        pfunc = '''
        import json
        def lambda_handler(event, context):
            resp = {"value":"input_str"}
            return json.dumps(resp)
        '''
        zip_output = io.BytesIO()
        zip_file = zipfile.ZipFile(zip_output, 'w', zipfile.ZIP_DEFLATED)
        zip_file.writestr('lambda_function.py', pfunc)
        zip_file.close()
        zip_output.seek(0)
        return zip_output.read()

    conn.create_function(
        FunctionName='lambda-function-name',
        Runtime='python3.8',
        Role='test-iam-role',
        Handler='lambda_function.lambda_handler',
        Code={
            'ZipFile': get_test_zip_file(),
        },
        Description='test lambda function',
        Timeout=3,
        MemorySize=128,
        Publish=True
    )

    resp = get_auth("arn", "input_str")
    assert resp is not None

在运行测试用例时,我遇到了错误

E   ModuleNotFoundError: No module named 'docker'

我的 Docker 已经在运行,我还需要做什么来运行它?

【问题讨论】:

    标签: python amazon-web-services docker aws-lambda moto


    【解决方案1】:

    该消息指的是名为 docker 的 pip 模块。 假设您使用 Moto >=2.x,请确保正确安装它以获取所有必需的依赖项:

    pip install moto[awslambda,s3,service1,etc]

    或者,如果您使用许多服务,安装所有依赖项而不必列出所有服务:

    pip install moto[all]

    这将安装所有必需的 Pip 模块,包括 Docker。

    来源:https://github.com/spulec/moto/issues/3722

    【讨论】:

      猜你喜欢
      • 2022-11-06
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多