如何在 localstack 中发布到 sns 主题
How to publish to sns topic inside localstack
我编写了 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 内部它引发错误 "wrong url"(sns 的 localstack enpoint 是“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)
我编写了 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 内部它引发错误 "wrong url"(sns 的 localstack enpoint 是“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)