【问题标题】:How to publish to sns topic inside localstack如何发布到 localstack 内的 sns 主题
【发布时间】:2020-02-13 00:54:10
【问题描述】:

我写了 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 或角色有一些问题,但在设置阶段,任何操作如 create lambda、create topic 都运行没有错误。

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

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

【问题讨论】:

    标签: python amazon-web-services aws-lambda amazon-sns localstack


    【解决方案1】:

    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)
    

    【讨论】:

    • 从 localstack 0.11.0 开始,所有服务的端口都是相同的,4566。这个例子也可以完美地与 4566 配合使用
    猜你喜欢
    • 2018-03-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-14
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多