AWS Simple Queue Service (SQS) is a fully managed message queuing service that enables us to decouple and scale microservices,  serverless applications, and distributed systems. Using SQS, we can send, store, and receive messages between software components  without losing them. AWS SQS offers two types of message queues, Standard queues and FIFO Queues.  To understand more about SQS Queues, search for “How to create an SQS Queue on AWS?” article.

AWS CloudFormation allows us to use programming languages (yaml/json) or a simple text file to model and provision all the resources needed for our applications. This gives us a single source of truth for our AWS resources.

In this article, we will see the steps to create a Standard and FIFO Queue using Cloudformation Stack.

Pre-requisites

  1. AWS Account (Create if you don’t have one). 
  2. Basic understanding of Cloudformation Stack.
  3. Basic understanding of SQS Queues.

What will we do?

  1. Login to AWS.
  2. Create a Standard Queue using Cloudformation Stack
  3. Create a FIFO Queue using Cloudformation Stack

Login to AWS

  1. Click here to go to AWS Login Page.

When we hit the above link, we will see a web page as follows where we are required to login using our login details.

<img alt="Log into AWS" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.12_.28_PM_.png61e7ed5f6f139.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Once we login into AWS successfully, we will see the main console with all the services listed.

<img alt="AWS Management Console" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.12_.48_PM_.png61e7ed5fbce6f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Create a Standard Queue using Cloudformation Stack

Before we proceed to create a Standard Queue, copy the code from the following block or download the template from here and save it on your local machine. This template will be required while creating a Cloudformation Stack.

---
AWSTemplateFormatVersion: '2010-09-09'
Description: This stack creates a Standard Queue
Parameters:
  DelaySeconds:
    Description: "The time in seconds that the delivery of all messages in the queue is delayed"
    Type: Number
    Default: '5'
  MaximumMessageSize:
    Type: Number
    Description: "The limit of how many bytes that a message can contain before Amazon SQS rejects it"
    Default: '262144'
  MessageRetentionPeriod:
    Description: "The number of seconds that Amazon SQS retains a message."
    Type: Number
    Default: '345600'
  ReceiveMessageWaitTimeSeconds:
    Description: "Specifies the duration, in seconds, that the ReceiveMessage action call waits until a message is in the queue in order to include it in the response"
    Type: Number
    Default: '0'
  UsedeadletterQueue:
    Description: "A dead-letter queue is a queue that other (source) queues can target for messages that can't be processed (consumed) successfully."
    Type: String
    AllowedValues:
    - 'true'
    - 'false'
    Default: 'false'
  VisibilityTimeout:
    Description: "This should be longer than the time it would take to process and delete a message"
    Type: Number
    Default: '5'
Mappings: {}
Conditions:
  CreateDeadLetterQueue:
    Fn::Equals:
    - Ref: UsedeadletterQueue
    - 'true'
Resources:
  SQSQueue:
    Type: AWS::SQS::Queue
    Properties:
      DelaySeconds:
        Ref: DelaySeconds
      MaximumMessageSize:
        Ref: MaximumMessageSize
      MessageRetentionPeriod:
        Ref: MessageRetentionPeriod
      ReceiveMessageWaitTimeSeconds:
        Ref: ReceiveMessageWaitTimeSeconds
      RedrivePolicy:
        Fn::If:
        - CreateDeadLetterQueue
        - deadLetterTargetArn:
            Fn::GetAtt:
            - MyDeadLetterQueue
            - Arn
          maxReceiveCount: 5
        - Ref: AWS::NoValue
      VisibilityTimeout:
        Ref: VisibilityTimeout
  MyDeadLetterQueue:
    Condition: CreateDeadLetterQueue
    Type: AWS::SQS::Queue
Outputs:
  QueueURL:
    Description: URL of the created SQS
    Value:
      Ref: SQSQueue
  QueueARN:
    Description: ARN of the created SQS
    Value:
      Fn::GetAtt:
      - SQSQueue
      - Arn
  QueueName:
    Description: Name of the created SQS
    Value:
      Fn::GetAtt:
      - SQSQueue
      - QueueName
  DeadLetterQueueURL:
    Condition: CreateDeadLetterQueue
    Description: URL of the dead letter queue
    Value:
      Ref: MyDeadLetterQueue
  DeadLetterQueueARN:
    Condition: CreateDeadLetterQueue
    Description: ARN of the dead letter queue
    Value:
      Fn::GetAtt:
      - MyDeadLetterQueue
      - Arn

To create a Standard Queue using the Cloudformation Stack, click on “Services” from the top menu bar  and search for “Cloudformation”.

<img alt="Standard QUEUE" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.12_.54_PM_.png61e7ed602f796.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

On the main dashboard for Cloudformation, click on “Create Stack” to create a stack.

<img alt="Stacks" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.13_.02_PM_.png61e7ed60ace93.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

To upload the template from your local machine, click on “Upload a template file” radio button and click on “Next”.

<img alt="Create Stack" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.13_.16_PM_.png61e7ed614153c.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Specify a name to the stack to be created and fill in the required details or proceed with the default values and click on “Next”.

<img alt="Stack Details" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.15_.02_PM_.png61e7ed61dd460.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Specify the tag which can be applied to the SQS upon its creation and click on “Next”.

