Amazon SQS

Amazon Simple Queue Service (SQS) is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. SQS eliminates the complexity and overhead associated with managing and operating message-oriented middleware, and empowers developers to focus on differentiating work. Using SQS, you can send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available.

Prerequisite Tasks

To use these operators, you must do a few things:

Operators

Publish a message to an Amazon SQS queue

To publish a message to an Amazon SQS queue you can use the SqsPublishOperator

In the following example, the task publish_to_queue publishes a message containing the task instance and the execution date to a queue with a default name of Airflow-Example-Queue.

tests/system/providers/amazon/aws/example_sqs.py[source]

publish_to_queue_1 = SqsPublishOperator(
    task_id='publish_to_queue_1',
    sqs_queue=sqs_queue,
    message_content='{{ task_instance }}-{{ logical_date }}',
)
publish_to_queue_2 = SqsPublishOperator(
    task_id='publish_to_queue_2',
    sqs_queue=sqs_queue,
    message_content='{{ task_instance }}-{{ logical_date }}',
)

Sensors

Read messages from an Amazon SQS queue

To read messages from an Amazon SQS queue until exhausted use the SqsSensor

tests/system/providers/amazon/aws/example_sqs.py[source]

read_from_queue = SqsSensor(
    task_id='read_from_queue',
    sqs_queue=sqs_queue,
)
# Retrieve multiple batches of messages from SQS.
# The SQS API only returns a maximum of 10 messages per poll.
read_from_queue_in_batch = SqsSensor(
    task_id='read_from_queue_in_batch',
    sqs_queue=create_queue,
    # Get maximum 10 messages each poll
    max_messages=10,
    # Combine 3 polls before returning results
    num_batches=3,
)

Was this entry helpful?