-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQS_DLQ_boto3_services.py
199 lines (156 loc) · 4.81 KB
/
SQS_DLQ_boto3_services.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import boto3
import json
import datetime
# Clients and Resources initialization
sqs_client = boto3.client('sqs')
sqs_resource = boto3.resource('sqs')
# #Dead letter queue arn
DEAD_LETTER_QUEUE_ARN = 'arn:aws:sqs:ap-south-1:288154355795:waitingQueue.fifo' # here we need to assign manually dead letter queue arn
# DEAD_LETTER_MAIN_QUEUE= '' #we need to define ARN of dead letter queue here
#main queue url
MAIN_QUEUE_URL = 'https://sqs.ap-south-1.amazonaws.com/288154355795/getDeadMsg.fifo'
#DLQ Url
DLQ_Url = ''
# message
static_message = {
"data": [
{
"item": "1"
},
{
"item": "2"
},
{
"item": "3"
}
]
}
# sample_message = {
# "data": {
# "item1": "1",
# "item2": "2",
# "item3": "3"
# }
# }
# data = dict(static_message)
# Creating SQS with some parameters and enable DLQ
def create_SQS_fifo():
createQueue = sqs_resource.create_queue(
QueueName = 'getDeadMsg.fifo', # For FIFO queue it must contains 'fifo' suffix at end of name.
Attributes = {
'RedrivePolicy': json.dumps({
"deadLetterTargetArn": DEAD_LETTER_QUEUE_ARN,
"maxReceiveCount": "2",
}),
"FifoQueue": "true",
},
# tags = {
# 'string': 'string'
# }
)
return createQueue
# Creating dlq which are help to provide relationship with main SQS with help of aws arn
def create_dlq_sqs_fifo():
createDlq = sqs_resource.create_queue(
QueueName = 'waitingQueue.fifo',
Attributes = {
"FifoQueue": "true",
}
)
return createDlq
# getting queue name list
def search_queue(QueueNamePrifix): # (QueueNamePrifix,NextToken,MaxResults): all optional to search
result = sqs_client.list_queues(
QueueNamePrefix = QueueNamePrifix) # (QueueNamePrefix=QueueNamePrifix,NextToken=NextToken,MaxResults=MaxResults)
return result
# list all the queues
def list_queues():
return sqs_client.list_queues()
# deletin queue
def delete_queue():
return sqs_client.delete_queue(
QueueUrl = 'main queue url'
)
def get_queue_url(queuename):
return sqs_client.get_queue_url(QueueName = queuename)
# sending message to sqs
def send_message_to_queue():
response = sqs_client.send_message(
QueueUrl = MAIN_QUEUE_URL,
MessageAttributes = {
'item1': {
'DataType': 'String',
'StringValue': '1'
},
'item2': {
'DataType': 'String',
'StringValue': '2'
},
'item3': {
'DataType': 'String',
'StringValue': '3'
}
},
MessageBody = "The msg send successfully!!!!!!!!",
# MessageDeduplicationId = 'duplicationTest',
MessageGroupId = 'messageGroup1'
)
return response
# sending msg using barches
def send_batch_messages_to_queue():
response = sqs_client.send_message_batch(
QueueUrl = MAIN_QUEUE_URL,
Entries = static_message
)
# recieving msges
def receive_messages_in_queue():
return sqs_client.receive_message(
QueueUrl = MAIN_QUEUE_URL,
# AttributeNames= '',
MaxNumberOfMessages = 10,
MessageAttributeNames = [
'All'
],
VisibilityTimeout= 0,
WaitTimeSeconds = 0
)
def delete_message_from_queue(receipt_handle):
sqs_client.delete_message(
QueueUrl = 'main queue url',
ReceiptHandle = receipt_handle
)
def purge_queue():
return sqs_client().purge_queue(
QueueUrl = 'MAIN_QUEUE_URL'
)
def lambda_handler(event, context):
# creating dlq
# dlq_queue = create_dlq_sqs_fifo()
dlq_queue = sqs_resource.get_queue_by_name(QueueName = 'waitingQueue.fifo')
# main queue creating as standard type
# main_queue = create_SQS()
# Main queue of fifo type
# main_queue= create_SQS_fifo()
main_queue = sqs_resource.get_queue_by_name(QueueName = 'getDeadMsg.fifo')
# send message
messageSend1 = send_message_to_queue()
messageSend2 = send_message_to_queue()
messageSend3 = send_message_to_queue()
messageSend4 = send_message_to_queue()
msg_receive = receive_messages_in_queue()
#response print
print("\nDLQ = \n", dlq_queue)
print("\nmain queue = \n", main_queue)
print("\nMsg receive =\n ",msg_receive)
print("\nmessage response 1 to 4 = \n", messageSend1, "\n", messageSend2, "\n", messageSend3, "\n", messageSend4)
# get single message during queue
# for msg in static_message["data"]:
return {
"statusCode": 200,
"body": json.dumps(
{
# "DLQ Name" :dlq_queue,
# "message response": messageSend
}
)
}