

# Deploying and testing the Amazon SNS event fork pipelines sample application
<a name="sns-deploy-test-fork-pipelines-sample-application"></a>

To accelerate the development of your event-driven applications, you can subscribe event-handling pipelines—powered by AWS Event Fork Pipelines—to Amazon SNS topics. AWS Event Fork Pipelines is a suite of open-source [nested applications](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-nested-applications.html), based on the [AWS Serverless Application Model](https://aws.amazon.com/serverless/sam/) (AWS SAM), which you can deploy directly from the [AWS Event Fork Pipelines suite](https://serverlessrepo.aws.amazon.com/applications?query=aws-event-fork-pipelines) (choose **Show apps that create custom IAM roles or resource policies**) into your AWS account. For more information, see [How AWS Event Fork Pipelines works](sns-fork-pipeline-as-subscriber.md#how-sns-fork-works).

This page shows how you can use the AWS Management Console to deploy and test the AWS Event Fork Pipelines sample application.

**Important**  
To avoid incurring unwanted costs after you finish deploying the AWS Event Fork Pipelines sample application, delete its CloudFormation stack. For more information, see [Deleting a Stack on the CloudFormation Console](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-console-delete-stack.html) in the *AWS CloudFormation User Guide*.

# AWS Event Fork Pipelines use case example
<a name="example-sns-fork-use-case"></a>

The following scenario describes an event-driven, serverless e-commerce application that uses AWS Event Fork Pipelines. You can use this [example e-commerce application](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:077246666028:applications~fork-example-ecommerce-checkout-api) in the AWS Serverless Application Repository and then deploy it in your AWS account using the AWS Lambda console, where you can test it and examine its source code in GitHub.

![\[The architecture of a serverless e-commerce application that integrates AWS services. It depicts the flow from ecommerce users placing orders via an API Gateway to different processing pipelines including order storage, search analytics, and replay, showcasing how events are managed and analyzed through Amazon SNS, Lambda, Amazon SQS, DynamoDB, and Kibana.\]](http://docs.aws.amazon.com/sns/latest/dg/images/sns-fork-example-use-case.png)


This e-commerce application takes orders from buyers through a RESTful API hosted by API Gateway and backed by the AWS Lambda function `CheckoutApiBackendFunction`. This function publishes all received orders to an Amazon SNS topic named `CheckoutEventsTopic` which, in turn, fans out the orders to four different pipelines.

The first pipeline is the regular checkout-processing pipeline designed and implemented by the owner of the e-commerce application. This pipeline has the Amazon SQS queue `CheckoutQueue` that buffers all received orders, an AWS Lambda function named `CheckoutFunction` that polls the queue to process these orders, and the DynamoDB table `CheckoutTable` that securely saves all placed orders.

## Applying AWS Event Fork Pipelines
<a name="applying-sns-fork-pipelines"></a>

The components of the e-commerce application handle the core business logic. However, the e-commerce application owner also needs to address the following:
+ **Compliance**—secure, compressed backups encrypted at rest and sanitization of sensitive information
+ **Resiliency**—replay of most recent orders in case of the disruption of the fulfillment process
+ **Searchability**—running analytics and generating metrics on placed orders

Instead of implementing this event processing logic, the application owner can subscribe AWS Event Fork Pipelines to the `CheckoutEventsTopic` Amazon SNS topic
+ [The event storage and backup pipeline](sns-fork-pipeline-as-subscriber.md#sns-fork-event-storage-and-backup-pipeline) is configured to transform data to remove credit card details, buffer data for 60 seconds, compress it using GZIP, and encrypt it using the default customer managed key for Amazon S3. This key is managed by AWS and powered by the AWS Key Management Service (AWS KMS).

  For more information, see [Choose Amazon S3 For Your Destination](https://docs.aws.amazon.com/firehose/latest/dev/create-destination.html#create-destination-s3), [Amazon Data Firehose Data Transformation](https://docs.aws.amazon.com/firehose/latest/dev/data-transformation.html), and [Configure Settings](https://docs.aws.amazon.com/firehose/latest/dev/create-configure.html) in the *Amazon Data Firehose Developer Guide*.
+ [The event search and analytics pipeline](sns-fork-pipeline-as-subscriber.md#sns-fork-event-search-and-analytics-pipeline) is configured with an index retry duration of 30 seconds, a bucket for storing orders that fail to be indexed in the search domain, and a filter policy to restrict the set of indexed orders.

  For more information, see [Choose OpenSearch Service for your Destination](https://docs.aws.amazon.com/firehose/latest/dev/create-destination.html#create-destination-elasticsearch) in the *Amazon Data Firehose Developer Guide*.
+ [The event replay pipeline](sns-fork-pipeline-as-subscriber.md#sns-fork-event-replay-pipeline) is configured with the Amazon SQS queue part of the regular order-processing pipeline designed and implemented by the e-commerce application owner.

  For more information, see [Queue Name and URL](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-general-identifiers.html#queue-name-url) in the *Amazon Simple Queue Service Developer Guide*.

The following JSON filter policy is set in the configuration for the Event Search and Analytics Pipeline. It matches only incoming orders in which the total amount is \$1100 or higher. For more information, see [Amazon SNS message filtering](sns-message-filtering.md).

```
{				
   "amount": [{ "numeric": [ ">=", 100 ] }]
}
```

Using the AWS Event Fork Pipelines pattern, the e-commerce application owner can avoid the development overhead that often follows coding undifferentiating logic for event handling. Instead, she can deploy AWS Event Fork Pipelines directly from the AWS Serverless Application Repository into her AWS account.

# Step 1: Deploying the sample Amazon SNS application
<a name="deploy-sample-application"></a>

1. Sign in to the [AWS Lambda console](https://console.aws.amazon.com/lambda/).

1. On the navigation panel, choose **Functions** and then choose **Create function**.

1. On the **Create function** page, do the following:

   1. Choose **Browse serverless app repository**, **Public applications**, **Show apps that create custom IAM roles or resource policies**.

   1. Search for `fork-example-ecommerce-checkout-api` and then choose the application.

1. On the **fork-example-ecommerce-checkout-api** page, do the following:

   1. In the **Application settings** section, enter an **Application name** (for example, `fork-example-ecommerce-my-app`).
**Note**  
To find your resources easily later, keep the prefix `fork-example-ecommerce`.
For each deployment, the application name must be unique. If you reuse an application name, the deployment will update only the previously deployed CloudFormation stack (rather than create a new one).

   1. (Optional) Enter one of the following **LogLevel** settings for the execution of your application's Lambda function:
      + `DEBUG`
      + `ERROR`
      + `INFO` (default)
      + `WARNING`

1. Choose **I acknowledge that this app creates custom IAM roles, resource policies and deploys nested applications.** and then, at the bottom of the page, choose **Deploy**.

On the **Deployment status for fork-example-ecommerce-*my-app*** page, Lambda displays the **Your application is being deployed** status.

In the **Resources** section, CloudFormation begins to create the stack and displays the **CREATE\$1IN\$1PROGRESS** status for each resource. When the process is complete, CloudFormation displays the **CREATE\$1COMPLETE** status.

**Note**  
It might take 20-30 minutes for all resources to be deployed.

When the deployment is complete, Lambda displays the **Your application has been deployed** status.

# Step 2: Executing the SNS-linked sample application
<a name="execute-sample-application"></a>

1. In the AWS Lambda console, on the navigation panel, choose **Applications**.

1. On the **Applications** page, in the search field, search for `serverlessrepo-fork-example-ecommerce-my-app` and then choose the application.

1. In the **Resources** section, do the following:

   1. To find the resource whose type is **ApiGateway RestApi**, sort the resources by **Type**, for example `ServerlessRestApi`, and then expand the resource.

   1. Two nested resources are displayed, of types **ApiGateway Deployment** and **ApiGateway Stage**.

   1. Copy the link **Prod API endpoint** and append `/checkout` to it, for example: 

      ```
      https://abcdefghij.execute-api.us-east-2.amazonaws.com/Prod/checkout
      ```

1. Copy the following JSON to a file named `test_event.json`.

   ```
   {
      "id": 15311,
      "date": "2019-03-25T23:41:11-08:00",
      "status": "confirmed",
      "customer": {
         "id": 65144,		
   	 "quantity": 2,
         "price": 25.00,
         "subtotal": 50.00
      }]
   }
   ```

1. To send an HTTPS request to your API endpoint, pass the sample event payload as input by executing a `curl` command, for example:

   ```
   curl -d "$(cat test_event.json)" https://abcdefghij.execute-api.us-east-2.amazonaws.com/Prod/checkout
   ```

   The API returns the following empty response, indicating a successful execution:

   ```
   { }
   ```

# Step 3: Verifying Amazon SNS application and pipeline performance
<a name="verify-sample-application-pipelines"></a>

## Step 1: Verifying the execution of the sample checkout pipeline
<a name="verify-execution-checkout-pipeline"></a>

1. Sign in to the [Amazon DynamoDB console](https://console.aws.amazon.com/dynamodb/).

1. On the navigation panel, choose **Tables**.

1. Search for `serverlessrepo-fork-example` and choose `CheckoutTable`.

1. On the table details page, choose **Items** and then choose the created item.

   The stored attributes are displayed.

## Step 2: Verifying the execution of the event storage and backup pipeline
<a name="verify-execution-event-storage-backup-pipeline"></a>

1. Sign in to the [Amazon S3 console](https://console.aws.amazon.com/s3/).

1. On the navigation panel, choose **Buckets**.

1. Search for `serverlessrepo-fork-example` and then choose `CheckoutBucket`.

1. Navigate the directory hierarchy until you find a file with the extension `.gz`.

1. To download the file, choose **Actions**, **Open**.

1. The pipeline is configured with a Lambda function that sanitizes credit card information for compliance reasons.

   To verify that the stored JSON payload doesn't contain any credit card information, decompress the file.

## Step 3: Verifying the execution of the event search and analytics pipeline
<a name="verify-execution-event-search-analytics-pipeline"></a>

1. Sign in to the [OpenSearch Service console](https://console.aws.amazon.com/aos/).

1. On the navigation panel, under **My domains**, choose the domain prefixed with `serverl-analyt`.

1. The pipeline is configured with an Amazon SNS subscription filter policy that sets a numeric matching condition.

   To verify that the event is indexed because it refers to an order whose value is higher than USD \$1100, on the **serverl-analyt-*abcdefgh1ijk*** page, choose **Indices**, **checkout\$1events**.

## Step 4: Verifying the execution of the event replay pipeline
<a name="verify-execution-event-replay-pipeline"></a>

1. Sign in to the [Amazon SQS console](https://console.aws.amazon.com/sqs/).

1. In the list of queues, search for `serverlessrepo-fork-example` and choose `ReplayQueue`.

1. Choose **Send and receive messages**.

1. In the **Send and receive messages in fork-example-ecommerce-*my-app*...ReplayP-ReplayQueue-*123ABCD4E5F6*** dialog box, choose **Poll for messages**. 

1. To verify that the event is enqueued, choose **More Details** next to the message that appears in the queue.

# Step 4: Simulating an issue and replay events for recovery
<a name="simulate-issue-replay-events-for-recovery"></a>

## Step 1: Enable the simulated issue and send a second API request
<a name="enable-simulated-issue-send-second-api-request"></a>

1. Sign in to the [AWS Lambda console](https://console.aws.amazon.com/lambda/).

1. On the navigation panel, choose **Functions**.

1. Search for `serverlessrepo-fork-example` and choose `CheckoutFunction`.

1. On the **fork-example-ecommerce-*my-app*-CheckoutFunction-*ABCDEF*...** page, in the **Environment variables** section, set the **BUG\$1ENABLED** variable to **true** and then choose **Save**.

1. Copy the following JSON to a file named `test_event_2.json`.

   ```
   {
   	   "id": 9917,
   	   "date": "2019-03-26T21:11:10-08:00",
   	   "status": "confirmed",
   	   "customer": {
   	      "id": 56999,
   "quantity": 1,
   	      "price": 75.00,
   	      "subtotal": 75.00
   	   }]
   	}
   ```

1. To send an HTTPS request to your API endpoint, pass the sample event payload as input by executing a `curl` command, for example:

   ```
   curl -d "$(cat test_event_2.json)" https://abcdefghij.execute-api.us-east-2.amazonaws.com/Prod/checkout
   ```

   The API returns the following empty response, indicating a successful execution:

   ```
   { }
   ```

## Step 2: Verify simulated data corruption
<a name="verify-simulated-data-corruption"></a>

1. Sign in to the [Amazon DynamoDB console](https://console.aws.amazon.com/dynamodb/).

1. On the navigation panel, choose **Tables**.

1. Search for `serverlessrepo-fork-example` and choose `CheckoutTable`.

1. On the table details page, choose **Items** and then choose the created item.

   The stored attributes are displayed, some marked as **CORRUPTED\$1**

## Step 3: Disable the simulated issue
<a name="disable-simulated-issue"></a>

1. Sign in to the [AWS Lambda console](https://console.aws.amazon.com/lambda/).

1. On the navigation panel, choose **Functions**.

1. Search for `serverlessrepo-fork-example` and choose `CheckoutFunction`.

1. On the **fork-example-ecommerce-*my-app*-CheckoutFunction-*ABCDEF*...** page, in the **Environment variables** section, set the **BUG\$1ENABLED** variable to **false** and then choose **Save**.

## Step 4: Enable replay to recover from the issue
<a name="enable-replay-recover-from-simulated-issue"></a>

1. In the AWS Lambda console, on the navigation panel, choose **Functions**.

1. Search for `serverlessrepo-fork-example` and choose `ReplayFunction`.

1. Expand the **Designer** section, choose the **SQS** tile and then, in the **SQS** section, choose **Enabled**.
**Note**  
It takes approximately 1 minute for the Amazon SQS event source trigger to become enabled.

1. Choose **Save**.

1. To view the recovered attributes, return to the Amazon DynamoDB console.

1. To disable replay, return to the AWS Lambda console and disable the Amazon SQS event source trigger for `ReplayFunction`.