<img alt="Advanced options" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.15_.15_PM_.png61e7ed6227366.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Scroll down the page and click on the “Create Stack” button to create a stack which will create a Standard Queue.

<img alt="Policies and Notifications" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.15_.22_PM_.png61e7ed62b656f.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

You can see the status under Events. Once the status changes to “CREATE_COMPLETE” of the stack, this means that the Queue has been created.

<img alt="SQS Queue created" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.15_.34_PM_.png61e7ed632bf51.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Click on “Services” and search for “SQS” to see if the queue has been created or not.

<img alt="SQS Service" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.15_.43_PM_.png61e7ed63a14da.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

On the main dashboard of the SQS, you can see that the Queue has been created and the name given to the Queue is Cloudformation Stack name with some random suffix string to it, the reason for this is that we did not specify Queue Name in the stack.

<img alt="Create new QUEUE" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.16_.11_PM_.png61e7ed64059f0.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Create a FIFO Queue using Cloudformation Stack

Before we proceed to create a FIFO  Queue, copy the code from the following block or download the template from here and save it on your local system.

---
AWSTemplateFormatVersion: '2010-09-09'
Description: This stack creates a FIFO Queue
Parameters:
  ContentBasedDeduplication:
    Description: Specifie whether to enable content-based deduplication
    Type: String
    AllowedValues:
    - 'true'
    - 'false'
    Default: 'true'
  QueueName:
    Description: This stack will append .fifo to the end of the Queue name.
    Type: String
  DelaySeconds:
    Description: "The time in seconds that the delivery of all messages in the queue"
    Type: Number
    Default: '5'
  MaximumMessageSize:
    Type: Number
    Description: "The limit of how many bytes that a message can contain before Amazon"
    Default: '262144'
  MessageRetentionPeriod:
    Description: "The number of seconds that Amazon SQS retains a message."
    Type: Number
    Default: '345600'
  ReceiveMessageWaitTimeSeconds:
    Description: "Specifies the duration, in seconds, that the ReceiveMessage action
      call waits until a message is in the queue in order to include it in the response"
    Type: Number
    Default: '0'
  UsedeadletterQueue:
    Description: "A dead-letter queue is a queue that other (source) queues can target
      for messages that can't be processed (consumed) successfully."
    Type: String
    AllowedValues:
    - 'true'
    - 'false'
    Default: 'false'
  VisibilityTimeout:
    Description: "This should be longer than the time it would take to process and
      delete a message"
    Type: Number
    Default: '5'
Mappings: {}
Conditions:
  CreateDeadLetterQueue:
    Fn::Equals:
    - Ref: UsedeadletterQueue
    - 'true'
Resources:
  SQSQueue:
    Type: AWS::SQS::Queue
    Properties:
      ContentBasedDeduplication:
        Ref: ContentBasedDeduplication
      FifoQueue: 'true'
      QueueName:
        Fn::Join:
        - ''
        - - Ref: QueueName
          - ".fifo"
      MaximumMessageSize:
        Ref: MaximumMessageSize
      MessageRetentionPeriod:
        Ref: MessageRetentionPeriod
      ReceiveMessageWaitTimeSeconds:
        Ref: ReceiveMessageWaitTimeSeconds
      RedrivePolicy:
        Fn::If:
        - CreateDeadLetterQueue
        - deadLetterTargetArn:
            Fn::GetAtt:
            - MyDeadLetterQueue
            - Arn
          maxReceiveCount: 5
        - Ref: AWS::NoValue
      VisibilityTimeout:
        Ref: VisibilityTimeout
  MyDeadLetterQueue:
    Condition: CreateDeadLetterQueue
    Type: AWS::SQS::Queue
    Properties:
      FifoQueue: 'true'
      QueueName:
        Fn::Join:
        - ''
        - - Ref: QueueName
          - Deadletter
          - ".fifo"
Outputs:
  QueueURL:
    Description: URL of the created SQS
    Value:
      Ref: SQSQueue
  QueueARN:
    Description: ARN of the created SQS
    Value:
      Fn::GetAtt:
      - SQSQueue
      - Arn
  QueueName:
    Description: Name of the created SQS
    Value:
      Fn::GetAtt:
      - SQSQueue
      - QueueName

Go back to the main dashboard of Cloudformation and follow the same steps we followed to create a Standard Queue.

<img alt="FIFO Queue" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.16_.32_PM_.png61e7ed64352c3.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Once the stack gets created, you can see that the FIFO Queue is ready to use. Here you see that the FIFO Queue does not have some random string, the reason for this is that we have an option in the Cloudformation Template where we can specify the name for the Queue to be created.

<img alt="Queue list" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.21_.28_PM_.png61e7ed6475023.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

If the Queues are no more needed, they can be deleted by deleting the Cloudformation Stack from the main dashboard.

<img alt="List of Stacks" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/01/echo/Screenshot_2020-03-22_at_3.22_.17_PM_.png61e7ed64e0079.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="469" loading="lazy" src="data:image/svg xml,” width=”750″>

Conclusion

In this article, we saw the steps to create a Standard and FIFO Queue using Cloudformation Stack.