Since I had the opportunity to use SQS in my business, I will summarize the operation check method that I did at that time.
SQS (Amazon Simple Queue Service) is a message queue, a service that acts as an intermediary for sending and receiving data.
It has almost the same function as RabbitMQ
, which is famous as a message broker.
The advantages of using SQS are: https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html
You must create a queue before you can send or receive data.
Create it by following the steps in the AWS console Service → sqs → Create queue
.
There are standard
and FIFO
in the queue, but this time we will use it as standard because we only check the operation.
The data transmission method is as follows. You need to set ʻaws configure` in advance.
import boto3
import json
que_name = 'test_que'
sqs = boto3.resource('sqs')
queue = sqs.get_queue_by_name(QueueName=que_name)
def sqs_send_msg():
msg = [{'Id': '1', 'MessageBody': json.dumps([{'result': "msg_1"}])}]
response = queue.send_messages(Entries=msg)
print(response)
return response
sqs_send_msg()
It is also possible to communicate with sqs by specifying the url of sqs. You can check the url from the AWS console screen.
import boto3
import json
sqs = boto3.client('sqs')
queue_url = "https://sqs.ap-northeast-1.amazonaws.com/xxxxxxxxxxxx/test_que"
def sqs_receive_msg():
msg = sqs.receive_message(QueueUrl=queue_url, MaxNumberOfMessages=10)
print(msg)
sqs_receive_msg()
Recommended Posts