Dear reader, I hope you are doing good. In today’s post, I will help you Use the CloudFormation application to configure the AWS SQS message queue.

But before we configure the SQS DLQ with CloudFormation, let’s try to understand what a dead string is and how it works. Feel free to skip section below If you are already familiar with SQS DLQ.

What is AWS SQS Dead Letter Queue?

As you may already know, Amazon SQS or Simple queuing service is a fully managed messaging service provided by AWS. In the microservice architecture, Amazon SQS helps to separate application components so that they can operate and fail independently.

Message producers send messages to the queue and the consumer processes them. Sounds perfect right?

What happens if the message fails to process?

What if the consumer is unable to process the message multiple times?

You don’t want to try to keep processing one message indefinitely, right?

You want to continue with the next message. But of course you want to debug your app later on all such failed messages.

Right?

How do you do that?

Well, to handle such messages, SQS offers you a feature called a queue of dead books. you can save all messages that could not be processed after a few attempts in the dead letter queue.

In other words, SQS DLQ allows us to store a message that the consumer could not process for any reason, such as corrupted messages or encoding errors, etc. So you can check the DLQ later to work on the messages.

More specifically, the SQS DLQ stores messages that were successfully received by the consumer but could not be processed even after several attempts.

If you want to understand SQS DLQ and how it works in detail, check out my previous article SQS dead letter queue.

So far, I assume you have a clear idea of ​​what SQS DLQ is and how it works. Assign a dead book queue to the application queue using CloudFormation.

How to configure AWS SQS using Dead Letter Queue CloudFormation?

Let’s look at the step-by-step instructions for configuring the AWS SQS dead letter string using CloudFormation.

Phase 1: Give the appropriate permission

  • If you are not an administrator user, you must provide it explicitly squares: CreateQueue and sqs: SetQueueAttributes your access rights user / role.
  • In addition, you also need cloud formation: * be able to create, update, etc. the CloudFormation stack

entry: If you can’t figure out the right permission at the beginning, you can use sqs: *, but it’s not safe to allow all functions, and you should only use it until you find the right permission.

Step 2: Prepare the model

You can use YAML or JSON your model. I prefer to like YAML when writing my models. But don’t worry, if you want it in JSON, I will JSON model.

How do you define a queue of dead books?

Determine a a queue of dead books you only need to create two queues for your actual queue (1 source queue and 1 dead letter)

Set the source queue redesign policy to send a failed message to the dead letter queue.

AWS::SQS::Queue The RedrivePolicy resource is shown below.

 MySourceQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      RedrivePolicy: 
        deadLetterTargetArn: !GetAtt MyDeadLetterQueue
        maxReceiveCount: 5

deadLetterTargetArn : Here you specify the ARN of the actual queue that you want to use as a sequence of dead letters.

maxReceiveCount: How many times the message is delivered to the source queue before it is moved to the dead letter queue.

A few things to consider –

  • The dead letter sequence in the FIFO queue must also be a FIFO queue. Similarly, the dead string of a constant string must also be a constant string.
  • By default, CloudFormation creates a regular queue for you. However, if you need to FIFO queue, you can specify a parameter FifoQueue true.
  • When you create a queue, you cannot change its type. For example, you cannot convert a Ordinary queue that FIFO and vice versa after creation.
  • If you create a FIFO queue, the name must end with a .fifo suffix.
  • The queue name must be unique to your account and region combination.
  • SQS does not create a queue automatically. You must first create a string and then use it as a string of dead letters.
  • Both the source queue and the dead book queue must be present in the same account and region

Model for creating an Amazon SQS queue using CloudFormation: YAML

In this model, two Amazon queues are reported, one standard and one FIFO. After creating the queue, we provide the queue URL and Arn for later use.

AWSTemplateFormatVersion: '2010-09-09'
Description: Template to configure a sample queue

Parameters:

  MySourceQueueName:
    Type: String
    Description: Standard Queue Name
    Default: app-queue
  MyDeadLetterQueueName:
    Type: String
    Description: Fifo Queue Name
    Default: app-dead-letter-queue

