AWS Lambda跨区域向SNS发布消息



我有一个Lambda函数(east-us-1(,它需要在(east-us-1(&(eu-central-1(区域。这可能吗?

这是我的代码片段。有人能帮助我如何做到这一点吗?我惊讶地看到boto3客户端文档上写着";您只能将消息发布到同一亚马逊网络服务区域中的主题和端点";此处链接:

from __future__ import print_function
import json
import urllib
import boto3
print('Loading message function...')

def send_to_sns(message, context):
# This function receives JSON input with three fields: the ARN of an SNS topic,
# a string with the subject of the message, and a string with the body of the message.
# The message is then sent to the SNS topic.
#
# Example:
#   {
#       "topic": "arn:aws:sns:REGION:123456789012:MySNSTopic",
#       "subject": "This is the subject of the message.",
#       "message": "This is the body of the message."
#   }

sns = boto3.client('sns')
sns.publish(
TopicArn=message['topic'],
Subject=message['subject'],
Message=message['body']
)
return ('Sent a message to an Amazon SNS topic.')

当SNS在不同的区域时,我的代码抛出以下错误

{
"response": {
"stackTrace": [
[
"/var/task/lambda_function.py",
28,
"send_to_sns",
"Message=message['body']"
],
[
"/var/runtime/botocore/client.py",
357,
"_api_call",
"return self._make_api_call(operation_name, kwargs)"
],
[
"/var/runtime/botocore/client.py",
676,
"_make_api_call",
"raise error_class(parsed_response, operation_name)"
]
],
"errorType": "InvalidParameterException",
"errorMessage": "An error occurred (InvalidParameter) when calling the Publish operation: Invalid parameter: TopicArn"
}
}

由于区域冲突,您正面临此问题,您可以检查以下代码您当前所在地区的

my_session = boto3.session.Session()
my_region = my_session.region_name

为了解决你的问题,我假设你连接到了美国东部1号,并且你的sns位于美国东部2号区域

sns = boto3.client('sns',region_name='us-east-2')

相关内容

最新更新