1

我写了 lambda 函数,它通过 sns 向主题发布消息。我想用它进行集成测试,所以 localstack 似乎是一个很好的解决方案。

在 setup_class 方法上,我成功创建了 lambda,使用压缩代码和依赖项,还创建了 sns 主题,我想稍后在测试中发布消息。

在调用 lambda 阶段我收到错误:

E           REPORT RequestId: f9f3bac1-3b4b-4826-a5cb-362355087929 Duration: 1906 ms Billed Duration: 2000 ms Memory Size: 1536 MB Max Memory Used: 32 MB Traceback (most recent call last):
E             File "/opt/code/localstack/localstack/services/awslambda/lambda_api.py", line 338, in run_lambda
E               event, context=context, version=version, asynchronous=asynchronous)
E             File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 81, in execute
E               return do_execute()
E             File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 66, in do_execute
E               result, log_output = self._execute(func_arn, func_details, event, context, version)
E             File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 230, in _execute
E               result, log_output = self.run_lambda_executor(cmd, stdin, environment)
E             File "/opt/code/localstack/localstack/services/awslambda/lambda_executors.py", line 151, in run_lambda_executor
E               (return_code, log_output))
E           Exception: Lambda process returned error status code: 1. Output:
E           START RequestId: f9f3bac1-3b4b-4826-a5cb-362355087929 Version: $LATEST
E           An error occurred (InvalidClientTokenId) when calling the Publish operation: The security token included in the request is invalid.: ClientError
E           Traceback (most recent call last):
E             File "/var/task/functions/opt_in.py", line 32, in handler
E               MessageStructure='json'
E             File "/var/task/botocore/client.py", line 357, in _api_call
E               return self._make_api_call(operation_name, kwargs)
E             File "/var/task/botocore/client.py", line 661, in _make_api_call
E               raise error_class(parsed_response, operation_name)
E           botocore.exceptions.ClientError: An error occurred (InvalidClientTokenId) when calling the Publish operation: The security token included in the request is invalid.
E           
E           END RequestId: f9f3bac1-3b4b-4826-a5cb-362355087929
E           REPORT RequestId: f9f3bac1-3b4b-4826-a5cb-362355087929 Duration: 1906 ms Billed Duration: 2000 ms Memory Size: 1536 MB Max Memory Used: 32 MB

它说我可能对 IAM 或角色有一些问题,但是在设置阶段,任何作为创建 lambda、创建主题的操作都运行没有错误。

我试图在 lambda 函数中为 sns 客户端添加端点 url,但在 localstack 内它会引发错误“错误的 url”(sns 的 localstack 端点是' http://localhost:4575 ')。

如何通过 localstack 测试解决“botocore.exceptions.ClientError”?

4

1 回答 1

2

Localstack 需要正确的 enpoint_url 用于 sns 或其他资源,如果你想在里面执行它。

import os
sns_url = 'http://%s:4575' % os.environ['LOCALSTACK_HOSTNAME']
sns = boto3.client('sns', region_name='us-east-2', endpoint_url=sns_url)
于 2019-10-30T09:56:15.900 回答