Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can I use AWS CloudFormation to validate and apply SQS queue attributes


Can I use AWS CloudFormation to validate and apply SQS queue attributes


Yes, you can use AWS CloudFormation to validate and apply SQS queue attributes. Here are the steps to follow:

Step 1: Create a CloudFormation Template

Create a CloudFormation template that defines the SQS queue and its attributes. For example, you can use the following template:

json
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "MyQueue": {
      "Type": "AWS::SQS::Queue",
      "Properties": {
        "QueueName": "MyQueue",
        "VisibilityTimeout": 60,
        "MessageRetentionPeriod": 86400,
        "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:000000000000:tutorial-dlq-sqs\",\"maxReceiveCount\":\"1\"}"
      }
    }
  }
}

Step 2: Validate the Template

Use the `cfn-lint` command to validate the template for any errors:

bash
cfn-lint template.json

Step 3: Apply the Template

Use the `aws cloudformation create-stack` command to apply the template:

bash
aws cloudformation create-stack --stack-name MyStack --template-body file://template.json

Step 4: Verify the Queue Attributes

Use the `aws sqs get-queue-attributes` command to verify that the queue attributes have been applied correctly:

bash
aws sqs get-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/000000000000/MyQueue --attribute-names VisibilityTimeout,MessageRetentionPeriod,RedrivePolicy

Example Steps

1. Create a CloudFormation Template:
Create a JSON file named `template.json` with the following content:
json
   {
     "AWSTemplateFormatVersion": "2010-09-09",
     "Resources": {
       "MyQueue": {
         "Type": "AWS::SQS::Queue",
         "Properties": {
           "QueueName": "MyQueue",
           "VisibilityTimeout": 60,
           "MessageRetentionPeriod": 86400,
           "RedrivePolicy": "{\"deadLetterTargetArn\":\"arn:aws:sqs:us-east-1:000000000000:tutorial-dlq-sqs\",\"maxReceiveCount\":\"1\"}"
         }
       }
     }
   }
   

2. Validate the Template:
Use the `cfn-lint` command to validate the template for any errors:

bash
   cfn-lint template.json
   

3. Apply the Template:
Use the `aws cloudformation create-stack` command to apply the template:

bash
   aws cloudformation create-stack --stack-name MyStack --template-body file://template.json
   

4. Verify the Queue Attributes:
Use the `aws sqs get-queue-attributes` command to verify that the queue attributes have been applied correctly:

bash
   aws sqs get-queue-attributes --queue-url https://sqs.us-east-1.amazonaws.com/000000000000/MyQueue --attribute-names VisibilityTimeout,MessageRetentionPeriod,RedrivePolicy
   

This process ensures that the CloudFormation template is valid and correctly applies the SQS queue attributes.

Citations:
[1] https://stackoverflow.com/questions/68199178/how-to-check-if-cloudformation-template-is-correct
[2] https://plainenglish.io/blog/aws-sqs-cli-commands-with-examples-abb9a0f0e362
[3] https://docs.aws.amazon.com/cli/latest/reference/sqs/set-queue-attributes.html
[4] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sqs-queuepolicy.html
[5] https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sqs-queue.html