Create Customer Notification Service Subscription

In this step, we will create an Amazon SQS queue for the Customer Notification Service and add a subscription to the Amazon SNS topic we created before:

1. Create a new Amazon SQS queue

In your Amazon SQS console, select Create New Queue in top left corner or click Get Started Now in the center of the page, if it’s your first queue in this region.

Detailed description

Enter CustomerNotificationServiceQueue as Queue Name, make sure Standard Queue is highlighted and click Quick-Create Queue.

Detailed description

2. Create a new subscription

After selecting the queue CustomerNotificationServiceQueue, click the Queue Actions drop down in the top left corner and chose Subscribe Queue to SNS Topic.

Detailed description

In the pop up window, select the RideCompletionTopic and click Subscribe. Click Ok in the confirmation dialog.

Detailed description

You may wondering why we don’t create the subscription from the Amazon SNS console as shown below. If we create a subscription from Amazon SNS to Amazon SQS, Amazon SNS will send a confirmation message first to Amazon SQS. As long as this subscription is not confirmed, Amazon SNS will not start sending messages to Amazon SQS. By initiating the subscription from the subscriber side, this is not necessary. Tip

3. Validate the subscription confirmation

Browse to your Amazon SNS console to list your existing topics. Select the RideCompletionTopic and verify, the subscription has the status Confirmed.

Detailed description

4. Grant permissions to our function to access the Amazon SQS queue

In your Amazon IAM console, select Roles in the left navigation. Use the filter text box to find the role with the name wild-rydes-async-msg-2-CustomerNotificationService-… (assuming your have chosen wild-rydes-async-msg-2 as your stack name).

Detailed description

Click on the role name and click Add inline policy to attache another one.

Detailed description

Select the JSON tab and passed the following policy statement into it, after you have substitute «…» with the correct values. It will add the permission to your Lambda function to access the Amazon SQS queue:

policy

Make sure you provide the AWS ACCOUNT ID in the form of XXXXXXXXXXXX and not XXXX-XXXX-XXXX!

Click Review policy and enter the Name CustomerNotificationServiceRolePolicy0. Click Create policy. To validate this step, select on the role again and your should see 3 policies attached to your role, including the one you just have created:

Detailed description

5. Add the Amazon SQS queue as event source for your Customer Notification Service AWS Lambda function

Open your AWS Lambda console and select Functions in the left navigation. Click on the function with the name wild-rydes-async-msg-2-CustomerNotification… (assuming your have chosen wild-rydes-async-msg-2 as your stack name). Click on the + Add Trigger button on the left side of the page:

Detailed description

On the following page, select SQS as the event source for this AWS Lambda function. For the SQS queue, select the CustomerNotificationServiceQueue and set the batch size to 1. Don’t forget to enable the trigger, before you click the Add button in the lower right corner.

Detailed description

After some seconds, the trigger will be enabled and and you are ready to go (you may have to refresh the site a few times).

Detailed description

1. Update the AWS SAM template

In your Cloud9 IDE for this workshop, open the SAM template file ‘wild-rydes-async-messaging/lab-2/template.yaml’. In the Resources section, add the definition for an Amazon SQS queue with the name CustomerNotificationServiceQueue, the CustomerNotificationService will use to consume messages from. You can find the AWS CloudFormation documentation to do so here.

Cheat Sheet

The next step, before we can define the subscription, is granting our Amazon SNS topic the permissions to publish messages into this Amazon SQS queue. You can find the AWS CloudFormation documentation to do so here.

Cheat Sheet

Now we are ready to create the Amazon SNS subscription for the CustomerNotificationService. You can find the AWS CloudFormation documentation to do so here.

Cheat Sheet

The next step is to attache an AWS IAM policy tou our CustomerNotificationService AWS Lambda function, which grants permission to access our previously created Amazon SQS queue, to consume the messages. You can find the AWS SAM documentation to do so here and here.

Cheat Sheet

Last but not least, we have to declare the CustomerNotificationServiceQueue as event source for our CustomerNotificationService. You can find the AWS SAM documentation to do so here.

Cheat Sheet
Detailed description

2. Deploy the updated AWS SAM template

Run the following command to build the lab again, after we have added the Amazon SQS queue and the Amazon SNS subscription:

cd ~/environment/wild-rydes-async-messaging/lab-2
sam build

Now we are ready to update the application, by running the following command to deploy the change:

sam deploy \
    --guided \
    --stack-name wild-rydes-async-msg-2 \
    --capabilities CAPABILITY_IAM

In the meantime while your waiting, you may want to have a look at the AWS SAM template to make yourself familiar with the stack we launched. Just click on the template.yaml attachment below to see the content.

Because AWS SAM will only deploy/update/delete resources which are changed, it only takes a couple of seconds to deploy the new Amazon SQS queue and the Amazon SNS subscription.