aws sns - setting up local development environment to publish messages to aws sns c#
When developing locally, we should probably configure our development environment (there's many options available) and this can be done by setting up by running aws configure. You will be prompted for your id, password and region. If you already set this up, then you can proceed to create a SNS topic.
In this exercise, we will create SNS topic and then send a message via dotnet app. Then we use a SQS to subscribe to this topic.
You need to be creating your SNS topic and ensure that you allow anyone to be able to subscribe to this topic. This is required for easy setup for SQS later
Next, we will setup who can publish to this topic.
Setting up SNS publish policy for local development AWS user
Go to your AWS console and create a topic. Ensure you have configure:
{
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::your-aws-account-id:user/jeremydev"
},
"Action": "SNS:Publish",
"Resource": "arn:aws:sns:ap-southeast-2:your-aws-account-id:topicfree"
}
Next, create your dotnet project and add the following dependencies
dotnet add package AWSSDK.SimpleNotificationService --version 3.7.400.77
Once you have done that, you can write the following code to push message to the queue.
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
string topicArn = "arn:aws:sns:ap-southeast-2:your-aws-account-id:topicfree";
string messageText = "This is an example message to publish to the ExampleSNSTopic.";
IAmazonSimpleNotificationService client = new AmazonSimpleNotificationServiceClient();
await PublishToTopicAsync(client, topicArn, messageText);
static async Task PublishToTopicAsync(
IAmazonSimpleNotificationService client,
string topicArn,
string messageText)
{
var request = new PublishRequest
{
TopicArn = topicArn,
Message = messageText,
};
var response = await client.PublishAsync(request);
Console.WriteLine($"Successfully published message ID: {response.MessageId}");
}
Create your SQS.
Goto AWS console and create your SQS. A standard queue would work. As for the access policy, we would like to configure message received to a specific user - as we will be retrieving the message from SQS via another dotnet app.
After you run the application, you should get a message as shown here
To read from a SQS message queue via dotnet, please create a new console app and then add the following libraries in there.
dotnet add package AWSSDK.SQS --version 3.7.400.77
Also notice that you have to manually remove the message after you have received it - as shown in the code below :-
// See https://aka.ms/new-console-template for more information
using Amazon.SQS;
using Amazon.SQS.Model;
// Create the Amazon SQS client
var sqsClient = new AmazonSQSClient();
var result = await GetMessage(sqsClient, "https://sqs.ap-southeast-2.amazonaws.com/your-aws-account-id/hello");
Console.WriteLine("done!");
static async Task<ReceiveMessageResponse> GetMessage(
IAmazonSQS sqsClient, string qUrl, int waitTime = 0)
{
var response = await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest
{
QueueUrl = qUrl,
MaxNumberOfMessages = 10,
WaitTimeSeconds = waitTime
// (Could also request attributes, set visibility timeout, etc.)
});
if (response != null)
await sqsClient.DeleteMessageAsync(qUrl, response.Messages[0].ReceiptHandle);
return response;
Comments