SQS queues and messages

Hi, I am trying to use the SQS integration of jets.

I am able to automatically create on deploy the SQS queue, but I don’t know how to programmatically retrieve the SQS URL necessary to send the messages. There is a method for that or some kind of syntax sugar to send a message to the defined queue?

My current code:

class Event < Jets::Stack
  sqs_queue(:events)
end

class EventJob < ApplicationJob
  depends_on :event
  sqs_event ref(:events)
  def dig
    puts "lol"
  end
end

Thanks.

@ceritium Good question. There was no nice way to programmatically get the SQS url. So added it in v1.9.15 in https://github.com/tongueroo/jets/pull/289

Adjusted some docs here: https://rubyonjets.com/docs/events/sqs/

Going from your example:

app/jobs/event_job.rb:

class EventJob < ApplicationJob
  class_timeout 30 # must be less than or equal to the SQS queue default timeout
  depends_on :event
  sqs_event ref(:events)
  def dig
    puts "lol"
    puts "received event payload: #{JSON.dump(event)}"
  end
end

app/jobs/postman_job.rb:

class PostmanJob < ApplicationJob
  include Jets::AwsServices

  iam_policy "sqs"
  def deliver
    queue_url = Event.lookup(:events_url)
    message_body = JSON.dump({"test": "hello world"})
    sqs.send_message(
      queue_url: queue_url,
      message_body: message_body,
    )
  end
end

The Event.lookup(:events_url) is looking up the url via the CloudFormation output in the created Event shared resource stack.

Note, take care not to send a message to the same queue in the same method that is being used as the lambda trigger. It’ll cause an infinite loop, and you’ll eventually exhaust the free lambda tier.

Here’s a demo project with the source code: Jets SQS Shared Resource Example

Awesome, thank you very much for your quick response!

You’re welcome. Here’s a demo project with the source code: Jets SQS Shared Resource Example. Also added it above.