Resources:

 MySourceQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: !Ref MySourceQueueName
      RedrivePolicy: 
        deadLetterTargetArn: !GetAtt MyDeadLetterQueue.Arn
        maxReceiveCount: 5
      
 MyDeadLetterQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: !Ref MyDeadLetterQueueName     
          
Outputs: 
  SourceQueueURL: 
    Description: URL of Source Queue
    Value: !Ref MySourceQueue
  SourceQueueARN: 
    Description: ARN of source queue
    Value: !GetAtt MySourceQueue.Arn
  DeadLetterQueueURL: 
    Description: URL of dead-letter queue
    Value: !Ref MyDeadLetterQueue
  DeadLetterQueueARN: 
    Description: ARN of dead-letter queue
    Value: !GetAtt MyDeadLetterQueue.Arn

Model for creating an Amazon SQS queue using CloudFormation: JSON

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Template to configure a sample queue",
    "Parameters": {
        "MySourceQueueName": {
            "Type": "String",
            "Description": "Standard Queue Name",
            "Default": "app-queue"
        },
        "MyDeadLetterQueueName": {
            "Type": "String",
            "Description": "Fifo Queue Name",
            "Default": "app-dead-letter-queue"
        }
    },
    "Resources": {
        "MySourceQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "QueueName": {
                    "Ref": "MySourceQueueName"
                },
                "RedrivePolicy": {
                    "deadLetterTargetArn": {
                        "Fn::GetAtt": [
                            "MyDeadLetterQueue",
                            "Arn"
                        ]
                    },
                    "maxReceiveCount": 5
                }
            }
        },
        "MyDeadLetterQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "QueueName": {
                    "Ref": "MyDeadLetterQueueName"
                }
            }
        }
    },
    "Outputs": {
        "SourceQueueURL": {
            "Description": "URL of Source Queue",
            "Value": {
                "Ref": "MySourceQueue"
            }
        },
        "SourceQueueARN": {
            "Description": "ARN of source queue",
            "Value": {
                "Fn::GetAtt": [
                    "MySourceQueue",
                    "Arn"
                ]
            }
        },
        "DeadLetterQueueURL": {
            "Description": "URL of dead-letter queue",
            "Value": {
                "Ref": "MyDeadLetterQueue"
            }
        },
        "DeadLetterQueueARN": {
            "Description": "ARN of dead-letter queue",
            "Value": {
                "Fn::GetAtt": [
                    "MyDeadLetterQueue",
                    "Arn"
                ]
            }
        }
    }
}

Step 3: Create a stack using the prepared template

Now we know the basics and we have a model, so let’s go create a stack.

  1. Grasp the YAML or JSON model from above according to mental health.
  2. Save the template according to the .yml or .json file template and follow the instructions below.
  3. log in AWS Management Console, go to CloudFormation and click Create Stack
  4. Click “Upload Sample File”, download saved .ly or .json file and click Next
  5. Enter a name for the stack and click Next. Keep everything in the configuration by default and click Next.
  6. On the Stack Events tab, you can view the status.
  7. Once the stack is created, you can go to SQS and check your queue.
  8. You can also check the Print tab of the CloudFormation stack to see the queue URL and the queue Arn.

Clean up

If you assign this SQS queue to a string of dead letters for learning purposes. Don’t forget to delete your CloudFormation stack so that your queue is deleted and you don’t pay anything.

Good learning !!!

Conclusion:

In this post, we specified AWS SQS dead string using CloudFormation. Let’s summarize what we have learned.

We learn it

  • SQS dead letter string works great for storing messages that could not be successfully processed even by a certain number of companies named MaxReceiveCount
  • Messages can be saved to a DLQ file for later debugging or other operations
  • AWS does not automatically create a trade book queue. You must first create a string and then use it as a string of dead letters
  • So ideally, we created two strings and used one string of dead letters by defining it as the source string in Redrive policy.
  • Both the source string and the dead letter string should be of the same type
  • In addition, both queues should be in the same account and in the same area

I hope you found this post useful.

Do you enjoy the content?

Subscribe to our newsletter below to receive great AWS learning materials straight to your inbox.

Don’t forget to motivate me

  • Add a comment below about what you liked and what could be improved.
  • Follow us
  • Share this message with your friends

Suggested figure:

LEAVE A REPLY

Please enter your comment!
Please enter your name here