

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon SNS 通知調用 Lambda 函數
<a name="with-sns"></a>

您可以使用 Lambda 函數來處理 Amazon Simple Notification Service (Amazon SNS) 通知。Amazon SNS 支援 Lambda 函數作為傳送至主題之訊息的目標。您可以將自己的函數訂閱至相同帳戶或其他 AWS 帳戶中的主題。如需詳細演練，請參閱[教學課程： AWS Lambda 搭配 Amazon Simple Notification Service 使用](with-sns-example.md)。

Lambda 僅支援標準 SNS 主題的 SNS 觸發條件。不支援 FIFO 主題。

Lambda 透過將訊息排入佇列並處理重試，以非同步方式處理 SNS 訊息。如果 Amazon SNS 無法連接至 Lambda 或訊息遭到拒絕，Amazon SNS 會在數小時中以增加的間隔重試。如需詳細資訊，請參閱 Amazon SNS 常見問答集中的[可靠性](https://aws.amazon.com/sns/faqs/#Reliability)。

**警告**  
Lambda 非同步調用至少會處理每個事件一次，而且可能會重複處理記錄。為避免與重複事件相關的潛在問題，強烈建議您讓函數程式碼具有等冪性。如需詳細資訊，請參閱 AWS 知識中心中的[如何使 Lambda 函式具有冪等性](https://repost.aws/knowledge-center/lambda-function-idempotent)。

## Powertools for AWS Lambda 的冪等性公用程式
<a name="services-sns-powertools-idempotency"></a>

Powertools for AWS Lambda 的冪等性公用程式可讓 Lambda 函式具有冪等性。該工具組支援 Python、TypeScript、Java 與 .NET。如需詳細資訊，請參閱 *Powertools for AWS Lambda (Python) documentation* 中的 [Idempotency utility](https://docs.powertools.aws.dev/lambda/python/latest/utilities/idempotency/)、*Powertools for AWS Lambda (TypeScript) documentation* 中的 [Idempotency Utility](https://docs.aws.amazon.com/powertools/typescript/2.1.1/utilities/idempotency/)、*Powertools for AWS Lambda (Java) documentation* 中的 [Idempotency Utility](https://docs.powertools.aws.dev/lambda/java/latest/utilities/idempotency/)，以及 *Powertools for AWS Lambda (.NET) documentation* 中的 [Idempotency Utility](https://docs.powertools.aws.dev/lambda/dotnet/utilities/idempotency/)。

**Topics**
+ [Powertools for AWS Lambda 的冪等性公用程式](#services-sns-powertools-idempotency)
+ [使用主控台為 Lambda 函數新增 Amazon SNS 主題觸發條件](#sns-trigger-console)
+ [手動新增 Lambda 函數的 Amazon SNS 主題觸發條件](#sns-trigger-manual)
+ [範例 SNS 事件形狀](#sns-sample-event)
+ [教學課程： AWS Lambda 搭配 Amazon Simple Notification Service 使用](with-sns-example.md)

## 使用主控台為 Lambda 函數新增 Amazon SNS 主題觸發條件
<a name="sns-trigger-console"></a>

若要新增 SNS 主題做為 Lambda 函數的觸發條件，最簡單的方法便是使用 Lambda 主控台。當您透過主控台新增觸發條件時，Lambda 會自動設定必要的許可和訂閱，以開始從 SNS 主題接收事件。

**若要新增 SNS 主題做為 Lambda 函數的觸發條件 (主控台)**

1. 開啟 Lambda 主控台中的 [函數頁面](https://console.aws.amazon.com/lambda/home#/functions)。

1. 選擇要為其新增觸發條件的函數名稱。

1. 選擇**組態**，然後**觸發條件**。

1. 選擇 **Add trigger (新增觸發條件)**。

1. 在**觸發條件組態**底下的下拉式清單中，選擇 **SNS**。

1. 針對 **SNS 主題**，選擇要訂閱的 SNS 主題。

## 手動新增 Lambda 函數的 Amazon SNS 主題觸發條件
<a name="sns-trigger-manual"></a>

若要手動設定 Lambda 函數的 SNS 觸發條件，您需要完成以下步驟：
+ 為函數定義資源型政策，以允許 SNS 調用它。
+ 讓 Lambda 函數訂閱 Amazon SNS 主題。
**注意**  
如果 SNS 主題和 Lambda 函數位於不同的 AWS 帳戶中，您還需要授予函數額外許可，以允許跨帳戶訂閱 SNS 主題。如需詳細資訊，請參閱[跨帳戶授予 Amazon SNS 訂閱的許可](with-sns-example.md#with-sns-subscription-grant-permission)。

您可以使用 AWS Command Line Interface (AWS CLI) 來完成這兩個步驟。首先，使用以下 AWS CLI 命令，為 Lambda 函數定義資源型政策以允許 SNS 調用。請務必以您的 Lambda 函數名稱取代 `--function-name`，並以您的 SNS 主題 ARN 取代 `--source-arn` 的值。

```
aws lambda add-permission --function-name {{example-function}} \
    --source-arn {{arn:aws:sns:us-east-1:123456789012:sns-topic-for-lambda}} \
    --statement-id function-with-sns --action "lambda:InvokeFunction" \
    --principal sns.amazonaws.com
```

若要讓函數訂閱 SNS 主題，請使用以下 AWS CLI 命令。以您的 SNS 主題 ARN 取代 `--topic-arn` 的值，並以您的 Lambda 函數 ARN 取代 `--notification-endpoint` 的值。

```
aws sns subscribe --protocol lambda \
    --region us-east-1 \
    --topic-arn {{arn:aws:sns:us-east-1:123456789012:sns-topic-for-lambda}} \
    --notification-endpoint {{arn:aws:lambda:us-east-1:123456789012:function:example-function}}
```

## 範例 SNS 事件形狀
<a name="sns-sample-event"></a>

Amazon SNS 使用包含訊息和中繼資料的事件，以[非同步方](invocation-async.md)式叫用您的函數。

**Example Amazon SNS 訊息事件**  

```
{
  "Records": [
    {
      "EventVersion": "1.0",
      "EventSubscriptionArn": "arn:aws:sns:us-east-1:123456789012:sns-lambda:21be56ed-a058-49f5-8c98-aedd2564c486",
      "EventSource": "aws:sns",
      "Sns": {
        "SignatureVersion": "1",
        "Timestamp": "2019-01-02T12:45:07.000Z",
        "Signature": "tcc6faL2yUC6dgZdmrwh1Y4cGa/ebXEkAi6RibDsvpi+tE/1+82j...65r==",
        "SigningCertURL": "https://sns.us-east-1.amazonaws.com/SimpleNotificationService-ac565b8b1a6c5d002d285f9598aa1d9b.pem",
        "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
        "Message": "Hello from SNS!",
        "MessageAttributes": {
          "Test": {
            "Type": "String",
            "Value": "TestString"
          },
          "TestBinary": {
            "Type": "Binary",
            "Value": "TestBinary"
          }
        },
        "Type": "Notification",
        "UnsubscribeUrl": "https://sns.us-east-1.amazonaws.com/?Action=Unsubscribe&amp;SubscriptionArn=arn:aws:sns:us-east-1:123456789012:test-lambda:21be56ed-a058-49f5-8c98-aedd2564c486",
        "TopicArn":"arn:aws:sns:us-east-1:123456789012:sns-lambda",
        "Subject": "TestInvoke"
      }
    }
  ]
}
```