Lambda in .NET Core and SNS
Contents
This week I wanted to investigate how to use a Pub/Sub mechanism in an on-premise environment without needing to install anything else in our infrastructure, so using a managed service. I also wanted to make my first tests with CDK. Let me show you what I did.
Goal
What I want to accomplish is that a system that lives in our infrastructure communicates asynchronously with another system on it. It’s easy to simulate the sender of the message with a console application, but for the receiver I’ll need a public HTTP endpoint. The easiest way I know to have a public HTTP endpoint is by having a Lambda function published via API Gateway, so let me do this. The experiment is still valid because, at the end, is just an HTTP endpoint.
Lambda function using .NET Core
I took advantage of the situation to develop a lambda function in .NET Core for the first time. I’ll be using the Serverless framework so you need to install and configure it. Once you’ve done that, create a folder for the entire system (I’ve called it SNSSample
) and type this command in your preferred terminal:
|
|
This will create the barebones of a Lambda function in .NET Core. To accomplish our mission, we’ll need to add a couple of NuGet packages:
|
|
We need the first one to be able to have the needed request and response objects for API Gateway. We need the second one to be able to parse an SNS message.
And now it’s time to take a look at the code of the function
|
|
When you create an HTTPS subscription in SNS, you need the subscription to be confirmed by the receiver endpoint. The way to do that is to visit the URL that SNS sends to it in the confirmation message. That what we do in the first part of the function.
Once the subscription is confirmed, you will receive the message and you’ll be able to do what you need with it.
The last step is to correctly configure the serverless.yml
file, which should look like like this:
|
|
Nothing super-special here, we just need an endpoint that responds to a POST request.
Let’s deploy this! First, we need to build the package. To build it, we can use the script that the Serverless framework has created for us:
|
|
And now, we can deploy the lambda function. In my case, I have a profile configured called serverless-local
so I need to type this:
|
|
Make sure you write down the URL of the Lambda function because we’ll need it.
SNS Topic
The next step is to create the SNS topic. We’ll use CDK to do this.
Let’s first create a folder just below our main folder called IaC. Go inside the folder and run the following command:
|
|
This will create the barebones of a CDK project using C# as a language to define your infrastructure.
In order to create an SNS endpoint, we’ll need a couple of NuGet packages. Type these commands in the IaC\src\IaC
folder:
|
|
It’s time to define our infrastructure, which is basically an SNS topic with a HTTP subscription:
|
|
Quite easy, isn’t it?
In a production environment you’d need to configure retries and DLQ. We’ll see that in future posts.
Go back to the main IaC folder and deploy the infrastructure:
|
|
If you now go the AWS Console, you will see that the subscription is confirmed
And if you go to CloudWatch you’ll see that the endpoint has been called.
Please, go to the AWS Console and write down the ARN of the SNS topic because we’ll need it in the next step.
Sending messages
Now it’s time to send a message to the topic. Go to the root folder and create a Console App:
|
|
Go inside the folder and add the following packages
|
|
Create an appsettings.json
file with the following structure (use your profile and the region you preffer):
|
|
And make sure the file is copied to the output directory by adding this to the csproj:
|
|
And finally, write the code to send the message:
|
|
As you can see, we’re loading the configuration file and using the GetAWSOptions
to get an object which can be used to create the client. Finally, you just need to create the request and send it. If you want to do the same in a ASP .NET Core application, you can follow this post by Steve Gordon.
You can now build and run the application
|
|
and take a look at cloudwatch to see that the message is there!
Summary
In this article we’ve seen how to create an SNS topic, subscribe to it and send a message to it. You can use this to send asynchronous messages between your services, even if they are deployed in your own infrastructure. Hope it helps!
Author Vicenç García
LastMod 14-06-2020