簡體   English   中英

如何發布到 localstack 內的 sns 主題

[英]How to publish to sns topic inside localstack

我寫了 lambda function,它通過 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、創建主題都運行沒有錯誤。

I've tried to add endpoint url for sns client in lambda function, but inside localstack it raises error "wrong url" (localstack enpoint for sns was ' http://localhost:4575 ').

如何通過 localstack 測試解決“botocore.exceptions.ClientError”?

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM