3 Things: Developing Amazon SQS Based Solutions
Tags: aws, serverless, sqs, beginners
Published: 2022-09-12 13:31:22

Amazon Simple Queue Service or Amazon SQS is a distributed message queuing service that enables developers to build  loosely coupled solutions. Often valued for its ease of use, Amazon SQS queues can be spun up in a matter of seconds from the AWS console, SDK or the CLI.   Still, there are subtleties that developers should be aware of when developing solutions with Amazon SQS.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xuhpd93gxryqqbfgsrx4.jpg)

Photo by [Shumilov Ludmila](https://unsplash.com/@lsfineartphotography?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

## Encryption

Messages stored in an Amazon SQS queue is data at rest. And, we can protect that data by encrypting it with KMS keys, just like we would if we were going to protect data in an Amazon S3 bucket or an Amazon SNS topic. With Amazon SQS Server-side encryption with KMS, messages are encrypted when they are received by SQS and are decrypted when delivered to a message consumer that is authorized for the SQS message queue and the KMS key.

Alternatively, Amazon Simple Queue Service provides it's own server-side encryption using SQS-managed encryption keys. Encrypting an SQS queue using SQS-managed encryption keys is as easy as using the following command:

```
$ aws sqs set-queue-attributes \
   ––queue-url https://(MyQueueURL) \
   ––attributes '{"SqsManagedSseEnabled": "true"}'
```
## Temporal Messaging

By default, messages that are stored in Amazon SQS are designed for "At-Least-Once Delivery". That is, an Amazon SQS message will be delivered at least once, but can be delivered more than once and there is no guarantee that the messages will stay ordered.  However, if you need messages delivered in order and only once, you may opt for Amazon SQS FIFO queues.  Amazon SQS FIFO queues are designed for, Exactly-Once Processing, where messages are only delivered once and are delivered in the order of, First-In-First-Out.

Below is an example of sending a message to an Amazon SQS FIFO queue using the AWS .NET SDK. Note the use of the "MessageGroupId" property.  This is a requirement for FIFO messaging.

```
SendMessageRequest sendMessageRequest = new SendMessageRequest(queueUrl, message);

sendMessageRequest.MessageGroupId = "message-group-1";

var sqsClient = new AmazonSQSClient();

await sqsClient.SendMessageAsync(sendMessageRequest);
```

## Message Retention

Messages in Amazon SQS can not be stored forever.  In fact, messages in Amazon SQS can be stored for a maximum of 14 days and for as little as 1 minute.  By default, the Amazon SQS message retention period is a generous 4 days.

The following is an example of a command for setting an Amazon SQS queue to have a retention period of 1 minute using the AWS CLI.

```
$ aws sqs set-queue-attributes \
   ––queue-url https://(MyQueueURL) \
   ––attributes '{"MessageRetentionPeriod": "60'
```
Want to know more about the tech in this article?  Checkout these resources:

[AWS CLI](https://aws.amazon.com/cli/), [Configuring the AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html), [AWS .NET SDK](https://aws.amazon.com/sdk-for-net/), [.NET](https://dotnet.microsoft.com/en-us/learn/dotnet/what-is-dotnet)