-
Notifications
You must be signed in to change notification settings - Fork 3
AWS SQS setup
chinyakao edited this page Jun 2, 2022
·
1 revision
- credit card needed
- get in My Security Credentials (at the right top dropdown)
- get in Access keys (access key ID and secret access key)
- create new access key
- copy the credentials
- NOTE: the credentials is shown only one time except downloading the file
- get in sqs site
- set the region (at the right top dropdown)
- dev & test: Asia Pacific (Tokyo)ap-northeast-1
- production(Heroku): US East (N. Virginia)us-east-1 (NOT SURE)
- create queue
- NOTE: the queue name and type could not change after created
- copy the queue name & queue URL
require 'aws-sdk-sqs'
sqs = Aws::SQS::Client.new(
access_key_id: config.AWS_ACCESS_KEY_ID,
secret_access_key: config.AWS_SECRET_ACCESS_KEY,
region: config.AWS_REGION
)
q_url = sqs.get_queue_url(queue_name: 'your_queue_name').queue_url
queue = Aws::SQS::Queue.new(url: q_url, client: sqs)
msg = { code: "The time is #{Time.now}"}
queue.send_message(message_body: msg.to_json)
messages = queue.receive_messages
msg = messages.first
# PROCESS THE MESSAGE HERE
JSON.parse(msg.body)
queue.delete_messages(
entries: [{
id: msg.message_id,
receipt_handle: msg.receipt_handle
}])
poller = Aws::SQS::QueuePoller.new(q_url, client: sqs)
poller.poll do |msg|
print "MESSAGE: #{JSON.parse msg.body}, "
end