【问题标题】:pytest localstack test error You must specify a regionpytest localstack test error 你必须指定一个区域
【发布时间】:2020-02-23 22:45:39
【问题描述】:

我完成了一个 python 测试,它启动了一个 localstack 容器,并在 localstack 容器内提供了一个 S3 存储桶和一个 lambda。

测试看起来像这样:

def my_test(self, setup_terraform_infra):
    s3 = boto3.resource('s3')
    buckets = list(s3.buckets.all())
    assert len(buckets) == 1

    lambda_client = boto3.client('lambda', region_name='us-east-1')
    payload = json.dumps(self.load_file(TEST_INPUT_SUCCESS_MESSAGE))

    list_function_resp = lambda_client.list_functions(
        MasterRegion='us-east-1',
        Marker='',
        MaxItems=123
    )

    assert len(list_function_resp['Functions']) == 1

    response = lambda_client.invoke(
        FunctionName='function',
        InvocationType='RequestResponse',
        Payload=payload,
    )

在您点击调用函数之前,上述测试中一切正常:

response = lambda_client.invoke(
    FunctionName='function',
    InvocationType='RequestResponse',
    Payload=payload,
)

这会导致以下错误:

botocore.exceptions.ClientError: An error occurred (InternalFailure) when calling the Invoke operation (reached max retries: 4): Error executing Lambda function arn:aws:lambda:us-east-1:000000000000:function:function: You must specify a region.
botocore.exceptions.NoRegionError: You must specify a region.

如果我跳到测试创建的 docker 映像上,我可以重现此错误:

awslocal lambda invoke --region eu-west-1 --function-name "arn:aws:lambda:us-east-1:000000000000:function:function" --payload "{}" reponse.json

错误:

>>> botocore.exceptions.NoRegionError: You must specify a region.

我已尝试在 docker 映像中同时设置 AWS_DEFAULT_REGION 和 DEFAULT_REGION。但问题仍然存在。

我的想法已经用完了

【问题讨论】:

    标签: python aws-lambda pytest terraform localstack


    【解决方案1】:

    我之前遇到过完全相同的问题,不幸的是错误消息具有误导性,但我的问题原来是由于对 Lambda 使用了错误的InvocationType

    设置正确的InvocationType(在我的情况下应该设置为Event而不是RequestResponse)后,它可以正常工作。

    希望这也能解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-02
      相关资源
      最近更新 更多