How to Create Amazon SQS Queue using CloudFormation

How to Create Amazon SQS Queue using CloudFormation
Sharing is Caring:

In this post, I will help you create amazon SQS queue using CloudFormation. I will be discussing creation of standard queue as well as FIFO queue using CloudFormation.

Let’s start with understanding amazon SQS !!!

What is Amazon SQS?

Amazon SQS or Simple Queue Service is a fully managed message queue Service provided by AWS.

In the world of micro-services architecture, Amazon SQS helps you decouple your application components so that they can work and fail independently.

  • An SQS queue frees you from managing your own message queuing system
  • You can send, receive and store messages between components
  • Increases the fault tolerance of your application.
  • You can share sensitive data between components using encryption
  • SQS queues Scale efficiently as your application grows

SQS Provides two types of Message Queue

  1. Standard Queue
  2. FIFO Queue

Suggested Read: AWS SQS Standard Queue vs FIFO Queue: All You Need to Know

We will discuss how to create each one of them using CloudFormation in upcoming sections.

Steps to Create Amazon SQS Queue using CloudFormation?

Let’s see the step by step instruction to create amazon SQS queue using CloudFormation.

Step 1: Provide proper permission

If you are not an admin user, you should explicitly provide sqs:CreateQueue permission for your user/role. Additionally, you will also needs cloudformation:* as well to be able to do CloudFormation stack creation, updation etc.

Step 2: Prepare a template

You can use YAML or JSON for your template. I prefer YAML for writing my templates. But don’t worry, If you want it in JSON, I will provide JSON template as well.

To create an SQS standard and FIFO queue, all you need is a AWS::SQS::Queue resource like below.

  MyQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: DemoQueue

Few things to note there –

  • By default CloudFormation creates a standard queue for you. However, If you need a FIFO queue, you can specify a parameter FifoQueue as true.
  • Once you create a queue, you can not change it’s type. For example you can not convert a Standard queue to FIFO and vice versa after the creation.
  • If you are creating a FIFO queue, name must end with .fifo suffix.
  • You queue name must be unique for your account and region combination.

Ideally the template to create a simple FIFO queue looks like below.

  MyQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: DemoFifoQueue.fifo
      FifoQueue: true

Template to Create Amazon SQS Queue using CloudFormation : YAML

In this template, we are declaring two amazon queue , one standard and one FIFO. After the creation of queue we are outputting queue URL and Arn for later use.

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

Parameters:

  StandardQueueName:
    Type: String
    Description: Standard Queue Name
    Default: DemoStandardQueue
  FifoQueueName:
    Type: String
    Description: Fifo Queue Name
    Default: DemoFifoQueue.fifo

Resources:

 MyStandardQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: !Ref StandardQueueName
      
 MyFifoQueue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: !Ref FifoQueueName     
      FifoQueue: true
          
Outputs:
  StandardQueueURL:
    Description: Queue URL for standard queue
    Value: !Ref MyStandardQueue
  FifoQueueURL:
    Description: Queue URL for Fifo queue
    Value: !Ref MyFifoQueue
  StandardQueueArn:
    Description: Queue Arn for Standard queue
    Value: !GetAtt MyStandardQueue.Arn
  FifoQueueArn:
    Description: Queue Arn for FIFO queue
    Value: !GetAtt MyFifoQueue.Arn

Template to Create Amazon SQS Queue using CloudFormation: JSON

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Template to create a sample queue",
    "Parameters": {
        "StandardQueueName": {
            "Type": "String",
            "Description": "Standard Queue Name",
            "Default": "DemoStandardQueue"
        },
        "FifoQueueName": {
            "Type": "String",
            "Description": "Fifo Queue Name",
            "Default": "DemoFifoQueue.fifo"
        }
    },
    "Resources": {
        "MyStandardQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "QueueName": {
                    "Ref": "StandardQueueName"
                }
            }
        },
        "MyFifoQueue": {
            "Type": "AWS::SQS::Queue",
            "Properties": {
                "QueueName": {
                    "Ref": "FifoQueueName"
                },
                "FifoQueue": true
            }
        }
    },
    "Outputs": {
        "StandardQueueURL": {
            "Description": "Queue URL for standard queue",
            "Value": {
                "Ref": "MyStandardQueue"
            }
        },
        "FifoQueueURL": {
            "Description": "Queue URL for Fifo queue",
            "Value": {
                "Ref": "MyFifoQueue"
            }
        },
        "StandardQueueArn": {
            "Description": "Queue Arn for Standard queue",
            "Value": {
                "Fn::GetAtt": [
                    "MyStandardQueue",
                    "Arn"
                ]
            }
        },
        "FifoQueueArn": {
            "Description": "Queue Arn for FIFO queue",
            "Value": {
                "Fn::GetAtt": [
                    "MyFifoQueue",
                    "Arn"
                ]
            }
        }
    }
}

Step3: Create a Stack using prepared template

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

  1. Grab the YAML or JSON template from above as per your convinience.
  2. Save the template with .yml or .json as per the choice of template and follow below steps.
  3. Login to AWS Management Console, navigate to CloudFormation and click on Create stack
  4. Click on “Upload a template file”, upload your saved .yml  or .json file and click Next
  5. Enter the stack name and click on Next. In configuration, keep everything as default and click on Next.
  6. In the events tab of stack, you can view the status.
  7. Once stack is successfully created, you can go to SQS service and verify your queue.
  8. Also, you can check output tab of your CloudFormation stack to view queue url and Arn of queue.
Create SQS Queue using CloudFormation successful

Clean Up

If you are creating this SQS queue for learning purpose. Don’t forget to delete your CloudFormation stack so that your queue is deleted and you don’t bear any cost.

Happy Learning !!!

Conclusion:

In this post, we learnt how to create amazon SQS queue using CloudFormation.

  • As AWS provides two type of queue –Standard and FIFO. So we learnt to create each of them using CloudFormation
  • We learnt that by default, queue is created as standard.
  • Once created, queue type can not be changed.
  • FIFO queue name must have .fifo in the end as suffix

I hope you found this post helpful.

Enjoyed the content?

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Subscribe to our newsletter below to get awesome AWS learning materials delivered straight to your inbox.

Don’t forget to motivate me by-

  • Adding a comment below on what you liked and what can be improved.
  • Follow us on
  • Subscribe to our newsletter to get notified each time we post new content
  • Share this post with your friends

Suggested Read:

Sharing is Caring:

Leave a Reply

Your email address will not be published. Required fields are marked *