-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqs.py
124 lines (99 loc) · 4.07 KB
/
sqs.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/local/bin/python3
"""
This script was created by Madani Napaul.
• Checks if an SQS Queue with name 'cpd-sqs-s1903342' already exists
• If it does not exist, attempt to create the SQS Queue
• Check if a queue with the same name has been deleted within 60 seconds
• If yes, catch the error and start a 60-second timer
• If no error is thrown while creating the queue, exit
• Attach the required access policy after its creation for 'SendMessage' access
"""
# Import the required libraries
import boto3
import json
import botocore
import time
import sys
# Create the resource for sqs
sqs = boto3.client("sqs")
# Set the SQS Queue Name
sqs_name = "cpd-sqs-s1903342"
# Open the 'sqs_policy.json' file and load it to return a JSON object
with open("/Users/madani/Desktop/CPD_Coursework/sqs_policy.json") as json_file:
policy = json.load(json_file)
# This function verifies if the SQS Queue already exists
# If the queue exists, an error is thrown and printed in the console
# If a queue with the same name has been deleted within 60 seconds, an error is thrown
# A timer is launched an the queue creation proceeds after 60 seconds has passed
# After the creation of the queue, the required access policy for 'SendMessage' is attached
def get_queues(sqs):
try:
sqs.get_queue_url(QueueName="cpd-sqs-s1903342")
print(
"\n\033[93m"
+ " ••• SQS QUEUE 'cpd-sqs-s1903342' ALREADY EXISTS\n"
+ "\033[0m"
)
return True
except botocore.exceptions.ClientError as e:
error_code = e.response["Error"]["Code"]
try:
if error_code == "AWS.SimpleQueueService.NonExistentQueue":
sqs.create_queue(
QueueName=sqs_name, Attributes={"ReceiveMessageWaitTimeSeconds": "20"}
)
print(
"\n\033[96m"
+ " ••• CREATING SQS QUEUE 'cpd-sqs-s1903342'\n"
+ "\033[0m"
)
time.sleep(5)
print("\033[96m" + " ••• ATTACHING REQUIRED ACCESS POLICIES'" + "\033[0m")
time.sleep(5)
result = sqs.get_queue_url(QueueName="cpd-sqs-s1903342")
sqs.set_queue_attributes(
QueueUrl=result["QueueUrl"], Attributes={"Policy": json.dumps(policy)}
)
print(
"\n\033[96m"
+ " ••• SQS QUEUE 'cpd-sqs-s1903342' HAS BEEN CREATED\n"
+ "\033[0m"
)
except botocore.exceptions.ClientError as x:
error_code_ = x.response["Error"]["Code"]
if error_code_ == "AWS.SimpleQueueService.QueueDeletedRecently":
print(
"\n\033[93m"
+ " ••• ANOTHER SQS QUEUE WITH THE SAME NAME CAN BE CREATED 60 SECONDS AFTER DELETION, PLEASE WAIT\n"
+ "\033[0m"
)
for remaining in range(60, 0, -1):
sys.stdout.write("\r")
sys.stdout.write(
"\033[93m" + " ••• {:2d} SECONDS REMAINING".format(remaining)
)
sys.stdout.flush()
time.sleep(1)
sys.stdout.write(
"\033[92m" + "\r ••• SQS QUEUE SCRIPT LAUNCHED \n"
)
sqs.create_queue(
QueueName=sqs_name, Attributes={"ReceiveMessageWaitTimeSeconds": "20"}
)
print(
"\n\033[96m" + " ••• CREATING SQS QUEUE 'cpd-sqs-s1903342'" + "\033[0m"
)
time.sleep(5)
print("\n\033[96m" + " ••• ATTACHING REQUIRED ACCESS POLICIES" + "\033[0m")
time.sleep(5)
print(
"\n\033[96m"
+ " ••• SQS QUEUE 'cpd-sqs-s1903342' HAS BEEN CREATED\n"
+ "\033[0m"
)
result = sqs.get_queue_url(QueueName="cpd-sqs-s1903342")
sqs.set_queue_attributes(
QueueUrl=result["QueueUrl"], Attributes={"Policy": json.dumps(policy)}
)
return False
get_queues(sqs)