AWS SDK for Ruby
Developer Guide

Working with Queues in Amazon SQS

Amazon SQS provides highly scalable hosted queues for storing messages as they travel between applications or microservices. To learn more about queues, see How Amazon SQS Queues Work.

In this example, you use the AWS SDK for Ruby with Amazon SQS to:

  1. Get a list of your queues by using Aws::SQS::Client#list_queues.

  2. Create a queue by using Aws::SQS::Client#create_queue.

  3. Get the queue's URL by using Aws::SQS::Client#get_queue_url.

  4. Delete the queue by using Aws::SQS::Client#delete_queue.

Prerequisites

Before running the example code, you need to install and configure the AWS SDK for Ruby, as described in:

Example

require 'aws-sdk-sqs' # v2: require 'aws-sdk' sqs = Aws::SQS::Client.new(region: 'us-east-1') # Get a list of your queues. sqs.list_queues.queue_urls.each do |queue_url| puts queue_url end # Create a queue. queue_name = "my-queue" begin sqs.create_queue({ queue_name: queue_name, attributes: { "DelaySeconds" => "60", # Delay message delivery for 1 minute (60 seconds). "MessageRetentionPeriod" => "86400" # Delete message after 1 day (24 hours * 60 minutes * 60 seconds). } }) rescue Aws::SQS::Errors::QueueDeletedRecently puts "A queue with the name '#{queue_name}' was recently deleted. Wait at least 60 seconds and try again." exit(false) end # Get the queue's URL. queue_url = sqs.get_queue_url(queue_name: queue_name).queue_url puts queue_url # Delete the queue. sqs.delete_queue(queue_url: queue_url)

On this page: