

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将 Lambda 函数，以接收来自不同 AWS 区域中的 S3 存储桶的事件通知
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions"></a>

*Suresh Konathala，Amazon Web Services*

*Andrew Preston 和 Arindom Sarkar，无*

## Summary
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions-summary"></a>

[Amazon Simple Storage Service (Amazon S3) 事件通知](https://docs.aws.amazon.com/AmazonS3/latest/userguide/NotificationHowTo.html)会针对您的 S3 存储桶中的某些事件（例如，对象创建事件、对象移除事件或恢复对象事件）发布通知。您可使用 AWS Lambda 函数根据应用程序的要求处理这些通知。但是，Lambda 函数无法直接订阅来自不同 AWS 区域托管的 S3 存储桶的通知。

这种模式的方法通过为每个区域使用 Amazon Simple Notiﬁcation Service (Amazon SNS)主题，部署[扇出场景](https://docs.aws.amazon.com/sns/latest/dg/sns-common-scenarios.html)来处理来自跨区域 S3 存储桶的 Amazon S3 通知。这些区域 SNS 主题将 Amazon S3 事件通知发送到也包含您的 Lambda 函数的中央区域中的 Amazon Simple Queue Service (Amazon SQS) 队列。Lambda 函数订阅此 SQS 队列，并根据您组织的要求处理事件通知。

## 先决条件和限制
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions-prereqs"></a>

**先决条件**
+ 一个活跃的 AWS 账户。
+ 多个区域中的现有 S3 存储桶，包括用于托管 Amazon SQS 队列和 Lambda 函数的中央区域。
+ 已安装和配置 AWS 命令行界面（AWS CLI）。有关这方面的更多信息，请参阅 AWS CLI 文档中的[安装、更新和卸载 AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)。 
+ 熟悉 Amazon SNS 的扇出场景。有关这方面的更多信息，请参阅 Amazon SNS 文档中的[常见 Amazon SNS 场景](https://docs.aws.amazon.com/sns/latest/dg/sns-common-scenarios.html)。

## 架构
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions-architecture"></a>

下图显示了此模式的方法的架构。 

![\[工作流使用 Amazon SNS、S3 和 SQS 来处理来自跨区域 S3 存储桶的事件通知。\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/images/pattern-img/cf6c1804-8c41-46f1-9f17-ff361708c595/images/760cf4c0-0cb3-48d1-92ae-1cf0fa8ae076.png)


下图显示了如下工作流：

1. Amazon S3 向同一区域的 SNS 主题发送有关 S3 存储桶（例如，已创建对象、删除对象或恢复对象）的事件通知。

1. SNS 主题将事件发布到中央区域的 SQS 队列。

1. SQS 队列配置为 Lambda 函数的事件源，并缓冲 Lambda 函数的事件消息。 

1. Lambda 函数轮询 SQS 队列中的消息，并根据您的应用程序的要求处理 Amazon S3 事件通知。

 

**技术堆栈**
+ Lambda
+ Amazon SNS
+ Amazon SQS
+ Amazon S3

## 工具
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions-tools"></a>
+ [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) – AWS 命令行界面（AWS CLI）是一种开源工具，用于通过命令行 Shell 中的命令与 Amazon Web Services 交互。仅需最少的配置，即可使用 AWS CLI 开始运行命令，以便从终端程序中的命令提示符实现与基于浏览器的 AWS 管理控制台所提供的功能等同的功能。
+ [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.html) — AWS CloudFormation 可帮助您建模和设置 AWS 资源，快速一致地配置这些资源，并在资源的整个生命周期中对其进行管理。您可以使用模板来描述资源及其依赖关系，然后将它们作为堆栈一起启动和配置，而不必单独管理资源。您可以跨多个 AWS 账户和 AWS 区域管理和预调配堆栈。
+ [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) – AWS Lambda 是一项计算服务，支持无需预置或管理服务器即可运行代码。只有在需要时 Lambda 才运行您的代码，并且能自动扩缩，从每天几个请求扩展到每秒数千个请求。您只需为消耗的计算时间付费 - 代码未运行时不产生费用。
+ [Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/welcome.html) – Amazon Simple Notiﬁcation Service (Amazon SNS) 可协调和管理发布者和客户端之间消息的传送或发送，包括 Web 服务器和电子邮件地址。订阅用户接收所有发布至他们所订阅主题的消息，并且一个主题的所有订阅用户收到的消息都相同。
+ [Amazon SQS](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html) – Amazon Simple Queue Service (Amazon SQS) 提供了一个安全、持久且可用的托管队列，以允许您集成和分离分布式软件系统与组件。Amazon SQS 支持标准队列和 FIFO 队列。

## 操作说明
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions-epics"></a>

### 在您的中央区域创建 SQS 队列和 Lambda 函数
<a name="create-the-sqs-queue-and-lambda-function-in-your-central-region"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
| 使用 Lambda 触发器创建 SQS 队列。 | 登录 AWS 管理控制台，按照 AWS Lambda 文档中的[将 Lambda 与 Amazon SQS 结合使用](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs-example.html)教程中的说明在您的中央区域创建以下资源：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions.html)确保将 SQS 队列配置为您的 Lambda 函数的事件源。 | AWS DevOps，云架构师 | 

### 创建 SNS 主题，并为每个所需区域中的 S3 存储桶设置事件通知
<a name="create-an-sns-topic-and-set-up-event-notifications-for-the-s3-buckets-in-each-required-region"></a>


| Task | 说明 | 所需技能 | 
| --- | --- | --- | 
|  创建 SNS 主题以接收 Amazon S3 事件通知。 | 在您想要接收 Amazon S3 事件通知的区域中创建 SNS 主题。有关这方面的更多信息，请参阅 Amazon SNS 文档中的[创建 SNS 主题](https://docs.aws.amazon.com/sns/latest/dg/sns-create-topic.html)。 确保记录您的 SNS 主题的 Amazon 资源名称（ARN）。  | AWS DevOps，云架构师 | 
| 为中央 SQS 队列订阅 SNS 主题。 | 为您的中央区域托管的 SQS 队列订阅您的 SNS 主题。有关这方面的更多信息，请参阅 Amazon SNS 文档中的[订阅 SNS 主题](https://docs.aws.amazon.com/sns/latest/dg/sns-create-subscribe-endpoint-to-topic.html)。 | AWS DevOps，云架构师 | 
| 更新 SNS 主题访问策略。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_cn/prescriptive-guidance/latest/patterns/subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions.html)<pre>{<br />  "Version": "2012-10-17",		 	 	 <br />  "Statement": [<br />    {<br />      "Sid": "0",<br />      "Effect": "Allow",<br />      "Principal": {<br />        "Service": "s3.amazonaws.com"<br />      },<br />      "Action": "sns:Publish",<br />      "Resource": "arn:aws:sns:us-west-2::s3Events-SNSTopic-us-west-2"<br />    }<br />  ]<br />}</pre> | AWS DevOps，云架构师 | 
| 为区域中的每个 S3 存储桶设置通知。 | 为区域中的每个 S3 存储桶设置事件通知。有关这方面的更多信息，请参阅 Amazon S3 文档中的[使用 Amazon S3 控制台启用和配置事件通知](https://docs.aws.amazon.com/AmazonS3/latest/userguide/enable-event-notifications.html)。在**目标**部分中，选择 **SNS 主题**，并指定您之前创建的 SNS 主题的 ARN。 | AWS DevOps，云架构师 | 
| 在所需的各个区域中，重复此操作。 | 针对您希望从中接收 Amazon S3 事件通知的每个区域（包括您的中央区域），重复此操作说明中的任务。 | AWS DevOps，云架构师 | 

## 相关资源
<a name="subscribe-a-lambda-function-to-event-notifications-from-s3-buckets-in-different-aws-regions-resources"></a>
+ [配置访问策略](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-configure-add-permissions.html)（Amazon SQS 文档）
+ [将 SQS 队列配置为事件源](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html#events-sqs-eventsource)（AWS Lambda 文档）
+ [配置 SQS 队列以启动 Lambda 函数](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-configure-lambda-function-trigger.html)（Amazon SQS 文档）
+ [AWS::Lambda::Function 资源](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-lambda-function.html)（AWS CloudFormation 文档）