Skip to content

Commit d69dae4

Browse files
rlhagermbrmur
andauthored
Python: SNS SQS Topics and Queues Feature Scenario with steering doc updates (#7704)
* Create feature_scenario.md * Initial files. * Fixes for tests and scenario. * Initial tests * Updates to tests. * Fix integration tests. * Fix for tests and emojis. * Adding metdata. * Fixes for pagination. * Fixes for tags. * Fixes for metadata. * Tag fixes. * Update to metadata. * README updates. * Create feature_scenario.md * Initial files. * Fixes for tests and scenario. * Initial tests * Updates to tests. * Fix integration tests. * Fix for tests and emojis. * Adding metdata. * Fixes for pagination. * Fixes for tags. * Fixes for metadata. * Tag fixes. * Update to metadata. * README updates. * Fix for duplicate metadata. * Updates to scenario READMEs. * Updates to instructions. * Update README.md * Update sqs_metadata.yaml * Updates to steering and scenario text. * Removed confusing descriptions. * Update README.md --------- Co-authored-by: Brian Murray <40031786+brmur@users.noreply.github.com>
1 parent 3b2a35f commit d69dae4

18 files changed

Lines changed: 3575 additions & 569 deletions

File tree

.doc_gen/metadata/sns_metadata.yaml

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,14 @@ sns_CreateTopic:
526526
- sdk_version: 3
527527
github: python/example_code/sns
528528
excerpts:
529-
- snippet_tags:
529+
- description:
530+
snippet_tags:
530531
- python.example_code.sns.SnsWrapper
531532
- python.example_code.sns.CreateTopic
533+
- description:
534+
snippet_tags:
535+
- python.example_code.sns.SnsWrapper.decl
536+
- python.example_code.sns.CreateFifoTopic
532537
Ruby:
533538
versions:
534539
- sdk_version: 3
@@ -631,9 +636,14 @@ sns_DeleteTopic:
631636
- sdk_version: 3
632637
github: python/example_code/sns
633638
excerpts:
634-
- snippet_tags:
639+
- description:
640+
snippet_tags:
635641
- python.example_code.sns.SnsWrapper
636642
- python.example_code.sns.DeleteTopic
643+
- description:
644+
snippet_tags:
645+
- python.example_code.sns.SnsWrapper.decl
646+
- python.example_code.sns.DeleteFifoTopic
637647
Swift:
638648
versions:
639649
- sdk_version: 1
@@ -765,6 +775,10 @@ sns_Publish:
765775
snippet_tags:
766776
- python.example_code.sns.SnsWrapper
767777
- python.example_code.sns.Publish_MessageStructure
778+
- description:
779+
snippet_tags:
780+
- python.example_code.sns.SnsWrapper.decl
781+
- python.example_code.sns.Publish
768782
Ruby:
769783
versions:
770784
- sdk_version: 3
@@ -1117,6 +1131,10 @@ sns_Subscribe:
11171131
snippet_tags:
11181132
- python.example_code.sns.SnsWrapper
11191133
- python.example_code.sns.Subscribe
1134+
- description: Subscribe a queue to a topic with optional filters.
1135+
snippet_tags:
1136+
- python.example_code.sns.SnsWrapper.decl
1137+
- python.example_code.sns.SubscribeWithFilters
11201138
Ruby:
11211139
versions:
11221140
- sdk_version: 3
@@ -1217,9 +1235,14 @@ sns_Unsubscribe:
12171235
- sdk_version: 3
12181236
github: python/example_code/sns
12191237
excerpts:
1220-
- snippet_tags:
1238+
- description:
1239+
snippet_tags:
12211240
- python.example_code.sns.SnsWrapper
12221241
- python.example_code.sns.Unsubscribe
1242+
- description:
1243+
snippet_tags:
1244+
- python.example_code.sns.SnsWrapper.decl
1245+
- python.example_code.sns.UnsubscribeTopic
12231246
Swift:
12241247
versions:
12251248
- sdk_version: 1

.doc_gen/metadata/sqs_metadata.yaml

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,13 @@ sqs_CreateQueue:
119119
github: python/example_code/sqs
120120
sdkguide:
121121
excerpts:
122-
- description:
122+
- description:
123123
snippet_tags:
124124
- python.example_code.sqs.CreateQueue
125+
- description:
126+
snippet_tags:
127+
- python.example_code.sqs.SqsWrapper.decl
128+
- python.example_code.sqs.CreateFifoQueue
125129
Ruby:
126130
versions:
127131
- sdk_version: 3
@@ -399,9 +403,13 @@ sqs_DeleteQueue:
399403
github: python/example_code/sqs
400404
sdkguide:
401405
excerpts:
402-
- description:
406+
- description:
403407
snippet_tags:
404408
- python.example_code.sqs.DeleteQueue
409+
- description:
410+
snippet_tags:
411+
- python.example_code.sqs.SqsWrapper.decl
412+
- python.example_code.sqs.DeleteTopicQueue
405413
Ruby:
406414
versions:
407415
- sdk_version: 3
@@ -651,9 +659,13 @@ sqs_ReceiveMessage:
651659
github: python/example_code/sqs
652660
sdkguide:
653661
excerpts:
654-
- description:
662+
- description:
655663
snippet_tags:
656664
- python.example_code.sqs.ReceiveMessage
665+
- description:
666+
snippet_tags:
667+
- python.example_code.sqs.SqsWrapper.decl
668+
- python.example_code.sqs.ReceiveTopicMessage
657669
Ruby:
658670
versions:
659671
- sdk_version: 3
@@ -827,9 +839,13 @@ sqs_DeleteMessageBatch:
827839
github: python/example_code/sqs
828840
sdkguide:
829841
excerpts:
830-
- description:
842+
- description:
831843
snippet_tags:
832844
- python.example_code.sqs.DeleteMessageBatch
845+
- description:
846+
snippet_tags:
847+
- python.example_code.sqs.SqsWrapper.decl
848+
- python.example_code.sqs.DeleteTopicMessageBatch
833849
SAP ABAP:
834850
versions:
835851
- sdk_version: 1
@@ -936,6 +952,14 @@ sqs_GetQueueAttributes:
936952
- description:
937953
snippet_tags:
938954
- TopicsAndQueues.dotnetv3.GetQueueAttributes
955+
Python:
956+
versions:
957+
- sdk_version: 3
958+
github: python/cross_service/topics_and_queues
959+
excerpts:
960+
- snippet_tags:
961+
- python.example_code.sqs.SqsWrapper.decl
962+
- python.example_code.sqs.GetQueueAttributes
939963
C++:
940964
versions:
941965
- sdk_version: 1
@@ -1030,6 +1054,15 @@ sqs_SetQueueAttributes:
10301054
- description: Set the policy attribute of a queue for a topic.
10311055
snippet_tags:
10321056
- TopicsAndQueues.dotnetv3.SetQueueAttributes
1057+
Python:
1058+
versions:
1059+
- sdk_version: 3
1060+
github: python/cross_service/topics_and_queues
1061+
excerpts:
1062+
- description: Set the policy attribute of a queue for a topic.
1063+
snippet_tags:
1064+
- python.example_code.sqs.SqsWrapper.decl
1065+
- python.example_code.sqs.SetQueueAttributes
10331066
Go:
10341067
versions:
10351068
- sdk_version: 2
@@ -1173,6 +1206,19 @@ sqs_Scenario_TopicsAndQueues:
11731206
excerpts:
11741207
- snippet_tags:
11751208
- sns.kotlin.workflow.main
1209+
Python:
1210+
versions:
1211+
- sdk_version: 3
1212+
github: python/cross_service/topics_and_queues
1213+
excerpts:
1214+
- description: Run an interactive scenario at a command prompt.
1215+
snippet_tags:
1216+
- python.example_code.cross_service.topics_and_queues.TopicsAndQueuesScenario
1217+
- description: Create classes that wrap &SNS; and &SQS; operations for use in the scenario.
1218+
snippet_tags:
1219+
- python.example_code.sns.SnsWrapper.class
1220+
- python.example_code.sqs.SqsWrapper.class
1221+
11761222
Swift:
11771223
versions:
11781224
- sdk_version: 1
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
# Topics and Queues Cross-Service Scenario
2+
3+
## Overview
4+
5+
Publish and subscribe is a mechanism for passing information. It’s used in social media, and it’s also used internally in software applications. A producer publishes a message, and the subscribers receive the message. In software, publish and subscribe notifications make message passing flexible and robust. The producers of messages are decoupled from the consumers of messages.
6+
7+
Use the sample code in this folder to explore publishing and subscribing to a topic by using filters and queues. This tutorial does not create a complete end-to-end application. Instead, you can use it to play around with a publish and subscribe architecture.
8+
9+
You can create an Amazon Simple Notification Service (Amazon SNS) topic and subscribe two Amazon Simple Queue Service (Amazon SQS) queues to the topic. You can enable FIFO (First-In-First-Out) queueing, and you can add filtered subscriptions. Then, you can publish messages to the topic and see the results in the queues.
10+
11+
You can publish and subscribe using Amazon SNS alone. But combining Amazon SNS with Amazon SQS gives you more flexibility in how the messages are consumed.
12+
13+
Amazon SNS is a push service. It pushes to endpoints such as email addresses, mobile application endpoints, or SQS queues. (For a full list of endpoints, see [SNS event destinations](https://docs.aws.amazon.com/sns/latest/dg/sns-event-destinations.html)).
14+
15+
With Amazon SQS, messages are received from a queue by polling. With polling, the subscriber receives messages by calling a receive message API. Any code can poll the queue. Also, the messages stay in the queue until you delete them. This gives you more flexibility in how the messages are processed.
16+
17+
The sample code builds a command line application that asks you for input. This is implemented in multiple programming languages, and the interface can vary slightly between languages. The following shows the interface for the Python implementation.
18+
19+
### Create an SNS topic
20+
21+
```
22+
Would you like to work with FIFO topics? (y/n):
23+
```
24+
25+
You configure FIFO (First-In-First-Out) topics when you create them. Choosing a FIFO topic enables other options, too. To learn more, see [FIFO topics example use case](https://docs.aws.amazon.com/sns/latest/dg/fifo-example-use-case.html).
26+
27+
28+
```
29+
Use content-based deduplication instead of entering a deduplication ID? (y/n):
30+
```
31+
32+
Deduplication is only available for FIFO topics. Deduplication prevents the subscriber from responding more than once to events that are determined to be duplicates. If a message gets published to an SNS FIFO topic and it’s found to be a duplicate within the five-minute deduplication interval, the message is accepted but not delivered. For more information, see [Message deduplication for FIFO topics](https://docs.aws.amazon.com/sns/latest/dg/fifo-message-dedup.html).
33+
34+
Content-based deduplication uses a hash of the content as a deduplication ID. If content-based deduplication is not enabled, you must include a deduplication ID with each message.
35+
36+
```
37+
Enter a name for your SNS topic:
38+
```
39+
40+
Topic names can have 1-256 characters. They can contain uppercase and lowercase ASCII letters, numbers, underscores, and hyphens. If you chose a FIFO topic, the application automatically adds a “.fifo” suffix, which is required for FIFO topics.
41+
42+
### Create two SQS queues
43+
44+
Now, configure two SQS queues to subscribe to your topic. Separate queues for each subscriber can be helpful. For
45+
instance, you can customize how messages are consumed and how messages are filtered.
46+
47+
```
48+
Enter a name for SQS queue #1:
49+
```
50+
51+
Queue names can have 1-80 characters. They can contain uppercase and lowercase ASCII letters, numbers, underscores, and hyphens. If you chose a FIFO topic, the application automatically adds a “.fifo” suffix, which is required for FIFO queues.
52+
53+
54+
```
55+
Filter messages for <queue name>'s subscription to the topic? (y/n):
56+
```
57+
58+
If you chose FIFO topics, you can add a filter to the queue’s topic subscription. There are many ways to filter a topic. In this example code, you have the option to filter by a predetermined selection of attributes. For more information about filters, see [Message filtering for FIFO topics](https://docs.aws.amazon.com/sns/latest/dg/fifo-message-filtering.html).
59+
60+
61+
```
62+
You can filter messages by one or more of the following TONE attributes.
63+
Enter a number to add a TONE filter, or enter 0 to stop adding filters.
64+
1. cheerful
65+
2. funny
66+
3. serious
67+
4. sincere
68+
Your choice:
69+
```
70+
71+
If you add a filter, you can select one or more “tone” attributes to filter by. When you’re done, enter “0’” to continue.
72+
73+
The application now prompts you to add the second queue. Repeat the previous steps for the second queue.
74+
75+
The following diagram shows the topic and queue options.
76+
![Diagram of the options](images/fifo_topics_diagram.png)
77+
78+
After you create the topic and subscribe both queues, the application lets you publish messages to the topic.
79+
80+
81+
```
82+
Enter a message to publish:
83+
```
84+
85+
All configurations include a message text.
86+
87+
88+
```
89+
Enter a message group ID for this message:
90+
```
91+
92+
If this is a FIFO topic, then you must include a group ID. The group ID can contain up to 128 alphanumeric characters `(a-z, A-Z, 0-9)` and punctuation `(!"#$%&'()*+,-./:;<=>?@[\]^_``{|}~)`.
93+
For more information about group IDs, see [Message grouping for FIFO topics](https://docs.aws.amazon.com/sns/latest/dg/fifo-message-grouping.html).
94+
95+
96+
```
97+
Enter a deduplication ID for this message:
98+
```
99+
100+
If this is a FIFO topic and content-based deduplication is not enabled, then you must enter a deduplication ID. The message deduplication ID can contain up to 128 alphanumeric characters `(a-z, A-Z, 0-9)` and punctuation `(!"#$%&'()*+,-./:;<=>?@[\]^_``{|}~)`.
101+
102+
103+
```
104+
Add an attribute to this message? (y/n):
105+
```
106+
107+
If you added a filter to one of the subscriptions, you can choose to add a filtering attribute to the message.
108+
109+
110+
```
111+
Enter a number for an attribute:
112+
1. cheerful
113+
2. funny
114+
3. serious
115+
4. sincere
116+
Your choice:
117+
```
118+
119+
Select a number for an attribute.
120+
121+
122+
```
123+
Send another message? (y/n):
124+
```
125+
126+
You can post as many messages as you want.
127+
128+
When you are done posting messages, the application polls the queues and displays their messages.
129+
130+
131+
## ⚠️ Important
132+
133+
* Running this code might result in charges to your AWS account.
134+
* Running the tests might result in charges to your AWS account.
135+
* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege).
136+
* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services).
137+
138+
## Prerequisites
139+
140+
For general prerequisites, see the [README](../../README.md#prerequisites) in the `python` folder.
141+
142+
### Install Python dependencies
143+
144+
The requirements for this example are listed in the accompanying `requirements.txt` file.
145+
146+
Install the requirements by running the following in a virtual environment:
147+
148+
```
149+
python -m pip install -r requirements.txt
150+
```
151+
152+
This installs the AWS SDK for Python (Boto3) and other required packages.
153+
154+
## Instructions
155+
156+
For general instructions to run the examples, see the
157+
[README](../../README.md#run-the-examples) in the `python` folder.
158+
159+
Run this example by running the following command in the folder that contains this README:
160+
161+
```
162+
python topics_and_queues_scenario.py
163+
```
164+
165+
This starts an interactive scenario that walks you through creating topics and queues,
166+
publishing messages with filters, and viewing the results.
167+
168+
Running this example requires AWS Identity and Access Management (IAM) permissions for both SNS and SQS.
169+
170+
## Additional resources
171+
172+
* [Amazon SNS Developer Guide](https://docs.aws.amazon.com/sns/latest/dg/welcome.html)
173+
* [Amazon SNS API Reference](https://docs.aws.amazon.com/sns/latest/api/welcome.html)
174+
* [Amazon SQS Developer Guide](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/welcome.html)
175+
* [Amazon SQS API Reference](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/Welcome.html)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
boto3>=1.26.137
2+
botocore>=1.29.137
3+
pytest>=7.2.1

0 commit comments

Comments
 (0)