[aws] Send and receive sqs messages

Overview

Since I had the opportunity to use SQS in my business, I will summarize the operation check method that I did at that time.

What is SQS

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

Creating a queue

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.

スクリーンショット 2020-10-31 11.11.50.png

There are standard and FIFO in the queue, but this time we will use it as standard because we only check the operation.

Send data

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()

Data reception

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

[aws] Send and receive sqs messages
Send and receive Flask images
[AWS] Operate SQS from SDK (send / receive) [Python] [Node.js]
Send messages and images using LineNotify
Send messages to Skype and Chatwork in Python
Start communication with UDP and send and receive with TCP
[blackbird-sqs] Monitoring AWS SQS
Send and receive Gmail via the Gmail API using Python
Send and receive data with MQTT via Watson IoT Platform
AWS SNS delivers duplicate messages