Skip to content

Instantly share code, notes, and snippets.

@guybowden
Created April 29, 2019 09:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guybowden/a3a435f21c69630ddc8f3abcebdb9062 to your computer and use it in GitHub Desktop.
Save guybowden/a3a435f21c69630ddc8f3abcebdb9062 to your computer and use it in GitHub Desktop.
simple simple notification service / simple queue service demo

Simple demo of pub and sub to the AWS notification and queue services:

Note: for some reason the queue is not being subscribed to the notification (have to go in and do it in the console).

Just run the two files in two terminals and watch as one program publishes events, whilst another one consumnes them.

Publish events:

$ python sns-pub.py

Consume them:

$ python sns-sub.py
import boto3
import json
import time
import uuid
# Create an SNS client
sns = boto3.client("sns")
response = sns.create_topic(Name="demo-save")
topic_arn = response["TopicArn"]
# Create an SQS Queue
sqs = boto3.resource('sqs')
queue = sqs.create_queue(QueueName="demo-save")
# subscribe queue to topic
sub_response = sns.subscribe(
TopicArn=topic_arn,
Protocol="sqs",
Endpoint=queue.attributes['QueueArn'],
Attributes={"RawMessageDelivery": "true"}
)
print sub_response
while(True):
response = sns.publish(
TopicArn=topic_arn,
Message=json.dumps(
{"default": json.dumps({"product_id": str(uuid.uuid4()), "created": True})}
),
MessageStructure="json",
)
print response
time.sleep(0.1)
import boto3
import json
import time
# Create an SQS Queue
sqs = boto3.resource('sqs')
queue = sqs.create_queue(QueueName="demo-save")
print queue
while(True):
for message in queue.receive_messages(
WaitTimeSeconds=20, MaxNumberOfMessages=5):
print message.body
# Let the queue know that the message is processed
message.delete()
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment