-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_gpt_aws_sqs_sns.py
57 lines (49 loc) · 2.33 KB
/
example_gpt_aws_sqs_sns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import boto3
from botocore.exceptions import ClientError
AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
AWS_DEFAULT_REGION = os.environ['AWS_DEFAULT_REGION']
def create_sqs_queue(queue_name):
sqs_client = boto3.client('sqs', aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION)
try:
response = sqs_client.create_queue(QueueName=queue_name)
return response['QueueUrl']
except ClientError as e:
print(e)
return None
def create_sns_topic(topic_name):
sns_client = boto3.client('sns', aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION)
try:
response = sns_client.create_topic(Name=topic_name)
return response['TopicArn']
except ClientError as e:
print(e)
return None
def subscribe_sqs_queue_to_sns_topic(queue_url, topic_arn):
sqs_client = boto3.client('sqs', aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION)
try:
queue_arn = sqs_client.get_queue_attributes(QueueUrl=queue_url, AttributeNames=['QueueArn'])['Attributes']['QueueArn']
sns_client = boto3.client('sns', aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
region_name=AWS_DEFAULT_REGION)
response = sns_client.subscribe(TopicArn=topic_arn, Protocol='sqs', Endpoint=queue_arn)
return response['SubscriptionArn']
except ClientError as e:
print(e)
return None
def integration_test(queue_name, topic_name):
queue_url = create_sqs_queue(queue_name)
print(f"SQS Queue created: {queue_url}")
topic_arn = create_sns_topic(topic_name)
print(f"SNS Topic created: {topic_arn}")
subscription_arn = subscribe_sqs_queue_to_sns_topic(queue_url, topic_arn)
print(f"SQS Queue subscribed to SNS Topic: {subscription_arn}")
if __name__ == '__main__':
integration_test(queue_name='test_queue', topic_name='test_topic')