SNS Notification | AWS + .NET Core | Part 2

Fardeen Khan
Code Panthers
4 min readApr 13, 2022

--

This is Part 2 of a series. All the links are below

In this article, we are going to modify our Project from Part 1 and add a mechanism to send out email notifications to registered users by using SNS Topics. You will be able to add new users as well.

TL;DR

File upload requests to S3 using Upload Service will publish a notification to an SNS Topic. All email users subscripted to the topic will receive a notification about the file upload

At the end of this article, you will be able to

  • Create SNS Topics and Subscriptions
  • Integrate SNS to any Project
  • Publish messages to SNS Topics

SNS as a Delivery Mechanism

Here’s what the people at AWS have to say about SNS

Amazon Simple Notification Service (Amazon SNS) is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication.

Basically, it allows you to send over message payloads through protocols like Email, HTTP/S, Mobile Push Notification, other AWS services, and custom applications without managing any order of sorts (there is an option for that with FIFO SNS)

Prerequisites

  • The project we created in Part 1. Here’s the repo link
  • A verified AWS Account

Create an SNS Topic

Steps

  1. Head over to the AWS Console and select SNS Service.
  2. Create a Topic (Just provide a simple name, such as FileUploadTopic)

Optional Configuration

  1. Add an Optional Display Name (Better Readability)
  2. Enable Encryption using AWS KMS key
  3. Access Policy — If you wish to enable access to the topic and its subscriptions across AWS accounts or just to everyone
  4. Delivery Retry Policy (for HTTP/S protocol only) — takes care of the number of retries, delays, etc
  5. Delivery Status Logging — (Not for Email protocol) Will need a Service role to allow SNS to write logs to CloudWatch
  6. And finally Tags

And finally, create the Topic. Copy the Topic ARN, we will use it later on

Setup AWS Rules and Policies

We need permission for registering Topic Subscriptions for the E-mail protocol and also to publish messages on the topic.

Following the principle of Least Privileges, we are going to assign the following actions to the policy and restrict them to the topic we created

  • sns:Publish
  • sns:Subsribe

Here’s the JSON doc for the policy SNSAccessPolicyForUploadService

Attach the policy to the UploadServiceDev UserGroup we created in the last

Code It

Service Configuration

  • Add Nuget Package
    AWSSDK.SimpleNotificationService (v3.7.3.47)
  • Next, we are going to add the Topic ARN to the appsettings.json config file so we can access it in our services
{
......,
"AWS": {
"Profile": "default",
"Region": "ap-south-1"
},
"S3BucketName": "sdk-net-upload-service",
"S3ObjectExpirationHours": 6,
"SNSTopicArn" : "<TopicARN here>"
}
  • Edit the Startup.cs file and add the following lines to inject the SNS Service in the DI Container
public void ConfigureServices(IServiceCollection services)
{
.....
services.AddAWSService<IAmazonSimpleNotificationService>();
.....
}

Notification Service

In the Services directory, we will add a new Interface called INetworkService which will have the method declarations required for our use case. Here’s the code for the same

Here we have two methods each for the use-cases

  • Publish a message to a Topic aka Send an email notification
  • Register an email id to receive such notifications

In case we omit the Topic ARN from the method call, then we will revert to the SNSTopicArn we declared in appsettings.json

Now to write the full logic, by implementing these methods from the IService class so we have our concrete integration with SNS

Finally, we add these classes to the DI Container so we can access the same in other classes

public void ConfigureServices(IServiceCollection services)
{
.....
services.AddTransient<INotificationService, NotificationService>();
.....
}

Now, all we need is to use the services in the UploadController and FileUploadService.

Exposing the Service

Add notification call to FileUploadService. Just before returning the pre-signed URL as a response, we would like to send out the notification.

Here’s the updated Service class method

Next, we add a new controller action to register new email addresses to receive topic messages. We are now updating the UploadController with a new method. Here’s the snippet:

Lastly…..

With these changes added we have a working integration of SNS in our S3 File Upload project. Here's the link to the project on Github (Drop a start if you liked it too much !!!)

In the next article, we will explore adding CloudFront on top of the S3 when fetching the objects back to have an Edge-based CDN. Also, I am having a plan for discussing deployment strategies for the project through various AWS services. So have a lookout for the new articles as they come in.

Thanks for visiting!!!

Subscribe to our YouTube Channel over Here

And here’s our medium publication

--

--